简体   繁体   English

如何将canvas内容用作位图?

[英]How to use canvas content as a bitmap?

I'm writing a paint application in WPF, and when I want make color picker or fill tool i get a problem. 我正在WPF中编写一个绘图应用程序,当我想制作颜色选择器或填充工具时,我遇到了问题。 I dont know how to use canvas content as bitmap, the only one solution I invent is save it, and open as bitmap then I can easy operate on pixels to get and sets pixels, but I want do it in another way. 我不知道如何使用画布内容作为位图,我发明的唯一解决方案是保存它,然后打开作为位图然后我可以轻松操作像素来获取和设置像素,但我想以另一种方式做到这一点。 Any suggestions? 有什么建议么?

If you're looking for converting your canvas UIElement to a BitmapSource so that you can manipulate it then you can take a look at this post on MSDN MSDN . 如果您正在寻找将画布UIElement转换为BitmapSource以便您可以操作它,那么您可以在MSDN MSDN上查看此帖子。

Here is the helper function to get the BitmapSource from your canvas: 以下是从画布中获取BitmapSource的辅助函数:

public static BitmapSource CreateBitmapSourceFromVisual(
    Double width,
    Double height,
    Visual visualToRender,
    Boolean undoTransformation)
{
    if (visualToRender == null)
    {
        return null;
    }
    RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width),                                                        (Int32)Math.Ceiling(height), 96,96, PixelFormats.Pbgra32);

    if (undoTransformation)
    {
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext dc = dv.RenderOpen())
        {
            VisualBrush vb = new VisualBrush(visualToRender);
            dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
        }
        bmp.Render(dv);
    }
    else
    {
        bmp.Render(visualToRender);
    }
  return bmp;
}

I didn't get what do want to say by a color picker or fill tool problem, so I can't help you with that. 我没有得到颜色选择器或填充工具问题想要说什么,所以我无法帮助你。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM