Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trouble converting byte[] to bmp.....

Trouble converting byte[] to bmp.....

Scheduled Pinned Locked Moved C#
helpgraphicstutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Are Jay
    wrote on last edited by
    #1

    public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); } I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.

    I'm listening but I only speak GEEK.

    N C A 3 Replies Last reply
    0
    • A Are Jay

      public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); } I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.

      I'm listening but I only speak GEEK.

      N Offline
      N Offline
      Nick Hodapp
      wrote on last edited by
      #2

      I did something like this the other day, using the static Bitmap.FromStream() method. Here's my code - my bitmap was encoded in a string... MemoryStream ms = new MemoryStream( Convert.FromBase64String( eImage.InnerText ) ); Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap; Why do you stream.Write()? Isn't the data already in the MemoryStream from the constructor? I also don't understand why you set bmp = null, or what the purpose of constructing a new Bitmap on return.

      A 1 Reply Last reply
      0
      • A Are Jay

        public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); } I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.

        I'm listening but I only speak GEEK.

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Parts of your code are a mess. Also, you don't say where the errors occurred - this is important and I cannot guess where. Lets examine the code:

        MemoryStream stream = new MemoryStream( b, true ); // MSDN MemoryStream Constructor[^]
        stream.Write( b, 0, b.Length ); // MSDN MemoryStream.Write[^]

        I'm not sure about your thought processes here. You create a MemoryStream using the contents of the buffer, b, which contains the data. Then you write the contents of b into the stream. But the stream already contains the contents of b. I suggest that the Write operation is redundant.

        Bitmap bmp = new Bitmap( stream ); // MSDN Bitmap Constructor[^]
        stream.Close();

        This seems reasonable.

        bmp = null;
        return new Bitmap( bmp, imageSize );

        I am curious as to what you think this actually does. I don't mean to be unkind, but I really don't understand your thought process. The following would seem to be a good way forward:

        using(MemoryStream ms = new MemoryStream(b))
        {
        Bitmap bmp = new Bitmap(ms);
        return bmp;
        }


        Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

        A 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Parts of your code are a mess. Also, you don't say where the errors occurred - this is important and I cannot guess where. Lets examine the code:

          MemoryStream stream = new MemoryStream( b, true ); // MSDN MemoryStream Constructor[^]
          stream.Write( b, 0, b.Length ); // MSDN MemoryStream.Write[^]

          I'm not sure about your thought processes here. You create a MemoryStream using the contents of the buffer, b, which contains the data. Then you write the contents of b into the stream. But the stream already contains the contents of b. I suggest that the Write operation is redundant.

          Bitmap bmp = new Bitmap( stream ); // MSDN Bitmap Constructor[^]
          stream.Close();

          This seems reasonable.

          bmp = null;
          return new Bitmap( bmp, imageSize );

          I am curious as to what you think this actually does. I don't mean to be unkind, but I really don't understand your thought process. The following would seem to be a good way forward:

          using(MemoryStream ms = new MemoryStream(b))
          {
          Bitmap bmp = new Bitmap(ms);
          return bmp;
          }


          Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

          A Offline
          A Offline
          Are Jay
          wrote on last edited by
          #4

          Colin Angus Mackay wrote:

          Also, you don't say where the errors occurred

          Error Message[^] Understanding the logic behind the MemoryStream basiclly this is the first time I'd used it and based of examples I'd found, I thought I need to perform the write to prepare the stream. I'm still getting the same error message. I've added a link to a screen shot of the error message. Thanks for your help.

          I'm listening but I only speak GEEK.

          1 Reply Last reply
          0
          • N Nick Hodapp

            I did something like this the other day, using the static Bitmap.FromStream() method. Here's my code - my bitmap was encoded in a string... MemoryStream ms = new MemoryStream( Convert.FromBase64String( eImage.InnerText ) ); Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap; Why do you stream.Write()? Isn't the data already in the MemoryStream from the constructor? I also don't understand why you set bmp = null, or what the purpose of constructing a new Bitmap on return.

            A Offline
            A Offline
            Are Jay
            wrote on last edited by
            #5

            Nick Hodapp wrote:

            Why do you stream.Write()?

            Basiclly this is the first time I'd used it and based of examples I'd found, I thought I need to perform the write to prepare the stream.

            Nick Hodapp wrote:

            I also don't understand why you set bmp = null

            I'll be calling this method hundreds of times with ASP.NET and will need to make sure that Garbage Collector knows I'm done with the variable. I'm still getting the same error message[^]. I've added a link to a screen shot of the error message. Thanks for your help.

            I'm listening but I only speak GEEK.

            1 Reply Last reply
            0
            • A Are Jay

              public static Bitmap BytesToBmp( byte[] b, Size imageSize ) { // Open a stream for the image and write the bytes into it MemoryStream stream = new MemoryStream( b, true ); stream.Write( b, 0, b.Length ); // Create a bitmap from the stream Bitmap bmp = new Bitmap( stream ); stream.Close(); bmp = null; return new Bitmap( bmp, imageSize ); } I'm getting an error: System.ArgumentException: Parameter is not valid. Then when I use the Stream Object I get: Cannot create an instance of the abstract class or interface 'System.IO.Stream'. I'm at a lose on how to create a Bitmap from a Stream if I can not create a Stream. I was under the impression that a derived class such as: MemoryStream would work. Any help explaining this would be helpful. Thanks.

              I'm listening but I only speak GEEK.

              A Offline
              A Offline
              Are Jay
              wrote on last edited by
              #6

              Thank you every one for your help.

              public static Bitmap BytesToBmp( byte[] b )
              {
              using ( MemoryStream ms = new MemoryStream(b , true) )
              {
              Bitmap bmp = Bitmap.FromStream( ms ) as Bitmap;
              return bmp;
              }
              }

              Works great. This is was the byte[] that I was sending the method; it was not being recognized by the Bitmap Constructor. I am storing images within an Access 2003 db, (yes, not a good idea), and the way Access had been altering the byte [] so that the Bitmap Constructor couldn't recreate it. I'm not sure n exaclty what Access was doing with the Image but I as inserting a bitmap from file to it. ??? If anyone know the answer to this, please drop me a line. Otherwise, I'm now converting the image and inserting it into the database using a FileStream in and then a FileStream out. If anyone is interested I will post the project when I've completed. Thanks again!!

              I'm listening but I only speak GEEK.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups