Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Converting from AI/SVG to PNG with transparent backgrounds.

$
0
0
Hey,
So I am having an issue with converting my images to png while maintaining transparency.
My code currently looks like this:
public async Task<string> ConvertImage(byte[] data)
{

    // Reader settings
    var settings = new MagickReadSettings
    {
        Density = new PointD(300, 300),
        ColorSpace = ColorSpace.RGB,
        Format = MagickFormat.Svg
    };

    // Create our image
    using (var image = new MagickImage(data, settings))
    {

        // Create a new memory stream
        using (var memoryStream = new MemoryStream())
        {

            // Set our image properties
            image.Format = MagickFormat.Png;
            image.Resize(600, 600);      

            // Write to our stream
            image.Write(memoryStream);

            // Reset our memory stream position
            memoryStream.Position = 0;

            // Create a new blob block to hold our image
            var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString() + ".png");

            // Upload to azure
            await blockBlob.UploadFromStreamAsync(memoryStream);

            // Return the blobs url
            return blockBlob.StorageUri.PrimaryUri.ToString();
        }
    }
}
The image is created successfully, but it is never transparent. I did try doing this:
 image.TransparentChroma(new MagickColor("white"), new MagickColor("white"));
and that sort of works (except there is a clear white outline to the image). This is not a valid solution though, because some of my images are actually white on a transparent background.
Does anyone know how I can convert and retain the transparency?

I am using Magick.Net-Q16-AnyCPU.

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>