1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
Public Class Form1
Public g_RowSizeBytes As Integer
Public g_PixBytes1() As Byte
Public g_PixBytes2() As Byte
Private m_BitmapData1 As BitmapData
Private m_BitmapData2 As BitmapData
' Lock the bitmap's data.
Public Sub LockBitmap(ByVal bm As Bitmap, ByRef m_BitmapData As BitmapData, ByRef g_PixBytes() As Byte)
' Lock the bitmap data.
Dim bounds As Rectangle = New Rectangle( _
0, 0, bm.Width, bm.Height)
m_BitmapData = bm.LockBits(bounds, _
Imaging.ImageLockMode.ReadWrite, _
Imaging.PixelFormat.Format24bppRgb)
g_RowSizeBytes = m_BitmapData.Stride
' Allocate room for the data.
Dim total_size As Integer = m_BitmapData.Stride * _
m_BitmapData.Height
ReDim g_PixBytes(total_size)
' Copy the data into the g_PixBytes array.
Marshal.Copy(m_BitmapData.Scan0, g_PixBytes, _
0, total_size)
End Sub
Public Sub UnlockBitmap(ByVal bm As Bitmap, ByRef m_BitmapData As BitmapData, ByRef g_PixBytes() As Byte)
' Copy the data back into the bitmap.
Dim total_size As Integer = m_BitmapData.Stride * _
m_BitmapData.Height
Marshal.Copy(g_PixBytes, 0, _
m_BitmapData.Scan0, total_size)
' Unlock the bitmap.
bm.UnlockBits(m_BitmapData)
' Release resources.
g_PixBytes = Nothing
m_BitmapData = Nothing
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Lock the bitmap data.
Dim bm1 As New Bitmap("C:\Image1.jpg")
Dim bm2 As New Bitmap("C:\Image2.jpg")
LockBitmap(bm1, m_BitmapData1, g_PixBytes1)
LockBitmap(bm2, m_BitmapData2, g_PixBytes2)
Dim pix = 0
For Y = 0 To bm1.Height - 1
For X = 0 To bm1.Width - 1
'comparaison de pixel
If Not (g_PixBytes1(pix) = g_PixBytes2(pix) And g_PixBytes1(pix + 1) = g_PixBytes2(pix + 1) And g_PixBytes1(pix + 2) = g_PixBytes2(pix + 2)) Then
g_PixBytes2(pix) = 0
g_PixBytes2(pix + 1) = 0
g_PixBytes2(pix + 2) = 255
End If
pix += 3
Next X
Next Y
' Unlock the bitmap data.
UnlockBitmap(bm1, m_BitmapData1, g_PixBytes1)
UnlockBitmap(bm2, m_BitmapData2, g_PixBytes2)
bm2.Save("C:\Image_result.jpg", ImageFormat.Jpeg)
End Sub
End Class |
Partager