gpt4 book ai didi

c# - 关闭从 Canvas 渲染的黑白位图的抗锯齿

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:11 31 4
gpt4 key购买 nike

我对 WPF 的文本抗锯齿有疑问。在我的应用程序中,用户设计要使用特殊打印设备打印的文本或图像字段。我正在使用 Canvas 来托管字段,这些字段是文本 block 或图像

问题是当我将此 Canvas 转换为 RenderTargetBitmap,然后转换为黑白 FormatConvertedBitmap 时,文本和图像看起来非常模糊。

我尝试在应用程序中的所有内容上使用“snaptodevicepixels = true”和“RenderOptions.EdgeMode”= Aliased。我知道抗锯齿对屏幕非常有用,但打印是一场真正的灾难。

这是我的代码示例:

private BitmapSource GetCanvasAsBitmap(Canvas cvs)
{
cvs.Background = Brushes.White;
RenderOptions.SetEdgeMode(cvs, EdgeMode.Aliased);
cvs.SnapsToDevicePixels = true;

// render TopCanvas visual tree to the RenderTargetBitmap

Size CanvasSize = new Size(cvs.Width, cvs.Height);
cvs.Measure(CanvasSize);
Rect CanvasRect = new Rect(CanvasSize);
cvs.Arrange(CanvasRect);

RenderTargetBitmap targetBitmap =
new RenderTargetBitmap((int)cvs.ActualWidth,
(int)cvs.ActualHeight,
96, 96,
PixelFormats.Default);

targetBitmap.Render(cvs);


double scale = PIXELSCALE / cvs.Height;
ScaleTransform ST = new ScaleTransform(scale, scale);

TransformedBitmap TB = new TransformedBitmap(targetBitmap, ST);

return TB;
}



private static FormatConvertedBitmap Recolor(BitmapSource b)
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(b));

using (FileStream fs = new FileStream("BeforeRecolor.bmp", FileMode.Create))
{
encoder.Save(fs);
fs.Flush();
fs.Close();
}

FormatConvertedBitmap FCB = new FormatConvertedBitmap(b, PixelFormats.Indexed1, new BitmapPalette(new List<Color>() { Colors.Black, Colors.White }), 0);

BmpBitmapEncoder en = new BmpBitmapEncoder();
en.Frames.Add(BitmapFrame.Create(FCB));

using (FileStream fs = new FileStream("AfterRecolor.bmp", FileMode.Create))
{
en.Save(fs);
fs.Flush();
fs.Close();
}

return FCB;
}

如何在创建 rendertargetbitmap 之前关闭抗锯齿?

最佳答案

http://blogs.msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx

显然抖动类型是硬编码的

FormatConvertedBitmap

This class wraps the standard WIC pixel formatconverter (IWICImagingFactory::CreateFormatConverter). This componentprovides the means of converting from one pixel format to another,handling dithering and halftoning to indexed formats, palettetranslation and alpha thresholding. The Source, DestinationFormat,DestinationPalette, and AlphaThreshold properties are used toinitialize the underlying component viaIWICFormatConverter::Initialize. The dither type is hard-coded to beWICBitmapDitherTypeErrorDiffusion. The palette translation type ishard-coded to be WICBitmapPaletteTypeMedianCut. The ISupportInitializeinterface is used to snap the values of the properties wheninitialization is complete. Further changes to the properties areignored.

关于c# - 关闭从 Canvas 渲染的黑白位图的抗锯齿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550731/

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