关闭从画布渲染的黑白位图的抗锯齿 [英] Turn off antialiasing for black and white bitmap Rendered from canvas

查看:152
本文介绍了关闭从画布渲染的黑白位图的抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WPF的文本反锯齿有疑问.在我的应用程序中,用户设计要使用特殊打印设备打印的文本或图像字段.我正在使用画布托管字段,这些字段可以是文本块,也可以是图像

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

问题是,当我将此画布转换为RenderTargetBitmap,然后转换为黑白FormatConvertedBitmap时,文本和图像最终显得非常模糊.

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.

我尝试对应用程序中的所有内容使用"snaptodevicepixels = true"和"RenderOptions.EdgeMode" = Alias.我知道抗锯齿功能对于屏幕非常有用,但是打印是一个真正的灾难.

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.

这是我的代码示例:

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之前,如何关闭抗锯齿功能?

how do i turn off the antiailasing before creating the rendertargetbitmap?

推荐答案

显然抖动类型是硬编码的

Apparently dither type is hardcoded

FormatConvertedBitmap

此类包装标准WIC像素格式 转换器(IWICImagingFactory :: CreateFormatConverter).这个组成部分 提供了从一种像素格式转换为另一种像素格式的方法, 将抖动和半色调处理为索引格式,调色板 翻译和Alpha阈值处理.源,DestinationFormat, DestinationPalette和AlphaThreshold属性用于 通过初始化基础组件 IWICFormatConverter ::初始化. 抖动类型被硬编码为 WICBitmapDitherTypeErrorDiffusion.调色板转换类型为 硬编码为WICBitmapPaletteTypeMedianCut. ISupportInitialize 接口用于在以下情况下捕捉属性的值 初始化完成.对属性的进一步更改是 忽略了.

FormatConvertedBitmap

This class wraps the standard WIC pixel format converter (IWICImagingFactory::CreateFormatConverter). This component provides the means of converting from one pixel format to another, handling dithering and halftoning to indexed formats, palette translation and alpha thresholding. The Source, DestinationFormat, DestinationPalette, and AlphaThreshold properties are used to initialize the underlying component via IWICFormatConverter::Initialize. The dither type is hard-coded to be WICBitmapDitherTypeErrorDiffusion. The palette translation type is hard-coded to be WICBitmapPaletteTypeMedianCut. The ISupportInitialize interface is used to snap the values of the properties when initialization is complete. Further changes to the properties are ignored.

这篇关于关闭从画布渲染的黑白位图的抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆