关闭从画布渲染的黑白位图的抗锯齿功能

发布于 2024-11-17 21:05:28 字数 1885 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-11-24 21:05:28

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

显然抖动类型是硬编码的

格式转换位图

此类包装了标准 WIC 像素格式
转换器(IWICImagingFactory::CreateFormatConverter)。该组件
提供从一种像素格式转换为另一种像素格式的方法,
处理索引格式、调色板的抖动和半色调
翻译和阿尔法阈值。源、目标格式、
DestinationPalette 和 AlphaThreshold 属性用于
通过初始化底层组件
IWICFormatConverter::初始化。 抖动类型被硬编码为
WICBitmapDitherTypeErrorDiffusion。
调色板转换类型为
硬编码为 WICBitmapPaletteTypeMedianCut。 ISupport初始化
接口用于捕捉属性的值
初始化完成。属性的进一步更改是
被忽略。

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

Apparently dither type is hardcoded

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文