图像质量模糊

本文关键字:模糊 图像质量 | 更新日期: 2024-09-22 20:33:34

我有一种情况,我想把一些XAML转换成图像,所以我创建了一个RichTextBox,然后拍摄了它的图像。现在的问题是图像中的单词模糊了,你知道我该怎么解决吗?

public System.Drawing.Bitmap ConvertXamltoImage(string XamlString, int Width, int Height)
{
    RichTextBox AdContentRichTextBox = new RichTextBox() { Width = Width, Height = Height };
    AdContentRichTextBox.BorderThickness = new Thickness(0);
    XmlReader _XmlReader = XmlReader.Create(new StringReader(XamlString));
    AdContentRichTextBox.Document = XamlString;          
    var size = new Size(Width, Height);
    AdContentRichTextBox.Measure(size);
    AdContentRichTextBox.Arrange(new Rect(size));
    RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 300, 300, PixelFormats.Pbgra32);
    bmp.Render(AdContentRichTextBox);

    DrawingVisual _drawingVisual = new DrawingVisual();
    using (DrawingContext _drwaingContext = _drawingVisual.RenderOpen())
    {
        VisualBrush _visualBrush = new VisualBrush(AdContentRichTextBox);
    }
    PngBitmapEncoder _png = new PngBitmapEncoder();
    _png.Frames.Add(BitmapFrame.Create(bmp));
    System.Drawing.Bitmap _tempBitmap = null;
    using (Stream _fileStream = new MemoryStream())
    {
        _png.Save(_fileStream);
        _tempBitmap = new System.Drawing.Bitmap(_fileStream);
        _fileStream.Flush();
    }
    return _tempBitmap;
}

图像质量模糊

嗯。。这里可能有很多东西在相互作用:

第一个

灰度回退-如果ClearType被禁用,或者在某些情况下无法运行ClearType算法渲染文本,WPF将使用灰度渲染算法来消除渲染文本的失真。

将文本渲染到RenderTargetBitmap似乎就是其中一种情况。。。。(渲染器从硬件路径切换到软件路径)。

第二个

此外,NET 4将默认缩放算法从高质量(Fant)切换到低质量(双线性)。。。。。现在这不应该在这里发挥作用,因为它看起来不像是以任何方式缩放位图。。。但你永远不知道里面发生了什么。可以将缩放器切换回更高质量的缩放器。

  • http://www.olsonsoft.com/blogs/stefanolson/post/Workaround-for-low-quality-bitmap-resizing-in-WPF-4.aspx

第三个

您可能需要考虑RichTextBox的父容器。。。请参阅下面的最后一个链接,提到它会扭曲字体渲染。

  • 使用WPF将文本渲染为位图的问题

关于如何解决这一问题的一些想法是:

  1. 以更高的分辨率(例如600dpi)渲染RichTextBox,然后缩小位图(可能没有什么区别)

  2. 捕捉屏幕。。。。如果您的视觉在屏幕外/被遮挡等,则很难或不实用。


参见相关链接:

  • http://windowsclient.net/wpf/white-papers/wpftextclarity.aspx

  • WPF渲染目标位图缩小文本ClearType到GreyScale

  • WPF渲染目标位图缩小文本渲染模式到灰度

  • WPF文本呈现不一致