Click to See Complete Forum and Search --> : Image class and memoryStream/Stream problems once more


eloberg
January 29th, 2003, 02:06 AM
I have an array of gray level bytes I want to show on screen. In order to do that I use MemoryStream and the Image class:
private void CellView_Paint(object sender, System.Windows.Forms.PaintEventArgas e)
{
MemoryStream strm = new MemoryStream();
Image img;
Graphics grfx;
Graphics grfx = e.Graphics;
// Panel was created further up in the program
// what is shown here is a small part of the whole program
grfx = panel.CreateGraphics();
strm.Position = 0; strm.Write(pByteArray,0,nSize);
Stream st = (Stream)strm;
img = Image.FromStream(st); grfx.DrawImage(img,0,0,width,height);
strm.Close;
}

But the program gives the same error message as before:

Invalid parameter used in the statement below:
img = Image.FromStream(strm);

It seems as if



Report this post to a moderator | IP: Logged
01-28-2003 10:48 AM

pareshgh
January 29th, 2003, 10:58 AM
instead of copy pasting the exact code.

why are you typing

private void CellView_Paint(object sender, System.Windows.Forms.PaintEventArgas e)

>>>Argas ?


Graphics grfx;
Graphics grfx = e.Graphics;

>>>declared twice ?

strm.Close
>>>strm.Close() ?


*************
I tested for at least compilation
private void buttonOpenIE_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
MemoryStream strm = new MemoryStream();
Image img;

Graphics grfx = e.Graphics;
byte []pByteArray;
pByteArray = new byte[10];
for(int i=0;i<10;i++)
pByteArray[i]= 10;
grfx = button1.CreateGraphics();
strm.Position = 0; strm.Write(pByteArray,0,pByteArray.Length);
Stream st = (Stream)strm;
img = Image.FromStream(st); grfx.DrawImage(img,0,0,20,20);
strm.Close();
}
************

atleast gets compiled.

strm doesn't have problem . the problem is somewhere else ?

thanx
Paresh

eloberg
January 30th, 2003, 02:26 AM
Thanks for your help.
I made a new program where I used the code you send me. The compilation went fine though, but when I ran the program I got the same error message as before in my original program. It crashes in the statement

img = Image.FromStream(st)

where an error message box pops up saying
Invalid parameter used

pareshgh
January 30th, 2003, 10:59 AM
Okay could you attach your project (without the bin directory) as a zip file . i will have a look.

Paresh

eloberg
January 31st, 2003, 03:40 AM
Hi!

Thanks for having a look. I attach the project without the bin directory. It is a very simple example in order to keep the program as simple and small as possible

pareshgh
January 31st, 2003, 04:29 PM
what is this ????

Image img;
byte[]pByteArray = new byte[10];
for (int i = 0; i < 10; i++)
pByteArray[i] = 10;

is this some kind of byte initialization ???? i just send u to insure that it compiles.

but why r u creating a mess here. u need a real file to read that.


Graphics grfx = button1.CreateGraphics();
strm.Position = 0;

???
strm.Write(pByteArray,0,pByteArray.Length);
Stream st = (MemoryStream)strm;
img = Image.FromStream(strm);

?? can't do like that. pbytearray will
have junk
it will throw error
grfx.DrawImage(img,0,0,20,20);
strm.Close();
st.Close();

u need to read from file and then use it atleast.
what r u overall trying to do ? learning ???

Paresh