我目前有一个可写位图图像和带有绘图的画布,我想将图像发送给对等方。为了减少带宽,我想将画布转换为可写位图,因此我可以将两个图像都blit到一个新的可写位图。问题是我找不到转换画布的好方法。所以想问问有没有直接的办法可以把canvas转成writeablebitmap类。
问问题
4788 次
1 回答
5
这取自这篇博文,但不是写入文件,而是写入 WriteableBitmap。
public WriteableBitmap SaveAsWriteableBitmap(Canvas surface)
{
if (surface == null) return null;
// Save current canvas transform
Transform transform = surface.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
surface.LayoutTransform = null;
// Get the size of canvas
Size size = new Size(surface.ActualWidth, surface.ActualHeight);
// Measure and arrange the surface
// VERY IMPORTANT
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
96d,
96d,
PixelFormats.Pbgra32);
renderBitmap.Render(surface);
//Restore previously saved layout
surface.LayoutTransform = transform;
//create and return a new WriteableBitmap using the RenderTargetBitmap
return new WriteableBitmap(renderBitmap);
}
于 2013-04-17T02:14:58.597 回答