将绘图图像转换为位图图像 [英] Convert DrawingImage to BitmapImage

查看:51
本文介绍了将绘图图像转换为位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制大量形状(线条、椭圆和...),然后将它们保存为位图或 png.我画了图,问题是:如何在 C# 中将 DrawingImage 转换为 BitmapImage?代码是这样的:

I want to draw large number of shapes (lines, ellipses and ...) and then save them as bitmap or png. I made the drawings and the question is: how can I convert a DrawingImage to BitmapImage in C#? the code is something like this:

DrawingGroup drawingGroup = new DrawingGroup();
using(DrawingContext context = drawingGroup.Open())
{
    //make some drawing 
}
DrawingImage drawingImage = new DrawingImage(drawingGroup)

// your suggestion? DrawingImage - > BitmapImage

推荐答案

您可以将 ImageDrawing 放入 Image 控件中,然后将其渲染到 RenderTargetBitmap,它是一个 BitmapSource,因此可以通过 BitmapEncoder(本例中为 PngBitmapEncoder).

You may put the ImageDrawing into an Image control and render that into a RenderTargetBitmap, which is a BitmapSource and can therefore be serialized by a BitmapEncoder (PngBitmapEncoder in this example).

public void SaveDrawingToFile(Drawing drawing, string fileName, double scale)
{
    var drawingImage = new Image { Source = new DrawingImage(drawing) };
    var width = drawing.Bounds.Width * scale;
    var height = drawing.Bounds.Height * scale;
    drawingImage.Arrange(new Rect(0, 0, width, height));

    var bitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(drawingImage);

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (var stream = new FileStream(fileName, FileMode.Create))
    {
        encoder.Save(stream);
    }
}

请注意,您实际上并不需要 BitmapImage 进行编码,因为 BitmapSource(或任何派生类,如 RenderTargetBitmap)将被接受为 BitmapFrame.Create.

Note that you don't actually need a BitmapImage for encoding, because BitmapSource (or any derived class like RenderTargetBitmap) will be accepted as argument to BitmapFrame.Create.

稍微不同的解决方案将涉及 DrawingVisual 而不是 DrawingImage:

A slightly different solution would involve a DrawingVisual instead of a DrawingImage:

public void SaveDrawingToFile(Drawing drawing, string fileName, double scale)
{
    var drawingVisual = new DrawingVisual();

    using (var drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.PushTransform(new ScaleTransform(scale, scale));
        drawingContext.PushTransform(new TranslateTransform(-drawing.Bounds.X, -drawing.Bounds.Y));
        drawingContext.DrawDrawing(drawing);
    }

    var width = drawing.Bounds.Width * scale;
    var height = drawing.Bounds.Height * scale;
    var bitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(drawingVisual);

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (var stream = new FileStream(fileName, FileMode.Create))
    {
        encoder.Save(stream);
    }
}

这篇关于将绘图图像转换为位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆