Getting .png pixel data in Silverlight - silverlight

We have a Silverlight application that needs to load a number of .png files. We can load the images OK, but Silverlight doesn't support reading the pixel data from the Image class.
Can anyone suggest a simple solution for getting at this data? Our current best bet would be a third party .png loading library, but we are having trouble finding a suitable one.

There is no built in classes for doing pixel based imagine manipulation/generation i n Silverlight. You need to implement your own PNG Encoder/Decoder that works on an byte array containing the image information. Joe Stegman has implemented one such encoder you should check out. He got lots of great information about "editable images" in Silverlight over at http://blogs.msdn.com/jstegman/. He does things like applying filters to images, generating mandlebrots and more.
This blog discuss a JPEG Silverilght Encoder (FJCore) you can use to resize and recompress photos client size: http://fluxcapacity.net/2008/07/14/fjcore-to-the-rescue/
Another tool is "Fluxify" which lets you resize and upload photos using Silverilght 2. Can be found over at http://fluxtools.net/
So yes, client side image processing can definitely be done in Silverilght 2. Happy hacking!

PNG decoding is hard to find.
I wrote an article for MSDN that includes some open source code I cobbled together from Joe Stegman, FluxCapacity, and a few others. It includes PNG decoding (as well as GIF, JPG, and BMP) for those in this thread that are looking for that.
http://www.microsoft.com/youshapeit/msdn/ExpertKnowledge/2008-10/InnovateWithSilverlight2.aspx
If you don't care for my implementation of it for Silverlight, then you can go straight to what I modified for PNG decoding: http://sourceforge.net/projects/pr2/
Cheers!
Update: It looks like they no longer offer the source code on the site, so I re-posted the content here:
http://dimebrain.com/2009/01/innovate-with-silverlight-2-article-code-available.html

Thanks - I've seen the Joe Stegman blog. It's very handy, but the decoder he doesn't have is a PNG one. We're using PNGs as we need transparency. The fluxtools link seems to be broken too.
I'm still looking (unsuccessfully) for a PNG decoder I can drop into Silverlight.

One option that may (depending on circumstances) be easier is to save the color data and transparency data separately, then programmatically apply the transparency to the image once you have it loaded. That way, you could save the image as a 8-bit gif representing the alpha channel, plus a jpg or bmp or whatever for the color data.

Dimebrain - that msdn link you provided doesn't have a valid link to the sourcecode - any chance you have a link to the source that works?

Related

Using 3D photos in silverlight?

I want use photos captured by 3D cameras in Silverlight 3 or 4.
I want use that photos like in maps.google.com. Is it possible to do with silverlight and if yes is there any book, tutorial, video or anything alse, from which I can learn how to use tham.
Thanks.
It is possible to convert a stereoscopic images to an Anaglyph in Silverlight.
Have a watch of this video from Mix10 Moving at 60fps. You want to be 55 minutes into the video (its mainly about video but the principles are the same).
Unfortunately Eric doesn't provide any access to the pixel shader in question.
Check this demo out. It comes with full source code:
http://www.evanjohnston.com/post/Silverlight-4-Webcams-to-the-3rd-Dimension.aspx
They use two webcams and red- and green-filter pixel shaders to separate the streams. Of course the same can be used for static images.
It can be done with panoramic images.
Here is example for cylindric images.

Getting Bitmap for First Frame of AVI in Silverlight

I was wondering if there was a way to programmatically retrieve the first frame of an AVI and get a bitmap image to show the user a preview. The MediaControl in Silverlight shows a preview for Silverlight supported video files but not AVI. Because I'm in a Silverlight environment I cannot use unmanaged code or libraries to do so. I only have access to the filestream.
The Silverlight runtime doesn't have support for AVI files natively so you won't be able to use those with the MediaElement.
What you would need to do is actually parse the AVI file by hand and pull out frames from that file. Once you get to the point where you are parsing frames, it potentially gets a little trickier.
If you plan on having this work on Silverlight 2, your AVI file would need to contain WMV frames or frames in one of Silverlight's supported image formats(JPG or PNG). If you were working with WMV, I would set up a MediaStreamSource and pass in the desired video frame to the MediaStreamSource as my first sample. If you are working with one of the image formats you should use Image and set its source to a stream. You can see an example where Joe Stegman has used this to help Silverlight 2 support non-native image formats.
If you were doing this in Silverlight 3 (it's in Beta right now) the techniques are generally the same but the media format support grows a bit to include (in addition to WMV): H264, Raw YV12, or Raw ARGB frames. Similarly on the imaging front, you now have the WriteableBitmap which you could use to draw your frame.
If your video is something like Theora, Divx, Xvid, VP6, etc. You would need to find a way to decode that frame so you could display it in Silverlight.
Similar question has been posted on the Silverlight forum. The forum thread includes code samples.
You might want to pick a frame further into the video since the first few frames might fade in, or not be very representative of the video.
Depending on the encoding, your AVI files might not be readable by the MediaPlayer class. (See here for compatible encodings). You might need to transcode the video to do this with managed code. Unless you find/write your own decoder.

Convert a silverlight application to video format

I'm planning to make an animation with Silverlight, i want to export the animation to a video format, to be able to share it on video sharing website.
How can i do this ?
You could use a tool like Camtasia to record your screen or part of your screen. It then can be exported to a bunch of different video formats.
A pure code approach is to write a sequence of images, and then combine those images together as a video file. Eric Gunnerson has code to capture images from an animation , its WPF but would probably work in Silverlight? Codeproject has an example of converting a stream of images to an AVI video file in C#. It creates an AVI output. It would be great to go straight to mpg, but as it's compressed the input needs to be a video stream. If you do need mpg as the final output look for an off the shelf avi->mpg converter.
BTW - Let me know how you get on, I'm interested in the performance/functionality of this solution.
MrTelly: That approach would not work because Silverlight does not expose the RenderTargetBitmap class. (Well, this is not completely true, the class is there, but its constructor and methods are all marked SecurityCritical and as such are not normally accessible.)
The Camtasia solution proposed by Jakers is likely the simplest solution.
It's nuts that there isn't a simple way to do this in Silverlight, it's easy as pie in flash.

Sprite / Character animation in Silverlight (v2)

We have a Silverlight 2 project (game) that will require a lot of character animation. Can anyone suggest a good way to do this. Currently we plan to build the art in Illustrator, imported to Silverlight via Mike Snow's plug-in as this matches the skills our artists have.
Is key framing the animations our only option here? And if it is, what's the best way to do it? Hundreds of individual png's or is there some way in Silverlight to draw just a portion of a larger image?
You can use the Clip property on the image itself or on a container for the image to display a specific piece of a larger image, like a sprite sheet. This may or may not be more performant than swapping pngs. Also you could use the ImageBrush on a Rectangle to show just what you want, this would probably be a bit more efficient than the Clip property.
I just posted some code using Bill's suggestion regarding the Rectange and ImageBrush.
Silverlight at this time does not support bitmap effects nor has any libraries to manipulate the images. Your option now is to use keyframe animations from one png to another.
Now you can get at the raw bytes of an image. If you have your own image processing libraries you can compile them with the Silverlight dlls and then use the library in your Silverlight app.

Image processing in Silverlight 2

Is it possible to do image processing in silverlight 2.0?
What I want to do is take an image, crop it, and then send the new cropped image up to the server. I know I can fake it by clipping the image, but that only effects the rendering of the image. I want to create a new image.
After further research I have answered my own question. Answer: No. Since all apis would be in System.Windows.Media.Imaging and that namespace does not have the appropriate classes in Silverlight
I'm going to use fjcore. http://code.google.com/p/fjcore/
Thanks Jonas
Well, you can actually do local image processing in Silverlight 2... But there are no built in classes to help you. But you can load any image into a byte array, and start manipulating it, or implement your own image encoder.
Joe Stegman got lots of great information about "editable images" in Silverlight over at http://blogs.msdn.com/jstegman/. He does things like applying filters to images, generating mandlebrots and more.
This blog discuss a JPEG Silverilght Encoder (FJCore) you can use to resize and recompress photos client size: http://fluxcapacity.net/2008/07/14/fjcore-to-the-rescue/
Another tool is "Fluxify" which lets you resize and upload photos using Silverilght 2. Can be found over at http://fluxtools.net/
So yes, client side image processing can definetly be done in Silverilght 2. Happy hacking!
I know this doesn't directly answer your question, but what if you do all of the clipping on the client side to crop the image, then send the server the original image and the coordinates for clipping. Then on the server side, which will probably more suited for image manipulation like this (e.g. PHP it's very easy) you'll do the actual cropping of the image and storing the cropped version.
There is first-class support for bitmap surfaces in Silverlight 3: http://blogs.msdn.com/kaevans/archive/2009/03/20/some-silverlight-3-goodness-using-writeablebitmap.aspx

Resources