我有自定义Canvas(WPF)面板。从后面的代码我需要使用WriteableBitmap
(或WriteableBitmapEx
)呈现文本。
我该怎么做?
答案 0 :(得分:0)
使用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);