1

I'm using Magick.Net to create pdf portrait thumbnails.
All thumbnails should keep same thumbnail size regardless of pdf size.
Consequently, I'm first resizing the portrait image to fit the required area and then applying the Frame(width,height) command to compensate for missing width or height.
However, the Frame() command adds a grey layer over the thumbnail.
How can I make this layer transparent with C#?

async Task<string> Base64Thumbnail(IBrowserFile file, int thumbnailWidth, int thumbnailHeight)
{
    string retval = "";
    Stream stream = file.OpenReadStream(maxAllowedSize: 10000000);
    MemoryStream ms = new MemoryStream();
    await stream.CopyToAsync(ms);
    stream.Close();

    ms.Seek(0, SeekOrigin.Begin);
    using (MagickImageCollection images = new MagickImageCollection())
    {
        MagickReadSettings settings = new MagickReadSettings();
        settings.FrameIndex = 0; 
        settings.FrameCount = 1; 

        images.Read(ms, settings);
        if (images.Count > 0)
        {
            using (var image = images.First())
            {
                image.Format = MagickFormat.Png;
                var imgHeight = image.Height;
                var imgWidth = image.Width;
                var landscape = imgWidth / imgHeight > thumbnailWidth / thumbnailHeight;
                int resizeWidth = landscape ? thumbnailWidth : imgWidth * thumbnailHeight / imgHeight;
                int resizeHeight = landscape ? imgHeight * thumbnailWidth / imgWidth : thumbnailHeight;
                int frameWidth = (thumbnailWidth - resizeWidth) / 2;
                int frameHeight = (thumbnailHeight - resizeHeight) / 2;
                image.Resize(resizeWidth, resizeHeight);

                MagickGeometry geo = new MagickGeometry(frameWidth, frameHeight);
                image.Frame(geo);
                image.RePage();

                retval = @"data:image/png;base64," + image.ToBase64(MagickFormat.Png);
            }
        }
        return retval;
    }
}
Matias Masso
  • 1,670
  • 3
  • 18
  • 28

0 Answers0