Link to home
Start Free TrialLog in
Avatar of ironfly
ironfly

asked on

Trying to use colormap to replace color in png, with a gradient to transparent

I have some code that opens a transparent png with a black object on it, it successfully replaces the black section using a colormap. The issue is for a png that has an object that fades from black to transparent, the color map is only replacing the first few pixels of true black and leaving the gradient to transparent untouched. I am hoping for some help! I am not clear on how the semi transparent pixels are colored, or how i would replace them. Existing code below:

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
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Ca you attach these two images?
Avatar of ironfly
ironfly

ASKER

File attached.  you would need to change the name in the function call to test2.png instead of test.png.  thanks
test2.png
Posting from phone so not going in too much detail but basically you need to look for the tone not the exact color.

I cannot find the function I normally but see one that will suffice on http://stackoverflow.com/questions/12464072/how-do-i-replace-tone-variations-of-the-same-color-in-a-c-sharp-bitmap

These compare colors with a tolerance but I have used one that used the alpha tone (I think)
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ironfly

ASKER

Thanks, that did the trick!
If my comment helped you, please mark it as the answer :)
Avatar of ironfly

ASKER

Shaun provided a quick and helpful link to an example that did exactly what i needed, after struggling to get this working for 2 days i got it working in less than 30 minutes.