Imports System.Drawing.Imaging Imports System.Runtime.InteropServices Public Class Form1 Dim imageFilePath As String = "hogehoge.bmp" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim img As Image = Image.FromFile(imageFilePath) Me.BackgroundImage = GetChangeContrastImage1(img, 2) 'コントラストが2倍になる Me.BackgroundImage = GetChangeContrastImage2(img, 0.5) 'コントラストが1/2になる End Sub 'GetPixel,SetPixelバージョン、遅い Private Function GetChangeContrastImage1(ByVal image As Image, ByVal contrast As Double) As Image Dim level As Double = 127.5 Dim bmp As New Bitmap(image) For y As Integer = 0 To bmp.Height - 1 For x As Integer = 0 To bmp.Width - 1 Dim pixel As Color = bmp.GetPixel(x, y) Dim r, g, b As Byte r = Math.Min(Math.Max(Byte.MinValue, (pixel.R - level) * contrast + level), Byte.MaxValue) g = Math.Min(Math.Max(Byte.MinValue, (pixel.G - level) * contrast + level), Byte.MaxValue) b = Math.Min(Math.Max(Byte.MinValue, (pixel.B - level) * contrast + level), Byte.MaxValue) bmp.SetPixel(x, y, Color.FromArgb(r, g, b)) Next Next Return bmp End Function 'LockBitsバージョン、速い Private Function GetChangeContrastImage2(ByVal image As Image, ByVal contrast As Double) As Image Dim level As Double = 127.5 Dim bmp As New Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb) Using g As Graphics = Graphics.FromImage(bmp) g.DrawImage(image, Point.Empty) End Using Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb) Dim bmpDataBytes(bmpData.Stride * bmpData.Height) As Byte Marshal.Copy(bmpData.Scan0, bmpDataBytes, 0, bmpDataBytes.Length) For i As Integer = 0 To bmpDataBytes.Length - 1 bmpDataBytes(i) = Math.Min(Math.Max(Byte.MinValue, (bmpDataBytes(i) - level) * contrast + level), Byte.MaxValue) Next Marshal.Copy(bmpDataBytes, 0, bmpData.Scan0, bmpDataBytes.Length) bmp.UnlockBits(bmpData) Return bmp End Function End Class