Bonjour,

j'ai trouvé une source qui me permet d'inverser les couleurs d'une image de façon optimisée, j'aimerai plutôt remplacer une couleur contenue dans l'image par une autre mais je suis un peu perdu pour savoir comment faire, notemment au niveau du calcul des codes couleurs RGB des méthodes Getpixel et Setpixel de la classe BitMapFast.

Voici la classe utilisée :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
Imports System.Drawing
Imports System.Drawing.Imaging
 
Public Class BitmapFast
 
    Private b() As Byte
    Private i_Width As Integer
    Private i_Height As Integer
    Private i_Stride As Integer
    Private i_ColorSize As Integer
    Public Disposed As Boolean
 
    Public Sub New(ByVal filename As String)
        If (filename = "") Or (System.IO.File.Exists(filename) = False) Then
            MsgBox("file '" & filename & "' not found!")
            Exit Sub
        End If
 
        Dim pv_bitmap As Bitmap = Nothing
        pv_bitmap = New Bitmap(filename)
        i_Width = pv_bitmap.Width
        i_Height = pv_bitmap.Height
 
        Dim bounds As Rectangle = New Rectangle(0, 0, i_Width, i_Height)
        Dim bitmapData As BitmapData = pv_bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
        i_Stride = bitmapData.Stride
        i_ColorSize = bitmapData.Stride / bitmapData.Width
        ReDim b(bitmapData.Height * i_Stride)
        System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, b, 0, b.Length - 1)
        pv_bitmap.UnlockBits(bitmapData)
 
        pv_bitmap.Dispose()
        pv_bitmap = Nothing
    End Sub
 
    Public Function Getpixel(ByVal x As Integer, ByVal y As Integer) As Color
        Dim red, green, blue As Byte
        red = b(x * i_ColorSize + y * i_Stride)
        green = b((x * i_ColorSize + 1) + y * i_Stride)
        blue = b((x * i_ColorSize + 2) + y * i_Stride)
        Return Color.FromArgb(255, red, green, blue)
    End Function
 
    Public Sub Setpixel(ByVal x As Integer, ByVal y As Integer, ByVal color As Color)
        b(x * i_ColorSize + y * i_Stride) = color.R
        b((x * i_ColorSize + 1) + y * i_Stride) = color.G
        b((x * i_ColorSize + 2) + y * i_Stride) = color.B
    End Sub
 
    Public Function Get_Bitmap() As Bitmap
        Get_Bitmap = New Bitmap(i_Width, i_Height, PixelFormat.Format24bppRgb)
        Dim bounds As Rectangle = New Rectangle(0, 0, i_Width, i_Height)
        Dim bitmapData As Imaging.BitmapData = Get_Bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
        bitmapData.Stride = CInt(bitmapData.Stride / i_Width) * i_Width
        System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length - 1)
        Get_Bitmap.UnlockBits(bitmapData)
    End Function
 
    Public Sub Dispose()
        If Disposed = True Then Exit Sub
        Erase b
        Disposed = True
    End Sub
 
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        If Disposed = False Then Dispose()
    End Sub
 
End Class
Et voici le code qui appelle cette classe :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
Public Class Form1
    Private map0 As BitmapFast
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tps As Integer
        map0 = New BitmapFast(Application.StartupPath & "\..\data\heightmap.png")
        For j As Integer = 0 To 255
            For i As Integer = 0 To 255
                tps = map0.Getpixel(i, j).ToArgb
            Next
        Next
        PictureBox1.Image = map0.Get_Bitmap
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Color_S, Color_D As Color
        For j As Integer = 0 To 255
            For i As Integer = 0 To 255
                Color_S = map0.Getpixel(i, j)
                Color_D = Color.FromArgb(255, 255 - Color_S.B, 255 - Color_S.G, 255 - Color_S.R)
                map0.Setpixel(i, j, Color_D)
            Next
        Next
        PictureBox1.Image = map0.Get_Bitmap
    End Sub
End Class
Merci de votre aide.