Christian Sauer
Christian Sauer

Reputation: 10889

Vb.net changing colors of a picture

I am struggling with the following problem: I have a small picture with is painted in red. This color must be changed to another color (users'choice). I used msdn and some googling did the following:

    Private Function GetPicture(Iterator As Integer, tempfile As String) As String
        Dim Rstring = ""

        If Colors.Count = 0 OrElse Iterator >= Colors.Count Then
            Rstring = tempfile
        Else
            Dim NewPicture = My.Computer.FileSystem.GetTempFileName()
            My.Computer.FileSystem.CopyFile(tempfile, NewPicture, True)
            Dim mypict = New Bitmap(NewPicture)

            Dim ColorList As New List(Of Color)

            For x = 0 To mypict.Width - 1
                For y = 0 To mypict.Height - 1
                    Dim mypixel = mypict.GetPixel(x, y)

                    If ColorList.Contains(mypixel) = False Then
                        ColorList.Add(mypixel)
                    End If
                Next
            Next

            Dim NewColor = Color.FromArgb(255, 0, 0, 255)
            Dim ListOfColorMaps As New List(Of ColorMap)
            For Each elem In ColorList
                Dim newcolormap = New ColorMap
                newcolormap.OldColor = elem
                newcolormap.NewColor = NewColor
                ListOfColorMaps.Add(newcolormap)
            Next

            Dim imageAttributes As New ImageAttributes()
            Dim width As Integer = mypict.Width
            Dim height As Integer = mypict.Height
            Dim colorMap As New ColorMap()


            'colorMap.OldColor = Color.FromArgb(255, 0, 0, 0) ' opaque red
            'colorMap.NewColor = Color.FromArgb(255, 0, 0, 255) ' opaque blue
            Dim remapTable As ColorMap() = ListOfColorMaps.ToArray
            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)

            Dim tempBmp = New Bitmap(width, height)
            Dim g = Graphics.FromImage(tempBmp)
            g.DrawImage(tempBmp, New Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttributes)

            g.Save()
            g.Dispose()
            mypict.Dispose()

            Dim NewFileName = NewPicture.Remove(NewPicture.IndexOf("."c) - 1) & ".png"
            tempBmp.Save(NewFileName, Imaging.ImageFormat.Png)

            My.Computer.FileSystem.DeleteFile(NewPicture)

            tempBmp.Dispose()
            Rstring = NewPicture
        End If


        Return Rstring

The Code runs without exceptions, and it seems to find the desired colors but the saved tempbmp contains no picture. Does this happen because the code runs in a dll without graphic?

You can pretty much ignore the "IF" part - that has something to do with another usecase.

Greetings and sincere thanks Christian Sauer

Upvotes: 1

Views: 3070

Answers (1)

Alex Essilfie
Alex Essilfie

Reputation: 12613

You are getting no picture displayed because you are drawing an empty bitmap.

Your problem starts here:

Dim tempBmp = New Bitmap(width, height)
Dim g = Graphics.FromImage(tempBmp)
g.DrawImage(tempBmp, New Rectangle(0, 0, width, height), 0, 0, width, height, _
            GraphicsUnit.Pixel, imageAttributes)
  • You create a new bitmap (probably with a white background).
  • Then you create a new Graphics object from your empty bitmap.
  • Then you draw the empty bitmap onto itself.

What you should be doing is drawing the mypict object (which is the bitmap whose colors you want to change). Thus your third line should be as follows:

g.DrawImage(mypict, New Rectangle(0, 0, width, height), 0, 0, width, height, _
            GraphicsUnit.Pixel, imageAttributes)

Since the Graphics object g is associated with tempBmp (which is empty prior to the DrawImage operation) drawing mypict will draw to it with your parameters.

One other recommendation is that you remove the g.Save() line. You save a graphics object when you plan to restore it later. Doing a Graphics.Save() does not save a picture. What really saves the changes you have made is the tempBmp.Save() line.

Upvotes: 1

Related Questions