将画布转换为ImageSource

本文关键字:ImageSource 转换 布转换 | 更新日期: 2023-09-27 18:27:20

im试图将画布转换为图像源以用作OpacityMask,我想将其保存到内存中,而不是将其保存为文件,但我遇到了麻烦。下面是我的代码,我想我做错了!

真的,我需要获得Base64String的图像信息,所以我需要转换RenderTargetBitmap

public BitmapSource ExportToPng(Uri path, Canvas surface)
{
    BitmapEncoder encoder = new PngBitmapEncoder();
    System.IO.MemoryStream myStream = new System.IO.MemoryStream();
    // 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
    System.Windows.Size size = new System.Windows.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);
    // push the rendered bitmap to it
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    // save the data to the stream
    encoder.Save(myStream);
    // Restore previously saved layout
    surface.LayoutTransform = transform;
    var sr = new System.IO.StreamReader(myStream);
    var myStr = sr.ReadToEnd();
    var bytes = Convert.FromBase64String(myStr);
    // Save to memory
    /*Bitmap pg = new Bitmap("525, 350");
    Graphics gr = Graphics.FromImage(pg);
    gr.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255)), 0, 0, (float)size.Width, (float)size.Height);
    gr.DrawImage(System.Drawing.Bitmap.FromStream(myStream), 0, 0);*/

    return BitmapFromBase64(myStr);
}
public static BitmapSource BitmapFromBase64(string base64String)
{
    var bytes = Convert.FromBase64String(base64String);
    using (var stream = new System.IO.MemoryStream(bytes))
    {
        return BitmapFrame.Create(stream,
            BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}

编辑:

刚刚找到了另一种可能的方法,然而这会创建一个DrawingVisual,我需要将其转换为ImageBrush

C#

// Create a DrawingVisual that contains a rectangle.
        private DrawingVisual CreateDrawingVisualRectangle(List<Rectangle> rectangles)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            // Retrieve the DrawingContext in order to create new drawing content.
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            // Create a rectangle and draw it in the DrawingContext.
            foreach(Rectangle x in rectangles)
            {
                Rect rect = new Rect(new System.Windows.Point(x.X, x.Y), new System.Windows.Size(x.Width, x.Height));
                drawingContext.DrawRectangle(System.Windows.Media.Brushes.Black, (System.Windows.Media.Pen)null, rect);
            }
        // Persist the drawing content.
        drawingContext.Close();
        return drawingVisual;
    }

将画布转换为ImageSource

UIElement将任何Brush作为OpacityMask。您可以简单地从Canvas创建一个VisualBrush,因为每个UIElement的基类都是SWM.Visual.

Canvas c = new Canvas();
element.OpacityMask = new VisualBrush( c );

问候,雪球