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

标签 c# wpf bitmap antialiasing

我对 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 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.

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

相关文章:

actionscript-3 - BitmapData类的threshold方法中mask参数有什么作用?

C#/HLSL 和 XNA - 在 HLSL 中通过百分比将 2 种颜色混合在一起

wpf - 从绑定(bind)到 xml 的按钮样式列表框中获取单击的按钮内容

c# - .net 没有按照我的命令开始任务 - 开始新任务的逻辑是什么

c# - 如何实现异步命令

java - 什么是在浏览器中运行的简单位图编辑器(jquery/java/javascript)?

android - Android 存储位图信息的最有效方式

C# 单例对象的多个实例

c# - 我的 Action 被调用了两次

c# - 如何在 C# 中从另一个表达式创建一个表达式?