Search code examples
vb.netimagemagickimagemagick-convertimagemagick-identify

Converting Background of a Image to transparent Using imagemagick programaticaly


I am trying to Convert all the white background of a Image (generated from Html using a plugin) to transparent so that I can use it as a stamp

Using image As MagickImage = New MagickImage(filenamelocation)
                    image.Alpha(AlphaOption.Set)
                    image.ColorFuzz = New Percentage(40)
                    image.Settings.BackgroundColor = MagickColors.Transparent
                    image.Settings.FillColor = MagickColors.White
                    image.FloodFill(MagickColors.Transparent, 0, 0)   
                    image.Write(Path.Combine(Server.MapPath("~/Uploads/attachment/"), "stampimagename.png"))
                End Using

My png image is as this

Iam using ImageMagick nuget package for doing the same and needs to convert all the white to transparent Only blue Color items shows remain ,Can somebody advice what I am doing wrong


Solution

  • I don't think you're supposed to declare the background as transparent then flood fill a white (not really sure what that means anyway..)

    As I understand it from reading the command line operations that do this (and translating to VB; it seems that mostly the command line and VB are similar) you're just supposed to load the image, declare that white is the transparency color, with some fuzz factor maybe, and then save it as a format that supports transparency:

    Using image As MagickImage = New MagickImage(filenamelocation)
    
        image.ColorFuzz = new Percentage(10) 'declare a lower fuzz factor
        img.Transparent(MagickColors.White) 'declare that the transparency should apply to anything that is white 
        img.Write("image.png") 'save as a format that support transparency. jpg does not
    
    End Using