Get images from DrawingGroup

2020-07-22 04:24发布

Maybe it's a stupid question, but I have some problems with finding the proper answer:S

How to get frames as Bitmap's or Image's (or something similar) from DrawingGroup? I don't actually know how to bite it. I tried to look for it in the Internet, but had problems with finding something useful.

1条回答
Fickle 薄情
2楼-- · 2020-07-22 05:07

If you need an image to be used as the Source of an Image control, you could simply put the drawing into a DrawingImage:

var drawing = ...
var drawingImage = new DrawingImage(drawing);
image.Source = drawingImage;

If the question is about creating a BitmapSource (i.e. something that can be encoded by a BitmapEncoder via a BitmapFrame), there is no direct conversion. You have to put the image into an intermediate Image control and render that control into a RenderTargetBitmap, which is a BitmapSource:

var drawing = ...
var drawingImage = new DrawingImage(drawing);
var image = new Image { Source = drawingImage };
var bitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);
image.Arrange(new Rect(0, 0, bitmap.Width, bitmap.Height));
bitmap.Render(image);
查看更多
登录 后发表回答