Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I am using a flash tool to capture an image using webcam from asp.net page.

After capturing, I am storing it on a database.
The size of the image is around 40kb.
I am retrieving the image from the database to view in an asp.net page.

The image retrieved is same as that of the captured image.

What I want is that the width of the image should be reduced to a required width.

This is my code...

C#
byte[] im = (byte[])dt.Rows[0]["Photo"]; //How to Convert this im  to srcimage

                        Bitmap newImage = new Bitmap(10, 10);
           using (Graphics gr = Graphics.FromImage(newImage))
           {
               gr.SmoothingMode = SmoothingMode.AntiAlias;
               gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
               gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
               gr.DrawImage(srcImage, new Rectangle(0, 0, 10, 10));

           }

           Response.ContentType = path;
           Response.BinaryWrite(im);


Thanks in advance.
Posted
Updated 11-Jul-11 22:26pm
v5
Comments
Dalek Dave 12-Jul-11 3:28am    
Edited for Grammar, Spelling, Syntax and Readability.

For the edited in question:

Create a MemoryStream and use Image.FromStream[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jul-11 4:55am    
I also updated my answer, in a bit different way. My 5 for yours.
--SA
BobJanova 12-Jul-11 4:58am    
Thanks, SA. I think this method probably calls the constructor in your answer so it should be the same!
This is not "compressing". Probably you store already compressed image, if you store JPEG or something else with lossy or lostless compression. What you need is called "re-sampling". First thing you need to know: you can effectively reduce picture size with acceptable quality. If you want to re-sample with enlargement, don't hope for good quality. I think this is obvious.

You can do one of two things: re-sample the image in C# or use it on the page as is using the attribute width:

HTML
<img src="myfile.jpg" width="required width in pixels" />


In this case, the image will be re-sampled by the Web browser. The problem of this approach is that the image will be downloaded in its full size, which needs more time and uses more traffic compared to the smaller image size. Still, I think this simplest method the best in most practical cases. You might need re-sampling on the server side if the original image is really much bigger, or you show different fragment of huge picture with different resolutions (zoom feature).

For re-sampling in on the server side in C# is pretty easy with System.Drawing. See, for example, this code sample: http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality[^].

[EDIT: answering a follow-up question]

Use the class Bitmap and its constructor Bitmap(Stream). Create memory stream from your bytes using constructor MemoryStream(Byte[]) and read bitmap from this memory stream:

C#
Bitmap myBitmap = new Bitmap(new MemoryStream(myBytes));


See http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^], http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^].

—SA
 
Share this answer
 
v4
Comments
[no name] 12-Jul-11 2:53am    
Excellent.+5.
Sergey Alexandrovich Kryukov 12-Jul-11 3:09am    
Thank you, Ramalinga.
--SA
Mathews Davis 12-Jul-11 3:03am    
Thanx for the link..It was worth!!!
Sergey Alexandrovich Kryukov 12-Jul-11 3:10am    
You're welcome.
If so, will you accept the solution formally (green button)? -- thank you.
--SA
Mathews Davis 12-Jul-11 3:20am    
K..But i have a small problem...The image retrieved is in bytes..How to convert it in to [System.Drawing.Image] image.....
If you have an image, you can create a new, blank image in the size you want, and draw your resized image on to that image within a Graphics object, and then save the image in that new size. In fact, I feel you can pass an image and a size in to an image constructor to do the same thing.
 
Share this answer
 
Comments
Mathews Davis 12-Jul-11 2:47am    
Infact it has nothing to do with the size...After retrieving from database i have to show the photo with a reduced size in asp.net page..

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