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.Fro mFile(Serv er.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.Fr omImage(ne wImage)
Dim colorMap(0) As System.Drawing.Imaging.Col orMap
colorMap(0) = New System.Drawing.Imaging.Col orMap
colorMap(0).OldColor = oldColor
colorMap(0).NewColor = newColor
Dim imageAttr As System.Drawing.Imaging.Ima geAttribut es = New System.Drawing.Imaging.Ima geAttribut es
imageAttr.SetThreshold(0.7 , ColorAdjustType.Default)
imageAttr.SetRemapTable(co lorMap)
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.Output Stream)
newImage.Dispose()
Catch ex As Exception
Response.Write(ex.ToString )
End Try
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
ChangeImageColor(Image.Fro
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.Fr
Dim colorMap(0) As System.Drawing.Imaging.Col
colorMap(0) = New System.Drawing.Imaging.Col
colorMap(0).OldColor = oldColor
colorMap(0).NewColor = newColor
Dim imageAttr As System.Drawing.Imaging.Ima
imageAttr.SetThreshold(0.7
imageAttr.SetRemapTable(co
g1.DrawImage(newImage, New System.Drawing.Rectangle(0
Dim ms As New IO.MemoryStream
Response.ContentType = "image/png"
newImage.Save(ms, ImageFormat.Png)
ms.WriteTo(Response.Output
newImage.Dispose()
Catch ex As Exception
Response.Write(ex.ToString
End Try
End Sub
Ca you attach these two images?
ASKER
File attached. you would need to change the name in the function call to test2.png instead of test.png. thanks
test2.png
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, that did the trick!
If my comment helped you, please mark it as the answer :)
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.