0

我需要创建 600 个 DPI “三折”图像,其尺寸为 25.5"x11"(是 Letter 页面大小的三倍)。为此,我通过 DrawingVisual、DrawingContext 和 RenderTargetBitmap 类使用 WPF Imaging。

当我以较低的分辨率(例如 400 DPI 或更低)生成图像时,所有文本都按预期显示在正确的位置。但是,一旦我将图像分辨率提高到 500 DPI 级别或更高,位于图像最右侧的某些文本将简单地消失,而其他相对定位的文本/形状则可以完美打印。最疯狂的部分是,当我尝试不同的 DPI 时,会出现/消失不同的文本。在一个测试案例中,600 DPI 缺少一组绘制的 FormattedText,650 DPI 缺少另一组绘制的 FormattedText,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);
4

1 回答 1

1

此错误的解决方法是将字体大小四舍五入到小数点后 2 位:

Math.Round(8.0*DeviceIndependentUnits/TypographicUnits, 2),

这和一些额外的信息可以在匹配的 MSDN 帖子中找到: http: //social.msdn.microsoft.com/Forums/en-US/98717e53-89f7-4d5f-823b-7184781a7b85/wpf-formattedtext-randomly-disappears-高分辨率图像

于 2014-02-25T15:15:31.663 回答