2

I have this method that converts a image. I have done it like this:

    /// <summary>
    /// Converts any image to png and uploads it to azure
    /// </summary>
    /// <param name="data">The byte array of the image</param>
    /// <returns>The path to the azure blob</returns>
    public async Task<string> ConvertImage(byte[] data)
    {

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

            //// Resize the image
            //image.Resize(600, 600);

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

                // Set to a png
                image.Format = MagickFormat.Png;
                image.VirtualPixelMethod = VirtualPixelMethod.Transparent;
                image.Write(memoryStream);
                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();
            }
        }
    }

but run that bit of code, the image generated is not transparent even though the source file is. Does anyone know why?

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • Solution for this [convert storagefile(anyimageformat) to transparent PNG file](https://stackoverflow.com/questions/63278900/how-to-convert-storagefile-png-image-to-transparent-png-bitmap-in-c?noredirect=1#comment111898572_63278900) – Faraaz bhilwade Aug 06 '20 at 09:47

1 Answers1

2

Your source SVG file is not transparent but you can tell ImageMagick/Magick.NET to use a transparent background. You should read the image like this:

// Create our image
using (var image = new MagickImage())
{
  image.Settings.BackgroundColor = MagickColors.Transparent;
  image.Read(data);
}
dlemstra
  • 7,813
  • 2
  • 27
  • 43