r3plica
r3plica

Reputation: 13397

ImageMagick and transparency issues when converting images

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?

Upvotes: 2

Views: 1273

Answers (1)

dlemstra
dlemstra

Reputation: 8153

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);
}

Upvotes: 2

Related Questions