Vb.net改变图片的颜色

时间:2013-01-07 21:10:34

标签: vb.net graphics colors

我正在努力解决以下问题:我画了一张红色的小画。此颜色必须更改为其他颜色(用户选择)。我使用了msdn,一些谷歌搜索做了以下事情:

    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

代码运行没有例外,它似乎找到了所需的颜色,但保存的tempbmp不包含图片。这是否会发生,因为代码在没有图形的dll中运行?

你几乎可以忽略“IF”部分 - 它与另一个用例有关。

问候和真诚的感谢 Christian Sauer

1 个答案:

答案 0 :(得分:1)

由于您正在绘制空位图,因此未显示任何图片。

你的问题从这里开始:

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)
  • 您创建一个新的位图(可能带有白色背景)。
  • 然后从空位图创建一个新的Graphics对象。
  • 然后将空位图绘制到自身上。

您应该做的是绘制mypict对象(您想要更改其颜色的位图)。因此,您的第三行应如下:

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

由于Graphics对象gtempBmp相关联(在DrawImage操作之前为空),因此绘图mypict将使用您的参数绘制它。

另一个建议是删除g.Save()行。计划稍后还原时,可以保存图形对象。执行Graphics.Save()不能保存图片。真正保存所做更改的是tempBmp.Save()行。