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 116 117 118
| 'Le but de ce code est de pouvoir copier une image vers une matrice et vice versa.
'Pour illustrer ceci, ce code va premièrement copier une picturebox dans une matrice
'puis il va recopier cette même matrice dans une autre picture box et les deux
'transformations sont faites d'une manière directe sans passer pixel par pixel (Ultra rapides)
'Insérer deux images Picturebox dans une forme : Picture1 (Source) et Picture2 (Destination)
'Origine de la source (Largement modifiée)
'KPD-Team 1999
'URL: <a href="http://www.allapi.net/" target="_blank">http://www.allapi.net/</a>
'E-Mail: KPDTeam AT Allapi DOT net
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 NHeight, MWidth As Integer
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 Form_Load()
Dim i, j As Integer
Picture1.Picture = LoadPicture("c:\candle.jpg")
'Copie de l'image1 vers une matrice
Call MatrixFromImage(Picture1, Matrice())
'Inversion des couleurs de l'image
'Tout les calculs se font maintenant sur la matrice (que c'est facile et rapide !)
For i = 1 To NHeight
For j = 1 To MWidth
Matrice(i, j).Blue = 255 - Matrice(i, j).Blue
Matrice(i, j).Green = 255 - Matrice(i, j).Green
Matrice(i, j).Red = 255 - Matrice(i, j).Red
Next j
Next i
'Copie de la matrice vers l'image2
Call ImageFromMatrix(Picture2, Matrice())
End Sub |
Partager