簡體   English   中英

在畫布上使用WriteableBitmap渲染文本

[英]Render text with WriteableBitmap on Canvas

我有自定義的畫布(WPF)面板。 從后面的代碼中,我需要使用WriteableBitmap (或WriteableBitmapEx )呈現文本。

我怎樣才能做到這一點?

使用RenderTargetBitmap對象的Render方法有效地捕獲Canvas作為內存流-然后從中創建一個位圖:

var targetBitmap = new RenderTargetBitmap((int)yourCanvas.ActualWidth, (int)yourCanvas.ActualHeight, 96d, 96d, System.Windows.Media.PixelFormats.Default);

targetBitmap.Render(yourCanvas);

// add the RenderTargetBitmap to a Bitmap encoder
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));

// create the memory stream
var memoryStream = new MemoryStream();
encoder.Save(memoryStream);

// create the bitmap
var controlBitmap = new System.Drawing.Bitmap(memoryStream);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM