简体   繁体   中英

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. 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. 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.

Read this example 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

Windows 8.1 APIs include the new RenderTargetBitmap class that allows to render to a bitmap with its RenderAsync methods. you can now use this method link although its not working with MediaElement which i need :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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