Problem with reading byte[] from Stream

L

Li Zhang

I have a problem with reading byte[] from a memory stream. Basically I
want to resize a image and then store them to sql server. Here is part
of the codes. every time I tried to read byte[] from stream, I only got
all 0 byte[], I don't know what is the problem. Please help me to check
my error. Thanks.

httpImageFile = imageFile.PostedFile;
imageSize = httpImageFile.ContentLength;
imageContentType = httpImageFile.ContentType;
imageFileName = httpImageFile.FileName;
int ExtractPos = imageFileName.LastIndexOf("\\") + 1;
imageFileName =
imageFileName.Substring(ExtractPos,imageFileName.Length - ExtractPos);
byte[] imageBuffer = new byte[imageSize];
httpImageFile.InputStream.Read(imageBuffer, 0, imageSize);
System.Drawing.Image msImage =
System.Drawing.Image.FromStream(httpImageFile.InputStream);

Bitmap outBitmap = new Bitmap(msImage, new
Size(Int32.Parse(ConfigurationSettings.AppSettings["MaxImageWidth"].ToString()),
(int)((Int32.Parse(ConfigurationSettings.AppSettings["MaxImageWidth"].ToString())*msImage.Height)/msImage.Width)));
System.IO.Stream thumbnailStream = new System.IO.MemoryStream();
if (httpImageFile.ContentType.ToLower() == "image/pjpeg")
outBitmap.Save(thumbnailStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
else
outBitmap.Save(thumbnailStream,
System.Drawing.Imaging.ImageFormat.Gif);
image.Width =
Int32.Parse(ConfigurationSettings.AppSettings["MaxImageWidth"].ToString());
image.Height =
(int)((Int32.Parse(ConfigurationSettings.AppSettings["MaxImageWidth"].ToString())*msImage.Height)/msImage.Width);
byte[] thumbnailBuffer = new byte[(int)thumbnailStream.Length];
thumbnailStream.Read(thumbnailBuffer, 0,
(int)thumbnailStream.Length);
image.File = thumbnailBuffer;
 
M

Mattias Sjögren

every time I tried to read byte[] from stream, I only got
all 0 byte[], I don't know what is the problem. Please help me to check
my error. Thanks.

You have to "rewind" the stream before reading from it, either by
setting the Position property or calling Seek.



Mattias
 
L

Li Zhang

Mattias said:
every time I tried to read byte[] from stream, I only got
all 0 byte[], I don't know what is the problem. Please help me to check
my error. Thanks.


You have to "rewind" the stream before reading from it, either by
setting the Position property or calling Seek.



Mattias

Mattias, Thank you very much! Problem solved! Have a nice weekend!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top