473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with reading byte[] from Stream

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;
Nov 17 '05 #1
2 4268
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 Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Mattias Sjögren wrote:
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!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: Marty | last post by:
Hi, I took this socket listener from the VC++.NET help file. I inserted it in my project. What library do I have to include to have those identifier working: string, int32, Byte, server,
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
0
by: Iridium | last post by:
Greetings, I am trying to get a JPG Frame from a MJPG Stream. A MJPG is basically a stream of JPGs which are splitted by a special boundary string. So I tried to get the stream, split it by the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.