问题
A part of a software package I've written takes in a binary encoded PDF, converts it to a Bitmap, and saves it to a blob in a db. I'm discovering that although it's not immediately visible in the original in it's PDF form, there is transparency in the document that is completely visible in the converted bitmap.
I've seen other posts where the command line version of imagemagick can "flatten" an image to remove the transparency layers. Can this somehow be done with the .NET package? How do I remove the transparency before saving the bitmap? I need to keep it as a bitmap as it's a universally recognized format for their label printers.
/// <summary>
/// Write image data from a pdf file to a bitmap file
/// </summary>
/// <param name="imgData"></param>
private static void convertPdfToBmp(ImageData imgData)
{
MagickReadSettings settings = new MagickReadSettings();
// Settings the density to 600 dpi will create an image with a better quality
settings.Density = new Density(600);
using (MagickImageCollection images = new MagickImageCollection())
{
// Add all the pages of the pdf file to the collection
images.Read(imgData.pdfFilePath, settings);
// Create new image that appends all the pages horizontally
using (IMagickImage image = images.AppendVertically())
{
// Convert the image to a bitmap
image.Format = MagickFormat.Bmp;
// Delete any old file
if (File.Exists(imgData.bmpFilePath))
{
File.Delete(imgData.bmpFilePath);
}
// Save result as a bmp
image.Write(imgData.bmpFilePath);
}
}
}
I have tried to use different methods of adjusting the transparency layers, or coloring of the background without success.
//image.BackgroundColor = new ColorMono(false);
//image.TransparentChroma(new MagickColor(0, 0, 0), new MagickColor(0, 0, 0));
//image.TransparentChroma(new ColorRGB(0, 0, 0), new ColorRGB(65535, 65535, 65535));
//image.BackgroundColor = new MagickColor("#fff");
//image.BackgroundColor = new MagickColor("#ffffff");
//image.BackgroundColor = new MagickColor("#ffffffffffff");
//image.Settings.BackgroundColor.A = 0;
回答1:
Try removing the Alpha component, and set the background color depending on the output:
image.Alpha(AlphaOption.Remove);
来源:https://stackoverflow.com/questions/53093839/removing-transparency-using-imagemagick-net