ご提示のサンプルを元に以下のようなメソッドを作ってみました。 ------------------------------------------------------------ Public Sub AddCopy(ByVal Srcimg As Bitmap, ByVal Desimg As Bitmap)
'1ピクセルあたりのバイト数を取得する Dim SrcpixelFormat As PixelFormat = Srcimg.PixelFormat Dim SrcpixelSize As Integer = Image.GetPixelFormatSize(SrcpixelFormat) / 8 If SrcpixelSize < 3 OrElse 4 < SrcpixelSize Then Throw New ArgumentException( "1ピクセルあたり24または32ビットの形式のイメージのみ有効です。", "Srcimg") End If
Dim DespixelFormat As PixelFormat = Desimg.PixelFormat Dim DespixelSize As Integer = Image.GetPixelFormatSize(DespixelFormat) / 8 If DespixelSize < 3 OrElse 4 < DespixelSize Then Throw New ArgumentException( "1ピクセルあたり24または32ビットの形式のイメージのみ有効です。", "Desimg") End If
'Bitmapをロックする Dim SrcbmpDate As BitmapData = Srcimg.LockBits(New Rectangle(0, 0, Srcimg.Width, Srcimg.Height), ImageLockMode.ReadWrite, Srcimg.PixelFormat)
If SrcbmpDate.Stride < 0 Then Srcimg.UnlockBits(SrcbmpDate) Throw New ArgumentException( "ボトムアップ形式のイメージには対応していません。", "Srcimg") End If
Dim DesbmpDate As BitmapData = Desimg.LockBits(New Rectangle(0, 0, Desimg.Width, Desimg.Height), ImageLockMode.ReadWrite, Desimg.PixelFormat)
If DesbmpDate.Stride < 0 Then Desimg.UnlockBits(DesbmpDate) Throw New ArgumentException( "ボトムアップ形式のイメージには対応していません。", "Desimg") End If
'ピクセルデータをバイト型配列で取得する Dim Srcptr As IntPtr = SrcbmpDate.Scan0 Dim Srcpixels As Byte() = New Byte(SrcbmpDate.Stride * Srcimg.Height - 1) {} System.Runtime.InteropServices.Marshal.Copy(Srcptr, Srcpixels, 0, Srcpixels.Length)
Dim Desptr As IntPtr = DesbmpDate.Scan0 Dim Despixels As Byte() = New Byte(DesbmpDate.Stride * Desimg.Height - 1) {} System.Runtime.InteropServices.Marshal.Copy(Desptr, Despixels, 0, Despixels.Length)
'すべてのピクセルの色を補正する For y As Integer = 0 To SrcbmpDate.Height - 1 For x As Integer = 0 To SrcbmpDate.Width - 1 'ピクセルデータでのピクセル(x,y)の開始位置を計算する Dim Srcpos As Integer = y * SrcbmpDate.Stride + x * SrcpixelSize Dim Despos As Integer = y * DesbmpDate.Stride + x * DespixelSize
'新しい色を計算する Dim SrcnewR As Integer = Srcpixels(Srcpos + 2) Dim SrcnewG As Integer = Srcpixels(Srcpos + 1) Dim SrcnewB As Integer = Srcpixels(Srcpos + 0)
Dim DesnewR As Integer = Despixels(Despos + 2) Dim DesnewG As Integer = Despixels(Despos + 1) Dim DesnewB As Integer = Despixels(Despos + 0)
Dim newR As Integer = SrcnewR And DesnewR Dim newG As Integer = SrcnewG And DesnewG Dim newB As Integer = SrcnewB And DesnewB
'色を変更する Despixels(Srcpos + 2) = CByte(newR) Despixels(Srcpos + 1) = CByte(newG) Despixels(Srcpos) = CByte(newB) Next Next