473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Poste dFile;
imageSize = httpImageFile.C ontentLength;
imageContentTyp e = httpImageFile.C ontentType;
imageFileName = httpImageFile.F ileName;
int ExtractPos = imageFileName.L astIndexOf("\\" ) + 1;
imageFileName =
imageFileName.S ubstring(Extrac tPos,imageFileN ame.Length - ExtractPos);
byte[] imageBuffer = new byte[imageSize];
httpImageFile.I nputStream.Read (imageBuffer, 0, imageSize);
System.Drawing. Image msImage =
System.Drawing. Image.FromStrea m(httpImageFile .InputStream);

Bitmap outBitmap = new Bitmap(msImage, new
Size(Int32.Pars e(Configuration Settings.AppSet tings["MaxImageWi dth"].ToString()),
(int)((Int32.Pa rse(Configurati onSettings.AppS ettings["MaxImageWi dth"].ToString())*ms Image.Height)/msImage.Width)) );
System.IO.Strea m thumbnailStream = new System.IO.Memor yStream();
if (httpImageFile. ContentType.ToL ower() == "image/pjpeg")
outBitmap.Save( thumbnailStream ,
System.Drawing. Imaging.ImageFo rmat.Jpeg);
else
outBitmap.Save( thumbnailStream ,
System.Drawing. Imaging.ImageFo rmat.Gif);
image.Width =
Int32.Parse(Con figurationSetti ngs.AppSettings["MaxImageWi dth"].ToString());
image.Height =
(int)((Int32.Pa rse(Configurati onSettings.AppS ettings["MaxImageWi dth"].ToString())*ms Image.Height)/msImage.Width);
byte[] thumbnailBuffer = new byte[(int)thumbnailS tream.Length];
thumbnailStream .Read(thumbnail Buffer, 0,
(int)thumbnailS tream.Length);
image.File = thumbnailBuffer ;
Nov 17 '05 #1
2 4282
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
2326
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 your Visual Studio and compile it + run it; so you can see what the actual problem is. Thanks. code:
11
6603
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the...
0
3921
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header,...
1
1034
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
1837
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 (an endless loop). i thought that BinaryReader.ReadByte advanced to the next byte? i had it time out after 1000 iterations, and keeps outputting...
9
14254
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 work at all. I have tried also in C++ .NET 2005 and also does not work. I did not find any useful information on the net. Do you have a small...
2
11574
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 void loadBytes(InputStream is){ DataSource ds=new DataSource(is); player = Manager.createPlayer(ds);
0
3477
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 datasource import java.awt.BorderLayout; import java.awt.Component; import java.io.*; import java.net.URL; import javax.media.*;
0
2048
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 boundary strings and save the JPG binary data into a file. The problem is, I cant save the data into a proper JPG file which I then open. This is my...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.