有没有办法为WriteableBitmap获取一个DrawingContext (或类似的东西)?例如,允许您调用简单的DrawLine/DrawRectangle/etc类型的方法,而不是直接操作原始像素。
发布于 2008-09-22 20:09:47
它看起来是the word is no。
为了便于将来参考,我们计划将Writeable Bitmap Extensions的一个端口用于WPF。
对于使用纯现有代码的解决方案,下面提到的任何其他建议都可以。
发布于 2009-05-15 16:54:58
我发现六个字母变量的解决方案是最可行的。但是,这里缺少一个"drawingContext.Close()“。根据MSDN的说法,“在呈现DrawingContext内容之前,必须先关闭它”。结果是以下实用函数:
public static BitmapSource CreateBitmap(
int width, int height, double dpi, Action<DrawingContext> render)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
render(drawingContext);
}
RenderTargetBitmap bitmap = new RenderTargetBitmap(
width, height, dpi, dpi, PixelFormats.Default);
bitmap.Render(drawingVisual);
return bitmap;
}然后,可以像这样轻松地使用它:
BitmapSource image = ImageTools.CreateBitmap(
320, 240, 96,
drawingContext =>
{
drawingContext.DrawRectangle(
Brushes.Green, null, new Rect(50, 50, 200, 100));
drawingContext.DrawLine(
new Pen(Brushes.White, 2), new Point(0, 0), new Point(320, 240));
});发布于 2009-04-28 12:00:35
如果你不介意使用System.Drawing,你可以这样做:
var wb = new WriteableBitmap( width, height, dpi, dpi,
PixelFormats.Pbgra32, null );
wb.Lock();
var bmp = new System.Drawing.Bitmap( wb.PixelWidth, wb.PixelHeight,
wb.BackBufferStride,
PixelFormat.Format32bppPArgb,
wb.BackBuffer );
Graphics g = System.Drawing.Graphics.FromImage( bmp ); // Good old Graphics
g.DrawLine( ... ); // etc...
// ...and finally:
g.Dispose();
bmp.Dispose();
wb.AddDirtyRect( ... );
wb.Unlock(); https://stackoverflow.com/questions/88488
复制相似问题