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

问题描述

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

推荐答案

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

显然抖动类型是硬编码的

Apparently dither type is hardcoded

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

05-28 13:08