尝试使用colormap替换png中的颜色,渐变为透明

时间:2016-12-13 17:42:14

标签: asp.net vb.net

我有一些代码打开一个带有黑色物体的透明png,它使用色彩映射成功替换了黑色部分。问题在于png具有从黑色渐变到透明的对象,颜色贴图仅替换真黑色的前几个像素,并使渐变保持透明不变。我希望得到一些帮助!我不清楚半透明像素是如何着色的,或者我将如何替换它们。现有代码如下:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    ChangeImageColor(Image.FromFile(Server.MapPath("test.png")), Color.Black, Color.Blue)
End Sub

Public Sub ChangeImageColor(ByVal image As System.Drawing.Image, ByVal oldColor As System.Drawing.Color, ByVal newColor As System.Drawing.Color)
    Try
        Dim newImage As System.Drawing.Image = DirectCast(image.Clone, System.Drawing.Image)
        Dim g1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
        Dim colorMap(0) As System.Drawing.Imaging.ColorMap

        colorMap(0) = New System.Drawing.Imaging.ColorMap
        colorMap(0).OldColor = oldColor
        colorMap(0).NewColor = newColor

        Dim imageAttr As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes
        imageAttr.SetThreshold(0.7, ColorAdjustType.Default)

        imageAttr.SetRemapTable(colorMap)

        g1.DrawImage(newImage, New System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), 0, 0, newImage.Width, newImage.Height, Drawing.GraphicsUnit.Pixel, imageAttr)

        Dim ms As New IO.MemoryStream

        Response.ContentType = "image/png"
        newImage.Save(ms, ImageFormat.Png)
        ms.WriteTo(Response.OutputStream)

        newImage.Dispose()
    Catch ex As Exception
        Response.Write(ex.ToString)
    End Try
End Sub

0 个答案:

没有答案