現在書いているプログラム(近日公開)で、画面キャプチャした画像を表示する際、そのまま表示すると本物の画面と紛らわしいので、明るさを落として表示させたいと思いました。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace DarkenBitmap
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image image;
private void Form1_Load(object sender, EventArgs e)
{
image = new Bitmap(@"s:\test.jpg");
this.pictureBox1.Image = image;
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
float[][] colorMatrixElements = {
new float[] {0.5f, 0, 0, 0, 0},
new float[] {0, 0.5f, 0, 0, 0},
new float[] {0, 0, 0.5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
e.Graphics.DrawImage(
image,
new Rectangle(0, 0, width, height), // destination rectangle
0, 0, // upper-left corner of source rectangle
width, // width of source rectangle
height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
}
}
ずっと昔、OS/2のPresentation ManagerのAPIを見ていて、座標の変換を行列で指定可能なのを見たのに匹敵する感動かな?(既に記憶があやふやなので確かではないかも) 当時のWindows 2.x~3.0の座標系は縦横のスケーリングはできても、行列指定などという「自由度は高いが真面目に実装するとやたCPUパワーを食いそうな」機能は持っていなかったので。