0

我有要求創建尺寸爲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); 
+0

有你嘗試使用RenderOptions? –

+0

感謝回覆@ d.moncada。我相信我通過使用我自己的DrawingVisual的子實現來嘗試基本相同的事情,該實現設置了像上述VisualBitmapScalingMode,VisualTextRenderingMode,VisualEdgeMode這樣的受保護屬性。也就是說,我只是嘗試了'RenderOptions.SetBitmapScalingMode(visual,BitmapScalingMode.HighQuality);'沒有運氣。你認爲我應該嘗試的任何特定選項(組合)? – rossbeehler

回答