gpt4 book ai didi

c# - 使用 Imagemagick.NET 去除透明度

转载 作者:行者123 更新时间:2023-12-04 00:03:50 48 4
gpt4 key购买 nike

我编写的软件包的一部分接收二进制编码的 PDF,将其转换为位图,并将其保存到数据库中的 blob。我发现虽然它在 PDF 形式的原始文件中不会立即可见,但在转换后的位图中完全可见文档中的透明度。

我见过 other posts imagemagick 的命令行版本可以“展平”图像以删除透明层。这可以通过 .NET 包以某种方式完成吗?如何在保存位图之前删除透明度?我需要将其保留为位图,因为它是标签打印机普遍认可的格式。

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

我尝试使用不同的方法来调整透明度图层或背景着色,但均未成功。

            //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 组件,并根据输出设置背景颜色:

image.Alpha(AlphaOption.Remove);

关于c# - 使用 Imagemagick.NET 去除透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093839/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com