Saving a Canvas into a PNG file – WP8

This is a familiar situation that most developers run into. The need to save a canvas element to a png file.

Scenario

  • You have few strokes drawn on a canvas and you want to save it as a png .
  • You want to save the just the strokes on a transparent background.
  • You want to save the strokes along with the canvas background.
  • You want to save it with your specified sizes

I too ran into this ditch lately. And had a tough time finding a solution. A random google search will give you numerous methods, most of which is applicable only to traditional C#/.NET application.

Then I found this MSDN link. I still have’nt figured out how to use a PNGEncoder and DeCoder. If anyone finds a good blog about it, feel free to post it here. Many of the solutions I found never worked. The searches how ever led me to the WriteableBitmapEx hosted at codeplex. It had several useful functions but none to save a canvas to an image.

A week’s search, frustration, irritation,  trial & error of several broken classes finally led me to ToolStack. It is mentioned that it works for Windows Phone 7.1 but I have implemented in WP8 project. There 3 classes which will do the job neatly, although compression is not yet implemented.

  1. ToolStackPNGWriterWBext.cs – Implementation of WriteableBitmapExtensions
  2. ToolStackPNGWriterLib.cs – Core Class and implementation of saving to PNG
  3. ToolStackCRCLib.cs – Additional classes needed for ToolStackPNGWriterLib.

Getting it working on your project.

  1.  Download all necessary files from ToolStack. Download the example too and run it first so that you can understand how it works.
  2.  Add the Classes mentioned to your project.
  3. Create a Writeablebitmap of your canvas and then use the WritePNG function to save
    1
    2
    3
    4
    </pre>
    WriteableBitmap wb = new WriteableBitmap(MyCanvas, null );
    wb.WritePNG(MyStreamforWriteAsync);
    <pre>

And there your image lies in the specified location, in the size of your canvas! :)  Remember to add the necessary using statements in your file. Any canvas element can be captured into a WritableBitmap, even an InkPresenter can be captured.

NOTE TO WIN8 DEVELOPERS

If you try to implement the same functionality in a metro app, you’re in for trouble mate. Because the very simple function which allows to capture a canvas onto a WriteableBitmap does not exist! There is a method to save the inkstrokes though, but only the portion containing the stroke will be saved. You cannot save it along with its transparent/colored background. I consulted few developers and they seem to have no answer too. It seems this is possible in JavaScript.

Lets see if Windows 8.1 contains something for us developers in this direction.

Happy Coding!! :D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值