■■■

2016年4月3日日曜日

画像をセピア色に変換する方法

画像をセピア色に変換する方法
VB.NET
Public Shared Function CreateSepiatoneImage(ByVal img As Image) As Image

Dim newImg As New Bitmap(img.Width, img.Height)

Dim g As Graphics = Graphics.FromImage(newImg)

Dim cm As New System.Drawing.Imaging.ColorMatrix( _
New Single()() { _
New Single() {0.393F, 0.349F, 0.272F, 0, 0}, _
New Single() {0.769F, 0.686F, 0.534F, 0, 0}, _
New Single() {0.189F, 0.168F, 0.131F, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})

Dim ia As New System.Drawing.Imaging.ImageAttributes()

ia.SetColorMatrix(cm)

g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), _
0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)

g.Dispose()

Return newImg
End Function

C#
public static Image CreateSepiatoneImage(Image img)
{

Bitmap newImg = new Bitmap(img.Width, img.Height);

Graphics g = Graphics.FromImage(newImg);

System.Drawing.Imaging.ColorMatrix cm =
new System.Drawing.Imaging.ColorMatrix(
new float[][] {
new float[] {.393f, .349f, .272f, 0, 0},
new float[] {.769f, .686f, .534f, 0, 0},
new float[] {.189f, .168f, .131f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});

System.Drawing.Imaging.ImageAttributes ia =
new System.Drawing.Imaging.ImageAttributes();

ia.SetColorMatrix(cm);

g.DrawImage(img,
new Rectangle(0, 0, img.Width, img.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);

g.Dispose();

return newImg;
}

■■■