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.L ength - ExtractPos);
byte[] imageBuffer = new byte[imageSize];
httpImageFile.InputStream.Read(imageBuffer, 0, imageSize);
System.Drawing.Image msImage =
System.Drawing.Image.FromStream(httpImageFile.Inpu tStream);
Bitmap outBitmap = new Bitmap(msImage, new
Size(Int32.Parse(ConfigurationSettings.AppSettings["MaxImageWidth"].ToString()),
(int)((Int32.Parse(ConfigurationSettings.AppSettin gs["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.AppSettin gs["MaxImageWidth"].ToString())*msImage.Height)/msImage.Width);
byte[] thumbnailBuffer = new byte[(int)thumbnailStream.Length];
thumbnailStream.Read(thumbnailBuffer, 0,
(int)thumbnailStream.Length);
image.File = thumbnailBuffer;