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
| Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Function RenvoyerCouleurTableau(ByVal Image As Bitmap, ByVal Tableau() As Byte, ByVal X As Integer, Y As Integer) As String
If X < 0 Then
X = 0
ElseIf X > Image.Width - 1 Then
X = Image.Width - 1
End If
If Y < 0 Then
Y = 0
ElseIf Y > Image.Height - 1 Then
Y = Image.Height - 1
End If
Return Tableau((X * 4) + (Y * Image.Width * 4) + 2).ToString & ", " & Tableau((X * 4) + (Y * Image.Width * 4) + 1).ToString & ", " & Tableau((X * 4) + (Y * Image.Width * 4)).ToString & ", " & Tableau((X * 4) + (Y * Image.Width * 4) + 3).ToString
End Function
Private Function ChangerCouleurTableau(ByVal Image As Bitmap, ByVal Tableau() As Byte, ByVal X As Integer, Y As Integer, ByVal Couleur As System.Drawing.Color) As Boolean
Dim Rouge As Byte = Couleur.R
Dim Vert As Byte = Couleur.G
Dim Bleu As Byte = Couleur.B
Dim Alpha As Byte = Couleur.A
If X < 0 Then
X = 0
ElseIf X > Image.Width - 1 Then
X = Image.Width - 1
End If
If Y < 0 Then
Y = 0
ElseIf Y > Image.Height - 1 Then
Y = Image.Height - 1
End If
Tableau((X * 4) + (Y * Image.Width * 4) + 2) = Rouge
Tableau((X * 4) + (Y * Image.Width * 4) + 1) = Vert
Tableau((X * 4) + (Y * Image.Width * 4)) = Bleu
Tableau((X * 4) + (Y * Image.Width * 4) + 3) = Alpha
Return True
End Function
Private Sub LockUnlockBitsExample(ByVal e As PaintEventArgs)
' Créer une nouvelle Image bitmap.
Dim Image As New Bitmap("Votre image ici")
' Créer un rectangle.
Dim Rectangle As New Rectangle(0, 0, Image.Width, Image.Height)
' Verrouille les Bits de l'image bitmap.
Dim ImageData As System.Drawing.Imaging.BitmapData = Image.LockBits(Rectangle, Drawing.Imaging.ImageLockMode.ReadWrite, Image.PixelFormat)
' Renvoie l'adresse de la première ligne de l'image bitmap.
Dim AdressePremièreLigneImage As IntPtr = ImageData.Scan0
' Déclare un tableau pour contenir les octets de l'image bitmap.
' Ce code est spécifique à une image bitmap avec 24 bits par pixels.
Dim Bytes As Integer = Math.Abs(ImageData.Stride) * Image.Height
Dim TableauValeurRGB(Bytes - 1) As Byte
' Copie les valeurs RGB des couleurs dans le tableau.
System.Runtime.InteropServices.Marshal.Copy(AdressePremièreLigneImage, TableauValeurRGB, 0, Bytes)
' Exemple de manipulations des informations dans le tableau pour renvoyer ou modifier une couleurs dans l'image
' -------------------------------------------------------------------------------------------------------------
' Affiche la couleur du pixel dans le tableau qui se trouve en 2, 2 sur l'image
Debug.WriteLine(RenvoyerCouleurTableau(Image, TableauValeurRGB, 2, 2))
' Change la couleur du pixel dans le tableau qui se trouve en 2, 2 sur l'image
ChangerCouleurTableau(Image, TableauValeurRGB, 2, 2, Color.FromArgb(255, 255, 2, 255)) ' Alpha, Rouge, Vert, Bleu
' Affiche la couleur du pixel dans le tableau qui se trouve en 2, 2 sur l'image
Debug.WriteLine(RenvoyerCouleurTableau(Image, TableauValeurRGB, 2, 2))
' -------------------------------------------------------------------------------------------------------------
' -------------------------------------------------------------------------------------------------------------
' Copie les valeurs RGB des couleurs du tableau vers l'image bitmap.
System.Runtime.InteropServices.Marshal.Copy(TableauValeurRGB, 0, AdressePremièreLigneImage, Bytes)
' Déverrouille les Bits de l'image bitmap.
Image.UnlockBits(ImageData)
' Affiche l'image modifier.
e.Graphics.DrawImage(Image, 0, 0)
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
LockUnlockBitsExample(e)
End Sub
End Class |
Partager