locked
Can't convert byte array to system.io.stream RRS feed

  • Question

  • Hi,

        I'm using an older version of itextsharp to convert images in the inkpresenter to pdf format. The code I have uses

    System.Drawing.

     

    Image.FromStream to create an initial image out of the bitmap that is passed before converting it to a pdf. This conversion is taking place in an ihttphandler.

    I was able to take the data streamed into the handler in a stream variable, convert it to a byte array and isolate a part of the stream into another byte array. This "part" is the information I want to pass to

    System.Drawing.

     

    Image.FromStream. I've tried to use the write method of the stream object but canwrite is set to false. I'm not certain I can change it or why it's set that way.

    I've also tried to convert the byte array to a memory stream and then move it to the stream. In this case the runtime thinks the stream is a memory stream even though I've decleared it as a stream. The fromstream function won't accept anything but a stream; it won't accept a memory stream.

    I've tried other things I've read such as making the stream an object and moving the byte array to that but that doesn't work either. I've spent a long time on this one little quirk in .net. All I want to do is take the byte array, pass it to a new stream variable and pass that stream to the fromstream function.

    Any help would be appreciated.

    Thanks,

    Fig000

    Friday, March 11, 2011 4:19 AM

Answers

  • Stream and byte[] are different names for the same thing.  They are essentially interchangable.  ms = new MemoryStream(byte[]) and byte[] = ms.GetBuffer.

    For your problem, ensure that you're seeking the correct position when manipulating the stream.

    Friday, March 11, 2011 4:43 PM
  • system.io.stream is the base class , you need to use the derived classes like MemoryStream , FileStream

    like below

    System.IO.Stream stream = new System.IO.MemoryStream(bytesArray);

     

     

    try it

    Tuesday, March 15, 2011 2:35 AM

All replies

  • Hi,
    Haven't understood the question actually. If it's like byte to stream conversion (as it's in the subject line) then it's pretty easy. Examlple:

    byte[] bytes = new byte[100];
    System.IO.Stream stream = new System.IO.MemoryStream(bytes);

    Thanks
    Anirban
    Anirban Bhattacharya (Lead Consultant - ESSPL)
    Friday, March 11, 2011 6:55 AM
  • AnirbanBhattacherya,

       Thanks for answering so quickly. I have tried your method as follows:

    My real problem is that I am taking a stream passed into an httphandler. I then copy the stream to a byte array to isolate a pice of it. I write the piece of the stream I want to a new byte array. This is where I have problems. I'm trying to copy the new byte array  to a new stream and then pass it to:

    System.Drawing.Image.FromStream(onePageStream);

    So if I use your method it looks like this code below:

    System.IO.

     

    Stream onePageStream = new System.IO.MemoryStream(onePage);

    System.Drawing.

     

    Image img = System.Drawing.Image.FromStream(onePageStream);

    onePage is a byte array that I want to copy to the stream onePageStream. However when I assign the MemoryStream to the Stream, the system sees the Stream as a MemoryStream. That surprised me though I know that Stream is the baseclass for many of the other Stream based objects. But onePageStream is seen as a MemoryStream after the assignment in the first line.

    What happens is when it gets to the second line where FromStream is called, the system thinks that Stream is a MemoryStream. It blows up and tells me I have invalid parameters. It seems that FromStream will ONLY accept a Stream. It will NOT accept a MemoryStream.

    I know that FromStream works. I've passed a stream into the httphandler where the code is and passed it, unchanged, to FromStream with complete success. However, In this case I'm manipulating the stream by putting it into a byte array. I can't get the byte array back into a new stream. This is very frustrating.

    I've tried casting the MemoryStream as stream, casting the byte array onePage as an object before copying it to OnePageStream, and a few other things. Nothing works. I also tried stream.write but for some reason "canwrite" is always false and I don't think I can set it to true (not sure how that works).

    Any advice would be appreciated.

    Thanks,

    Fig000

    Friday, March 11, 2011 1:47 PM
  • Hi,

       After looking around in google I'm beginning to think that when I move the Stream to a byte array, create a new byte array and try to save that new byte array to a new stream, there might be some encoding involved. I'm not that experienced with encoding and what that involves. Here are my steps of what I need to do:

    1. Pass byte array to ihttphandler

    2. Receive byte array as stram inside the handler.

    3. Move the stream to a byte array

    4.  I am passing two bitmaps divided by certain characters (delimiter) in the stream going to the handler. I want to take the first part of the array, up to the delimiting characters, and save it in the new byte array. This should be my first image. I'll repeat this for the second image in the stream.

    5. Put the contents of the new byte array into a new stream to pass to Image.FromStream.

    Is there any encoding involved in passing the data around in any of these steps? In other words am I creating an invalid stream by moving it to a byte array and putting it in a memory stream to pass to Image.FromStream? Is the lack of encoding making the stream invalid?

    Thanks,

    Fig000

    Friday, March 11, 2011 3:20 PM
  • Stream and byte[] are different names for the same thing.  They are essentially interchangable.  ms = new MemoryStream(byte[]) and byte[] = ms.GetBuffer.

    For your problem, ensure that you're seeking the correct position when manipulating the stream.

    Friday, March 11, 2011 4:43 PM
  • Image.FromStream expects an image file in the stream, not mime-encoded, not base64-encoded. Make sure you decode such a stream before calling Image.FromStream.

    Monday, March 14, 2011 5:21 PM
  • system.io.stream is the base class , you need to use the derived classes like MemoryStream , FileStream

    like below

    System.IO.Stream stream = new System.IO.MemoryStream(bytesArray);

     

     

    try it

    Tuesday, March 15, 2011 2:35 AM
  • Hi Hamid,

    That works perfectly for me, thank you.  In VB.NET speak the line is...

    Dim stream As System.IO.Stream = New System.IO.MemoryStream(byteArray)

    Wednesday, December 19, 2012 12:48 AM