2

I am developing a paint like application in metro style with C#/XAML. I want to save whatever content is drawn on canvas as image. I have checked this solutions but it didn't helped me.

Save canvas to bitmap http://blogs.msdn.com/b/saveenr/archive/2008/09/18/wpf-xaml-saving-a-window-or-canvas-as-a-png-bitmap.aspx

So how can I save the canvas content as image ? Please help me with sample coding.

Community
  • 1
  • 1
Xyroid
  • 463
  • 5
  • 19

4 Answers4

1

The accepted answer from Sascha is no longer true for Windows 8.1. There is a new RenderTargetBitmap class that allows Rendered XAML to be converted to a bitmap.

See: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.rendertargetbitmap.aspx

However I can't seem to render that XAML is Collapsed or otherwise not currently on screen. Any help with that would be appreciated!

satur9nine
  • 13,927
  • 5
  • 80
  • 123
  • For Windows 8 my answer still stands true. For Windows 8.1 new APIs are introduced and RenderTargetBitmap is one of the new ones. It will be available for Windows 8.1 and most likely newer versions. The function is explicitly designed to copy visible content (XAML visual tree) to the bitmap. You might think of tricks (those tricks might not be good practice) like adding an element to coordinates not visible on the screen (but would if your screen would be large enough) and render that UIElement. Not tested, but might be an idea. Consider making this a new question (as you're targeting 8.1) – Sascha Aug 25 '13 at 15:18
  • Thanks I added a new question here: http://stackoverflow.com/questions/18419810/in-winrt-c-sharp-how-do-i-save-an-offscreen-xaml-tree-using-rendertargetbitmap – satur9nine Aug 25 '13 at 18:58
0

The standard approach in other xaml frameworks such as Silverlight and WP7 was to use WriteableBitmap, however the version in WinRT doesn't have a constructor that takes a xaml element which leads me to believe that it's currently impossible against the latest API.

Sorry I couldn't be more help/

Nigel Sampson
  • 10,549
  • 1
  • 28
  • 31
  • Thanks Nigel, I came across [WriteableBitmapEx](http://writeablebitmapex.codeplex.com/) How can I implement it ? Can you just give me any demo for that ? – Xyroid Jun 29 '12 at 04:59
  • WriteableBitmapEx won't give you a way to render text to bitmap. – Filip Skakun Jun 29 '12 at 06:33
0

One workaround is to use Direct2D with DirectWrite. It is a bit involved, but I might make a library available at some point.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Actually I am developing with C#/XAML. Can you please provide me any sample/example/code snippet for Direct2D with DirectWrite – Xyroid Jun 29 '12 at 07:08
  • I haven't written it yet, but I believe the Metro samples from Microsoft include code to render text with Direct2D and save it to an image file. I'll let you know when I have some sort of library available. It might take a few days or weeks though. – Filip Skakun Jun 29 '12 at 07:37
0

It seems it is currently not supported out of the box like we were used to do it with WPF:

http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/dd66c749-efed-4b55-a716-e0aa3a637d28

While when doing HTML5/Javascript Metro apps you can do this:

http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/cf8c17dc-68d4-4777-951f-bb7f0665bd06

Sascha
  • 10,231
  • 4
  • 41
  • 65