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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
Option Explicit
Option Base 1
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Private Type Pixel
Red As Byte
Green As Byte
Blue As Byte
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim Matrice() As Pixel
Dim Matrice2() As Pixel
Dim Rvb As Long
Dim NHeight, MWidth As Integer
'renvoi le niveau de gris du Rvb
Private Function Gris(bloc As Pixel) As Long
Dim Moyenne As Long
Moyenne = (bloc.Red + (bloc.Green / 256) + (bloc.Blue / 65536)) / 3
Gris = Moyenne
End Function
'***************************************************************************
'Procedure qui copie une image PictureBox vers une matrice de pixels
Private Sub MatrixFromImage(Picture As PictureBox, Matrice() As Pixel)
Dim PicBits() As Byte, PicInfo As BITMAP
Dim Size As Long
Dim i, j As Integer
Dim Z As Long
GetObject Picture.Image, Len(PicInfo), PicInfo
Size = PicInfo.bmWidth * PicInfo.bmBitsPixel * PicInfo.bmHeight / 8
ReDim PicBits(Size) As Byte
ReDim Matrice(PicInfo.bmHeight, PicInfo.bmWidth) As Pixel
GetBitmapBits Picture.Image, Size, PicBits(1)
For i = 1 To PicInfo.bmHeight
For j = 1 To PicInfo.bmWidth
Z = (i - 1) * PicInfo.bmWidth * 4 + (j - 1) * 4 + 1
Matrice(i, j).Blue = PicBits(Z)
Matrice(i, j).Green = PicBits(Z + 1)
Matrice(i, j).Red = PicBits(Z + 2)
Next j
Next i
NHeight = PicInfo.bmHeight
MWidth = PicInfo.bmWidth
End Sub
Private Sub ImageFromMatrix(Picture As PictureBox, Matrice() As Pixel)
Dim PicBits() As Byte
Dim i, j As Integer
Dim Z As Long
ReDim PicBits(UBound(Matrice(), 1) * UBound(Matrice(), 2) * 4)
For i = 1 To UBound(Matrice(), 1)
For j = 1 To UBound(Matrice(), 2)
Z = Z + 1
PicBits(Z) = Matrice(i, j).Blue
PicBits(Z + 1) = Matrice(i, j).Green
PicBits(Z + 2) = Matrice(i, j).Red
PicBits(Z + 3) = 0
Z = Z + 3
Next j
Next i
SetBitmapBits Picture.Image, UBound(PicBits), PicBits(1)
Picture.Refresh
End Sub
Private Sub Command1_Click()
Dim chemin As String
chemin = App.Path
If Right(chemin, 1) <> "\" Then
chemin = chemin & "\" & "Images-test\"
End If
chemin = chemin & "test-niveau-de-gris.jpg"
Picture1.Picture = LoadPicture(chemin)
End Sub
Private Sub Command2_Click()
Dim i, j As Integer
Call MatrixFromImage(Picture1, Matrice())
Picture2.Width = Picture1.Width
Picture2.Height = Picture1.Height
For i = 1 To NHeight
For j = 1 To MWidth
Matrice2(i, j).Blue = Gris(Matrice(i, j))
Matrice2(i, j).Green = Gris(Matrice(i, j))
Matrice2(i, j).Red = Gris(Matrice(i, j))
Next j
Next i
Call ImageFromMatrix(Picture2, Matrice2())
End Sub |
Partager