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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| Public Class Form1
Private Const TRACKBAR_NAME As String = "TrackBar"
Private Const TRACKBAR_RESOLUTION As Single = 100
Private _newColorMatrix()() As Single = {New Single() {1, 1, 1, 0, 0}, _
New Single() {1, -0.8, -0.8, 0, 0}, _
New Single() {1, 0.8, 0.8, 0, 0}, _
New Single() {1, 0.8, 0.8, 1, 0}, _
New Single() {-0.3, -1, -1, 0, 1}}
#Region "*** CONSTRUCTEURS "
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
InitializeTrackBar()
Me.upb_Image.Image = System.Drawing.Image.FromFile("Image.jpg")
Me.upb_Result.Image = Nothing
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
#End Region
#Region "*** EVENEMENTS "
Private Sub ucp_ImageTransparentColor_ColorChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ucp_ColorToReplace.ColorChanged, ucp_Color.ColorChanged
Me.upb_Result.Image = ReplaceColor2(Me.upb_Image.Image, ucp_ColorToReplace.Color, ucp_Color.Color)
End Sub
Private Sub tb_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As System.Windows.Forms.TrackBar = TryCast(sender, System.Windows.Forms.TrackBar)
If tb IsNot Nothing Then
Dim pos As System.Drawing.Point = tb.Tag
Me._newColorMatrix(pos.Y)(pos.X) = tb.Value * TRACKBAR_RESOLUTION
End If
upb_Result.Image = ReplaceColor(Me.upb_Image.Image, ucp_ColorToReplace.Color, ucp_Color.Color)
End Sub
Private Sub tb_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As System.Windows.Forms.TrackBar = TryCast(sender, System.Windows.Forms.TrackBar)
If tb IsNot Nothing Then
RemoveHandler tb.ValueChanged, AddressOf tb_ValueChanged
RemoveHandler tb.Disposed, AddressOf tb_Disposed
End If
End Sub
#End Region
#Region "*** METHODES "
Private Sub InitializeTrackBar()
With Me.tlp_TrackBar
.Controls.Clear()
.RowCount = Me._newColorMatrix.GetLength(0)
.RowStyles.Clear()
.ColumnCount = Me._newColorMatrix(0).GetLength(0)
.ColumnStyles.Clear()
For j As Integer = 0 To .RowCount - 1
.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(SizeType.Percent, 50.0!))
For i As Integer = 0 To .ColumnCount - 1
.RowStyles.Add(New System.Windows.Forms.RowStyle(SizeType.Percent, 50.0!))
Dim tb As New System.Windows.Forms.TrackBar
With tb
.Name = GetTrackBarName(i, j)
.Minimum = -1 * TRACKBAR_RESOLUTION
.LargeChange = 0.5 * TRACKBAR_RESOLUTION
.SmallChange = 0.1 * TRACKBAR_RESOLUTION
.TickFrequency = 1 * TRACKBAR_RESOLUTION
.Maximum = 1 * TRACKBAR_RESOLUTION
.Value = Me._newColorMatrix(j)(i) * TRACKBAR_RESOLUTION
.Tag = New System.Drawing.Point(i, j)
End With
AddHandler tb.ValueChanged, AddressOf tb_ValueChanged
AddHandler tb.Disposed, AddressOf tb_Disposed
.Controls.Add(tb)
Application.DoEvents()
Next i
Next j
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
End With
End Sub
Public Function GetTrackBarName(ByVal i As Integer, ByVal j As Integer) As String
Return TRACKBAR_NAME & "_" & i.ToString & "x" & j.ToString
End Function
Public Function ReplaceColor(ByVal Img As System.Drawing.Image, ByVal ColorToReplace As System.Drawing.Color, ByVal Color As System.Drawing.Color) As System.Drawing.Image
Dim oImg As System.Drawing.Image = Img.Clone
Try
Dim ia As New System.Drawing.Imaging.ImageAttributes
Dim cm As System.Drawing.Imaging.ColorMatrix
cm = New System.Drawing.Imaging.ColorMatrix(Me._newColorMatrix)
'cm = New System.Drawing.Imaging.ColorMatrix(New Single()() _
' {New Single() {1, 1, 1, 0, 0}, _
' New Single() {1, -0.8, -0.8, 0, 0}, _
' New Single() {1, 0.8, 0.8, 0, 0}, _
' New Single() {1, 0.8, 0.8, 1, 0}, _
' New Single() {-0.3, -1, -1, 0, 1}})
ia.SetColorMatrix(cm)
Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oImg)
oGraphics.DrawImage(oImg, New Rectangle(0, 0, oImg.Width, oImg.Height), 0, 0, oImg.Width, oImg.Height, GraphicsUnit.Pixel, ia)
Return oImg
Catch ex As Exception
Return oImg
End Try
End Function
Public Function ReplaceColor2(ByVal Img As System.Drawing.Image, ByVal ColorToReplace As System.Drawing.Color, ByVal NewColor As System.Drawing.Color) As System.Drawing.Image
Dim oImg As System.Drawing.Image = Img.Clone
Try
'Création d'un Graphics pour travailler sur l'image "Peinture"
Dim PeintureGraphics As Graphics = Graphics.FromImage(oImg)
'Création d'un mappage Transparent -> Magenta!
Dim map(0) As System.Drawing.Imaging.ColorMap
map(0) = New System.Drawing.Imaging.ColorMap()
map(0).OldColor = ColorToReplace
map(0).NewColor = NewColor
'Création d'attributs d'image, avec le mappage créer juste avant comme contenu
Dim myAttributs As New System.Drawing.Imaging.ImageAttributes()
myAttributs.SetRemapTable(map)
'Ce rectangle me servira apres pour dire à quel endroit placer l'image dans l'image "Peinture"
Dim Rect As New Rectangle(0, 0, oImg.Width, oImg.Height)
PeintureGraphics.DrawImage(oImg, Rect, 0, 0, oImg.Width, oImg.Height, GraphicsUnit.Pixel, myAttributs)
Return oImg
Catch ex As Exception
Return oImg
End Try
End Function
#End Region
End Class |
Partager