关闭从画布渲染的黑白位图的抗锯齿功能
我对 WPF 的文本抗锯齿有疑问。在我的应用程序中,用户设计要使用特殊打印设备打印的文本或图像字段。我使用画布来托管字段,这些字段可以是文本块或图像。
问题是当我将此画布转换为 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 之前关闭抗锯齿功能?
I have a problem with WPF's anti aliasing of text. In my application the user designs text or image fields to be printed using a special printing device. I am using a canvas to host the fields, which are either textblocks or Images
The problem is when i convert this canvas into a RenderTargetBitmap, then into a black and white FormatConvertedBitmap, the text and images end up extremely fuzzy looking.
I have tried using "snaptodevicepixels = true" and "RenderOptions.EdgeMode" = Aliased on everything in the application. I know anti aliasing is great for the screen, but printing is a real disaster.
Here is an example of my code:
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;
}
how do i turn off the antiailasing before creating the rendertargetbitmap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://blogs. msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
显然抖动类型是硬编码的
http://blogs.msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
Apparently dither type is hardcoded