Question

I have the requirement to create 600 DPI "trifold" images, which have the dimensions of 25.5"x11" (three times the size of a Letter page). To do so, I’m using WPF Imaging through the DrawingVisual, DrawingContext, and RenderTargetBitmap classes.

When I generate the image in lower resolutions, 400 DPI or less for example, all of the text displays in the correct positions as expected. However, once I increase my image resolution to the 500 DPI level and beyond, certain text positioned in the far right of the image will simply disappear, while other relatively positioned text/shapes print perfectly. The craziest part about it is that as I try varying DPIs, different text will appear/disappear. In one test case, 600 DPI is missing one set of drawn FormattedTexts, 650 DPI is missing a different set of drawn FormattedTexts, and 700 DPI prints everything fine!

I’ve recreated the issue with the snippet of code below. Run as-is (600 DPI) and all you get is a very large white image. Change the Dpi constant to 400 or lower, and it prints the text just fine.

Note that I’ve tried turning many of the knobs within the DrawingVisual class (e.g. VisualBitmapScalingMode, VisualTextRenderingMode, VisualEdgeMode, etc.) to no avail. Most of my research on those settings found them as useful settings to correct “fuzzy” text, not disappearing text. I’ve also had no luck with any of the guideline/snap settings of the DrawingVisual or DrawingContext.

Note that I’ve recreated this issue on both Win7 and Win2008R2, and my application is running .NET 4.5.

Any ideas?

        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);
Was it helpful?

Solution

The workaround to this bug is to round the font size to 2 decimal positions:

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

This and some extra information can be found in the matching MSDN post: http://social.msdn.microsoft.com/Forums/en-US/98717e53-89f7-4d5f-823b-7184781a7b85/wpf-formattedtext-randomly-disappears-in-high-resolution-images

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top