Hallo zusammen, eventuell kann mir jemand helfen.
Zum Thema: Ich möchte, wenn man auf ein Bild klickt eine Farbe durch eine andere ersetzen. Dies geht schon. Leider geht das natürlich nur mit dem genau angeklickten Pixel.
Jetzt würde ich gerne einen Farbbereich angeben können. So das die ähnlichen Farben auch ersetzt werden. Solch eine Funktion kennt man ja aus den Grafikprogrammen. Dort kann man auch mit der Pipette auswählen und den Farbbereich erweiter.
Mein Testprojekt sieht derzeit so aus:
Picturebox1 = Hier wird das Bild angezeigt
Button2 = Das Bild wird geladen
Button1 = Ersetzt die Ausgewählte Farbe
Label1 = hier wird der Name der Farbe (Pixel) angezeigt
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form1
Dim pixColor As Color
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click
Dim myColor As Color = Color.FromArgb(pixColor.ToArgb)
PictureBox1.Image = ReplaceColor(Image.FromFile( _
"c:\test\Muster01.jpg"), myColor, Color.Black)
End Sub
Public Function ReplaceColor(ByVal oBitmap As Image, ByVal OldColor As _
Color, ByVal NewColor As Color) As Image
Dim oMap(0) As ColorMap
oMap(0) = New ColorMap
With oMap(0)
.OldColor = OldColor
.NewColor = NewColor
End With
Dim oAttr As New ImageAttributes
oAttr.SetRemapTable(oMap)
Dim imgWidth As Integer = oBitmap.Width
Dim imgHeight As Integer = oBitmap.Height
Dim oImage As Image = Image.FromHbitmap( _
New Bitmap(imgWidth, imgHeight).GetHbitmap)
Using g As Graphics = Graphics.FromImage(oImage)
g.DrawImage(oBitmap, New Rectangle(0, 0, imgWidth, imgHeight), _
0, 0, imgWidth, imgHeight, GraphicsUnit.Pixel, oAttr)
End Using
Return oImage
End Function
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click
PictureBox1.Image = Image.FromFile("C:\test\Muster01.jpg")
End Sub
' benötigte API-Funktionen
<DllImport("gdi32.dll")> _
Private Shared Function CreateDC( _
ByVal lpszDriver As String, _
ByVal lpszDevice As String, _
ByVal lpszOutput As String, _
ByVal lpInitData As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll")> _
Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")> _
Private Shared Function GetPixel( _
ByVal hdc As IntPtr, _
ByVal nXPos As Integer, _
ByVal nYPos As Integer) As Integer
End Function
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As _
Color
Dim hdcScreen As IntPtr = CreateDC("Display", Nothing, Nothing, _
IntPtr.Zero)
Dim colorRef As Integer = GetPixel(hdcScreen, x, y)
DeleteDC(hdcScreen)
Return Color.FromArgb(colorRef And &HFF, _
(colorRef And &HFF00) >> 8, (colorRef And &HFF0000) >> 16)
End Function
Private Sub PictureBox1_Click(sender As System.Object, e As _
System.EventArgs) Handles PictureBox1.Click
pixColor = GetPixelColor(Cursor.Position.X, Cursor.Position.Y)
Label1.Text = pixColor.Name
Label1.ForeColor = pixColor
End Sub
Private Sub PictureBox1_MouseHover(sender As Object, e As System.EventArgs) _
Handles PictureBox1.MouseHover
Me.Cursor = Cursors.Cross
End Sub
Private Sub PictureBox1_MouseLeave(sender As Object, e As System.EventArgs) _
Handles PictureBox1.MouseLeave
Me.Cursor = Cursors.Default
End Sub
End Class Vielen Dank für eure Hilfe
Gruß Stam
Beitrag wurde zuletzt am 25.10.11 um 19:11:06 editiert. |