Click here to Skip to main content
Rate this: bad
good
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++)
{
      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 23-Oct-11 11:41am
Comments
Member 8750556 at 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 at 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 at 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..
gladiatron at 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 at 31-Jul-12 4:22am
   
Thanx for the help...

1 solution

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

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 23 Oct 2011
Copyright © CodeProject, 1999-2014
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