在 WPF 4.0 中:如何渲染到(PNG)位圖和窗口一樣好? (In WPF 4.0: How to render to a (PNG) bitmap as good as to the window?)


問題描述

在 WPF 4.0 中:如何渲染到(PNG)位圖和窗口一樣好? (In WPF 4.0: How to render to a (PNG) bitmap as good as to the window?)

I'm exporting my WPF (4.0) visuals (vector diagrams) to some image formats in the next way...

    public void ExportImageTo(BitmapEncoder Encoder, Stream ExportStream, Visual SourceVisual, int Width, int Height)
    {
        var Result = new RenderTargetBitmap(Width, Height, WPF_DPI, WPF_DPI, PixelFormats.Default);
        Result.Render(Source);

        Encoder.Frames.Add(BitmapFrame.Create(Result));
        Encoder.Save(ExportStream);

        ExportStream.Flush();
        ExportStream.Close();
    }

The problem is that even using a PngBitmapEncoder (loseless algorithm) it still generates some little pixels that differs from those in the screen.

I think it is related with the way WPF renders text or deals with anti‑aliasing and for which exists properties like UseLayoutRounding, SnapsToDevicePixels and BitmapScalingMode (note: I'm not using those properties on my code).

So, what can I do to make my exported visuals look like those rendered on screen? Thanks!

‑‑‑‑‑

參考解法

方法 1:

Apparently WPF doesn't render ClearType text if it's targeting a transparent background (google this: RenderOptions.ClearTypeHint). I wonder if you would have better success with a 24bit image format (no alpha channel). Also, Width/Height are not the same as ActualWidth/ActualHeight.

(by Néstor Sánchez A.Brannon)

參考文件

  1. In WPF 4.0: How to render to a (PNG) bitmap as good as to the window? (CC BY‑SA 3.0/4.0)

#bitmap #wpf-4.0 #wpf #.net






相關問題

在 WPF 4.0 中:如何渲染到(PNG)位圖和窗口一樣好? (In WPF 4.0: How to render to a (PNG) bitmap as good as to the window?)

Android 使用手勢移動和縮放 (Android using hand gestures to move and zoom)

Android轉換為位圖崩潰 (Android Converting to bitmap crash)

本機堆不斷增加 (Native heap keeps increasing)

如何為自定義 NSImageRep 子類實現 -draw (How to implement -draw for custom NSImageRep subclass)

為什麼 getBitmap 方法不起作用? (Why is getBitmap method not working?)

這個 3x3 均值過濾器我做錯了什麼? (What am I doing wrong with this 3x3 Mean filter?)

用於顯示位圖和處理按鈕按下的簡單框架 (Simple Frameworks for Displaying Bitmaps and Handling Button Presses)

如何在Android中位圖壓縮後保存Exif數據 (How to save Exif data after bitmap coppression in Android)

運行幾幀後位圖動畫停止工作 (Bitmap animation stops working, after running a few frames)

Silverlight PrtScr 一些控件 (Silverlight PrtScr some controls)

如何使用 PdfiumViewer 將 PDF 轉換為位圖圖像? (How to convert a PDF to a Bitmap image using PdfiumViewer?)







留言討論