简体   繁体   English

如何将画布上绘制的椭圆保存为图像-Windows RT

[英]How to save drawn ellipse over canvas as an image - Windows RT

I've drawn ellipse over canvas, now how can I save it as an image. 我已经在画布上绘制了椭圆,现在如何将其另存为图像。 I know you cannot directly save canvas as an image and neither you can take screenshot. 我知道您不能直接将画布另存为图像,也不能截图。 I am working in C#/xaml. 我正在使用C#/ xaml。 Below is my code for drawing ellipse over canvas. 下面是我在画布上绘制椭圆的代码。

private void canvasDraw_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        if (drawing)
        {
            PointerPoint current = e.GetCurrentPoint((UIElement)sender);
           // Line line = new Line() { X1 = start.Position.X, Y1 = start.Position.Y, X2 = current.Position.X, Y2 = current.Position.Y };
            //line.Stroke = new SolidColorBrush(Colors.Black);
            Ellipse circle = new Ellipse();
            circle.SetValue(Canvas.LeftProperty, current.Position.X);
            circle.SetValue(Canvas.TopProperty, current.Position.Y);
            circle.Height = 20;
            circle.Width = 20;
            circle.Fill = currentBrush;
            circle.Opacity = 0.7;
            circle.SetValue(Canvas.ZIndexProperty,1);
            canvasDraw.Children.Add(circle);

        }
    }

Edit : I am able to save the image by using InkManager. 编辑:我可以使用InkManager保存图像。 I stored every Ellipse in inkmanager and called SaveAsync method but the final issue is the image comes in black for example if I draw red ellipse the saved image has black ellipse. 我将每个椭圆存储在inkmanager中,并调用SaveAsync方法,但是最后一个问题是图像为黑色,例如,如果我绘制红色椭圆,则保存的图像为黑色椭圆。

Read this example http://blogs.msdn.com/b/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx 阅读此示例http://blogs.msdn.com/b/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx

Code Extract from site: 来自站点的代码摘录:

// render InkCanvas' visual tree to the RenderTargetBitmap
RenderTargetBitmap targetBitmap =
    new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
                           (int)inkCanvas1.ActualHeight,
                           96d, 96d,
                           PixelFormats.Default);
targetBitmap.Render(inkCanvas1);

// add the RenderTargetBitmap to a Bitmapencoder
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));

// save file to disk
FileStream fs = File.Open(fileName, FileMode.OpenOrCreate);
encoder.Save(fs);

Yes RenderTargetBitmap it is not available in windows store app target to 8.0 RenderTargetBitmap ,在Windows商店应用目标8.0中不可用

Windows 8.1 APIs include the new RenderTargetBitmap class that allows to render to a bitmap with its RenderAsync methods. Windows 8.1 API包括新的RenderTargetBitmap类,该类允许使用其RenderAsync方法呈现为位图。 you can now use this method link although its not working with MediaElement which i need :( 您现在可以使用此方法链接,尽管它不适用于我需要的MediaElement :(

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

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