问题描述
我正在开发一个应用程序,使用WPF动态呈现内容,包括来自WPF的文本和图像转换为jpg文件。我正在使用 RenderTargetBitmap
类来完成这项工作。一切都按预期工作,但渲染字体的质量是可怕的。我知道 RenderTargetBitmap
不使用ClearType,而是使用GrayScale抗锯齿,这是一种小字体。但是我使用大于30分的大字体,结果是完全不能接受的。有这种问题的解决方法吗? [更新]
我使用的代码如下所示。如预期的那样,在CompositionTarget的每个Rendering事件上都会调用它。
void CompositionTarget_Rendering(object sender,EventArgs e)
{
prefix =;
if(counter {
prefix =000;
else if(counter< 100)
{
prefix =00;
else if(counter< 1000)
{
prefix =0;
}
Size size = new Size(MainCanvas.Width,MainCanvas.Height);
MainCanvas.Measure(size);
MainCanvas.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap(imgWidth,imgHeight,96d,96d,PixelFormats.Default);
bmp.Render(MainCanvas);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 90;
encoder.Frames.Add(BitmapFrame.Create(bmp));
string file = basePath + prefix + counter.ToString()+_testpic.jpg;
using(Stream stm = File.Create(file))
{
encoder.Save(stm);
}
counter ++;
$ b $ p
$ b 下面是一些结果图像的例子:
在此先感谢。
解决方案好的,我终于找到了一个解决方案。 Gustavo你在正确的轨道上。问题是,我试图呈现为位图的主容器被其父容器扭曲。解决方案是将主容器添加到画布上,但没有布局引擎来扭曲孩子。我仍然需要做更多的尝试,但看起来很有希望。显然RenderTargetBitmap根本不喜欢扭曲的字体。
I am developing an application using WPF to dynamically render content, including text and images from WPF into jpg files. I am currently using the RenderTargetBitmap
class to do the job. Everything works as expected but the quality of the rendered fonts is terrible. I understand that the RenderTargetBitmap
doesn’t use the ClearType but a GrayScale antialias, which is kind of blury with small fonts. But I am using large fonts, larger than 30 pts, and the results are totally unacceptable. Is there some kind of workaround for this issue?
[Update]
The Code I am using is listed below. As expected it is called on each Rendering event of the CompositionTarget.
void CompositionTarget_Rendering(object sender, EventArgs e)
{
prefix = "";
if (counter < 10)
{
prefix = "000";
}
else if (counter < 100)
{
prefix = "00";
}
else if (counter < 1000)
{
prefix = "0";
}
Size size = new Size(MainCanvas.Width, MainCanvas.Height);
MainCanvas.Measure(size);
MainCanvas.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap(imgWidth, imgHeight, 96d, 96d, PixelFormats.Default);
bmp.Render(MainCanvas);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 90;
encoder.Frames.Add(BitmapFrame.Create(bmp));
string file = basePath + prefix + counter.ToString() + "_testpic.jpg";
using (Stream stm = File.Create(file))
{
encoder.Save(stm);
}
counter++;
}
Here are some examples of the resulting images:alt text http://www.randomnoise.org/temp/testpic_v1.jpgalt text http://www.randomnoise.org/temp/testpic_v2.jpg
Thanks in advance.
解决方案 Ok, I finally found a solution. Gustavo you were on the right track. The problem was that the main container that I was trying to render as bitmap was being distorted by its parent container. The solution was to add the main container to a canvas, that doesn't have a layout engine that distorts its children. I still need to do some more experimenting but it looks very promising. Apparently RenderTargetBitmap doesn't like distorted fonts at all.
这篇关于将文本呈现为使用WPF的位图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!