使用Imagemagick.NET删除透明度 [英] Removing Transparency using Imagemagick.NET
问题描述
我编写的软件包的一部分将采用二进制编码的PDF,将其转换为位图,然后将其保存到db中的blob中.我发现,尽管它不是立即以PDF格式显示在原始文档中,但文档中的透明度在转换后的位图中是完全可见的.
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.
我看过其他帖子,其中该命令imagemagick的行版本可以展平"图像以除去透明层.可以通过.NET包完成此操作吗?如何在保存位图之前删除透明度?我需要将其保留为位图,因为它是标签打印机的通用格式.
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;
推荐答案
尝试删除Alpha组件,并根据输出设置背景色:
Try removing the Alpha component, and set the background color depending on the output:
image.Alpha(AlphaOption.Remove);
这篇关于使用Imagemagick.NET删除透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!