Skip to main content
Microsoft Silverlight
Home Forums General Silverlight New Features in Silverlight 3 Bitmap API.... Export Canvas to JPEG
23 replies. Latest Post by avbersSL on July 15, 2009.
(0)
brauliod
Participant
1307 points
502 Posts
07-02-2009 3:42 PM |
Hi,
One missing functionallity on SL2 was the limitation of not being able to export a canvas content as a bitmpa (jpeg, ...).
I was hoping that this functionallity was added into SL3... I have been checking the BITMAP API but it seems that is only worthy to draw pixel by pixel.
Is there anyway on SL3 to export a canvas content as a bitmap? Or do we still have to go to the server side and replicate the code using GDI+?
Thanks
Braulio
ksleung
Contributor
5830 points
1,102 Posts
07-02-2009 3:50 PM |
Wait for one more week and hopefully there is a clear resolution to this problem. In SL3 beta, you can "Render" any control to a WriteableBitmap, but such bitmap can only be used as an image source due to security restriction. In SL3 RTW the security restriction should be relaxed, for example, allowing the WriteableBitmap to be readable if the control of origin is not derived from any cross-domain images (this is my interpretation, exact criteria has only been hinted at, not officially announced). Assuming that your to-be-rendered control passes the security restriction, you can then convert the WriteableBitmap to Jpeg, Bmp, or Png with the use of third-party image encoding libraries.
07-02-2009 4:02 PM |
Thanks that would be great.
Just for other folks, about exporting canvas to image:
http://www.wintellect.com/CS/blogs/jprosise/archive/2009/03/25/more-on-silverlight-3-s-new-writeable-bitmap.aspx
Then if ksleung is right on RTW we will be able to export this bitmap as JPEG, that would be great and quite powerful... any diagram based applicaiton could have a powerfull Export as Image feature.
07-10-2009 7:21 PM |
Hello,
Now that SL3 RTW has been release, any update on this?
Sorry trying to find any export to jpeg option but seems not be straight forward :).
thanks
07-10-2009 7:29 PM |
Oops... already answered by your side :
http://silverlight.net/forums/p/108255/245487.aspx
07-10-2009 7:32 PM |
As I mentioned before, now you can do it (I confirmed that the WriteableBitmap security issue is fixed in SL3 RTW), but as expected the solution is not native (and don't expect that to happen in SL4).
Andy Beaulieu has a great writeup on this.
http://www.andybeaulieu.com/Home/tabid/67/EntryID/161/Default.aspx
His approach, however, only convert the WriteableBitmap to an uncompressed Png file, but he explained how to do compressed Png using SharpZipLib (and I'd further add that there are other alternatives if GPL is not acceptable to your use). If you want Jpg you need to install FJCore and use its Encode() function.
I don't need it at this moment but when I do, I am willing to share the code. But really, besides from adding FJCore to your project, it's only a few lines of code of API call and you'll get your Jpg stream :D
07-10-2009 7:40 PM |
Thanks Hardy,
Sorry but the link didn't work. (the link works :)).
Will check, I don't needed just right now, but I think will need it soon, exporting to JPEG on the client side is a luxury (what about a Silverlight PhotoShop implementation ;-)).
07-10-2009 7:43 PM |
I just fixed the link. An extra white space in the URL :D
TomGiam
852 points
246 Posts
07-10-2009 11:04 PM |
I'm getting this error message;
WriteableBitmap has protected content. Pixel access is not allowed.
When I try that code.
Tom
07-10-2009 11:25 PM |
Ah, I think you hit the security criteria of WriteableBitmap :D
I haven't found any documentation on this yet, but most likely the security criteria is that you can only render "Content" from resources with the right cross domain privilege. If you just get an image from random website, you are likely to hit this problem. If the image is from a site that you have cross domain rights, then it shouldn't complain.
07-11-2009 5:06 AM |
Ok,
So images from your domain, or whatever canvas you have generated no prob to use WritableBitmap.
What about images that you upload using the load file dialog? (load the image from the local computer).
I don't understand the copyright thing... if you can do it with a desktop app (copy whatever image and paste it, modify it at your own risk), why you cannot do that with a SL app? what does it mean (c) exactly? I mean the SL app detects that you have downloaded the app from a cross domain and the applies the (c) checking? Or is a watermark that the file contains?
And... I guess is possible to use the code you indicated to save a writableBitmap as png and then use the Save As File Dialog to save the generated image locally?
Sorry lot of doubts :-)
07-11-2009 8:06 AM |
I got it to work.
My problem was that since I was running my application from my local machine (VS2008 debug mode) the files from my own real web server were causing problems.
When I deployed the application on my web server and launched the app from a browser, the same image files are now OK.
The problem now is that images from Flickr and PhotoBucket don't work, but that's too restrictive. If I can load those images into my SL application why can't I access the pixels so that I can print or save them?
Is there a way around this? Will there be a way to do this in the future?
07-11-2009 1:47 PM |
I think web images (http://...jpg) are not subject to cross domain policy if they are directly used as the URI source. However, if you want to load the bits using WebClient, you will run into cross domain issues. I think some of the Flickr and Photobucket images, not all, have the free-for-all policies. It depends on how they are hosted, I guess.
The workaround is to use a proxy server to load the images into a byte stream, then create the image from the byte stream. Since the origin is the byte stream, there is no security issue :D
07-11-2009 2:38 PM |
I use web images as the imagesource of an imagebrush like this:
<Rectangle Tag="Photo" Stretch="Fill"> <Rectangle.Fill> <ImageBrush ImageSource="http://farm4.static.flickr.com/3054/3058233611_c9d128883e.jpg" /> </Rectangle.Fill> </Rectangle>
and it fails.
I'm not sure how ro turn that into a byte stream.
I may have to copy the images to a temporary folder on my web server and change the URL of the image, or just not support any images outside of my domain for now, which is a real pity.
07-11-2009 2:45 PM |
Another option worth looking into is to provide a webservice on my webserver that returns an image from another web server, something like this:
<Rectangle Tag="Photo" Stretch="Fill"> <Rectangle.Fill> <ImageBrush ImageSource="http://mywebservice/getFile?src='http://farm4.static.flickr.com/3054/3058233611_c9d128883e.jpg' "/> </Rectangle.Fill> </Rectangle>
07-11-2009 3:52 PM |
There must be some stupid legal reason that has made Ms build that limitation... or maybe it's related to avoid cross domain security threats? I mean you can just do that only by using a proxy service (slow thing but...).
07-12-2009 3:34 PM |
About export canvas to PNG, compiled all the bit and pieces into a lib:
http://geekswithblogs.net/braulio/archive/2009/07/12/export-canvas-to-png-and-save-it-in-your-local.aspx
Thanks a lot for all your help
Step 2:... JPEG and FJCore :).
07-15-2009 2:09 AM |
Hey Arnoud provide me with a great update !!!
Found the ultimate Silverlight PNG encoder with zip-compression..http://www.codeplex.com/Wiki/View.aspx?ProjectName=imagetools
07-15-2009 2:17 AM |
Please be aware that imagetools, while useful to some, is GPL-licensed so if you are developing a commercial application you can't use it unless you want to open-source your application (or alternatively try to get a dual license from the author).
I have said before there are Zip PNG writers out there that are not GPL'ed. It may be in C# or in C++ (close enough to be ported). Keep in mind that writing a PNG encoder is 10X easier than writing a PNG decoder. And PNG decoder is pretty easy to write to begin with :D
avbersSL
Member
184 points
91 Posts
07-15-2009 5:24 AM |
I'll wait for your MS-PL licensed version then ksleung.. ;)
07-15-2009 9:22 AM |
Wow !!
Solved the technical issue... now license issue :D.
malignate
20 points
21 Posts
07-15-2009 9:59 AM |
I changed the license to MS-PL, I think there should be no conflicts with other libraries I use for imagetools.
07-15-2009 12:18 PM |
Thanks a lot !!!
Excellent news !!!
07-15-2009 2:06 PM |
Thanks, great work!