转换图像时ImageMagick和透明度问题

时间:2016-03-01 17:48:30

标签: c# imagemagick imagemagick-convert

我有这种转换图像的方法。我这样做了:

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

但是运行那段代码,即使源文件是,生成的图像也不透明。 有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

您的源SVG文件不透明,但您可以告诉ImageMagick / Magick.NET使用透明背景。你应该读这样的图像:

// Create our image
using (var image = new MagickImage())
{
  image.Settings.BackgroundColor = MagickColors.Transparent;
  image.Read(data);
}