Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Hackers,

SO i have been dabbling a little bit with System.Windows.Media.Imaging namespace and I am experimenting with accessing web cam through a silverlight web app. I can get the webcam's live feed and even capture a frame to a Rectangle, but I am finding myself at a dead end not being able to save that frame to an image file (.png or .jpg).

The frame captured is basically a WriteableBitmap and the below shown code converts the pixel array of the WriteableBitmap to bytes and pumps them out to a file.

C#
BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite));
for (int i = 0; i < a_pixels.Length; i++)
{
      bw.Write((byte)a_pixels[i]);
}

bw.Flush();
bw.Close();


Turns out it does create a file but obviously not in the correct format which means something more has to be done to render it into an image format. Which is what my question is "what do you think I may be missing?". Please don't forward me to open source libraries or anything like that coz I want to be able to do this myself which is why I haven't already opted for third party libs.

Help much appreciated. Thanks. Please let me know if you need more info.

Cheers.
Posted
Comments
Member 8750556 30-Jul-12 2:42am    
Hey, have u solved the problem? Actually I want to save captured image in the server folder..Can u plz provide me the code..
I.explore.code 30-Jul-12 4:56am    
Hey there. Well not exactly the way i wanted to. There is an open source library here http://imagetools.codeplex.com/ which might be upto the job. You can give this a try!
Member 8750556 31-Jul-12 0:46am    
Hey gladiatron
Thanx for replying.. Actually I have gone through this also..But I want to save webcam image directly to the server folder on clicking Save button..
Can u give me some code..
Neha..
I.explore.code 31-Jul-12 4:01am    
Well, if you are able to capture a frame from the camera in memory then all you have to do (theoretically) is send those bytes to the server to be handled by the ImageTools library which will then convert bytes to an image format. I think you can also do it from the client side directly if you are using Silverlight since ImageTools is also available for Silverlight. You would have to use a bit of your own imagination to save a file on the server. This is quite generic stuff and Google will give you loads of answers for it. Hope this helps.
Member 8750556 31-Jul-12 4:22am    
Thanx for the help...

1 solution

Take a look at the System.Windows.Media.Imaging.RenderTargetBitmap[^] class.

Use your WriteableBitmap as the source for the Image in the sample provided at the bottom of the page.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
I.explore.code 23-Oct-11 18:39pm    
thanks mate! but i am afraid this class is not designed for Silverlight runtime so the dll (PresentationCore.dll) cannot be referenced in a Silverlight project. They can however be referenced in a simple Winforms project but then i am not sure how the webcame can be captured coz there are no classes like ImageBrush, CaptureSource, VideoCaptureDevice etc... got any ideas? :(
Espen Harlinn 23-Oct-11 19:22pm    
Not something useful that fits inside a quick reply.
I'd look at http://johnpapa.net/saving-snapshots-to-png-in-silverlight-4-and-the-webcam
jpg, png, and other picture formats are encoded in special formats - both jpg and png relies on fairly complex compression techniques to reduce the spece required to store the image.
http://en.wikipedia.org/wiki/Comparison_of_graphics_file_formats provides a list of some popular image file formats, and you can follow the links to more information.

I.explore.code 24-Oct-11 4:14am    
Thanks mate! help much appreciated, will hack around these things to see what can be done. Good links, my 5 too!
Sergey Alexandrovich Kryukov 23-Oct-11 23:03pm    
My 5, good link.
--SA
Espen Harlinn 24-Oct-11 4:30am    
Thank you, Sergey!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900