■■■

2016年4月3日日曜日

画像のガンマ補正を行う方法

画像のガンマ補正を行う方法
VB.NET
Public Shared Function CreateGammaAdjustedImage(ByVal img As Image, _
ByVal gammaValue As Single) As Image

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

Dim g As Graphics = Graphics.FromImage(newImg)

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

ia.SetGamma(1 / gammaValue)

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 CreateGammaAdjustedImage(Image img, float gammaValue)
{

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

Graphics g = Graphics.FromImage(newImg);

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

ia.SetGamma(1/gammaValue);

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

g.Dispose();

return newImg;
}

■■■