我有要求创建尺寸为25.5“x11”(Letter Letter页面大小的三倍)的600 DPI“trifold”图像。为此,我通过DrawingVisual,DrawingContext和RenderTargetBitmap类使用WPF Imaging。WPF FormattedText在高分辨率图像中随机消失
当我以较低的分辨率生成图像时,例如400 DPI或更少,所有文本均按预期显示在正确的位置。但是,一旦将图像分辨率提高到500 DPI及以上级别,位于图像最右侧的某些文本将会简单地消失,而其他相对定位的文本/图形完美打印。最疯狂的部分是,当我尝试改变DPI时,不同的文字会出现/消失。在一个测试案例中,600 DPI缺少一组绘制的格式化文本,650 DPI缺少一个不同一组绘制的FormattedTexts,以及700 DPI印刷一切正常!
我用下面的代码片段重新创建了该问题。按原样运行(600 DPI),所有你得到的是一个非常大的白色图像。将Dpi常数改为400或更低,并且打印文本就好了。
请注意,我已经尝试在DrawingVisual类(例如VisualBitmapScalingMode,VisualTextRenderingMode,VisualEdgeMode等)内旋转许多旋钮,但无济于事。我对这些设置的大部分研究都发现它们是有用的设置,可以纠正“模糊”文本,而不会消失文本。我也没有运用任何DrawingVisual或DrawingContext的指引/捕捉设置。
请注意,我已经在Win7和Win2008R2上重新创建了这个问题,并且我的应用程序正在运行.NET 4.5。
任何想法?
const double ImageWidthInches = 25.5;
const double ImageHeightInches = 11.0;
const double Dpi = 600.0;
const double DeviceIndependentUnits = 96.0;
const double TypographicUnits = 72.0;
var visual = new DrawingVisual();
var drawing = visual.RenderOpen();
drawing.DrawRectangle(
Brushes.White,
null,
new Rect(0,
0,
ImageWidthInches*DeviceIndependentUnits,
ImageHeightInches*DeviceIndependentUnits));
var formattedText = new FormattedText(
"Why doesn't this display?",
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Arial Narrow"),
FontStyles.Normal,
FontWeights.Normal,
FontStretches.Normal),
8.0*DeviceIndependentUnits/TypographicUnits,
Brushes.Black);
drawing.DrawText(formattedText,
new Point(23.39*DeviceIndependentUnits,
2.6635416666666671*DeviceIndependentUnits));
drawing.Close();
var renderTarget = new RenderTargetBitmap(
(int) (ImageWidthInches*Dpi),
(int) (ImageHeightInches*Dpi),
Dpi,
Dpi,
PixelFormats.Default);
renderTarget.Render(visual);
var tiffEncoder = new TiffBitmapEncoder {Compression = TiffCompressOption.Ccitt4};
tiffEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (var fileStream = new FileStream(@"c:\recreateWpfTextIssue.tif", FileMode.Create, FileAccess.Write))
tiffEncoder.Save(fileStream);
有你尝试使用RenderOptions? –
感谢回复@ d.moncada。我相信我通过使用我自己的DrawingVisual的子实现来尝试基本相同的事情,该实现设置了像上述VisualBitmapScalingMode,VisualTextRenderingMode,VisualEdgeMode这样的受保护属性。也就是说,我只是尝试了'RenderOptions.SetBitmapScalingMode(visual,BitmapScalingMode.HighQuality);'没有运气。你认为我应该尝试的任何特定选项(组合)? – rossbeehler