Click here to Skip to main content
13,047,596 members (64,746 online)
Rate this:
Please Sign up or sign in to 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.

BinaryWriter bw = new BinaryWriter(new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite));
for (int i = 0; i < a_pixels.Length; i++)

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.

Posted 23-Oct-11 10:41am
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..
gladiatron 30-Jul-12 4:56am
Hey there. Well not exactly the way i wanted to. There is an open source library here 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..
gladiatron 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
gladiatron 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
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. provides a list of some popular image file formats, and you can follow the links to more information.

gladiatron 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!
SAKryukov 23-Oct-11 23:03pm
My 5, good link.
Espen Harlinn 24-Oct-11 4:30am
Thank you, Sergey!
Simon Bang Terkildsen 25-Oct-11 15:41pm

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Oct 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100