CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    36

    Image class and memoryStream/Stream problems once more

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    36

    Problems with Stream in FromImage command

    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

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    Okay could you attach your project (without the bin directory) as a zip file . i will have a look.

    Paresh

  5. #5
    Join Date
    Apr 1999
    Posts
    36

    StremTest program attached

    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
    Attached Files Attached Files

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured