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. Raw Image to Bmp Image

Raw Image to Bmp Image

Scheduled Pinned Locked Moved C#
data-structuresquestion
14 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 Alexsander Antunes

    I have un array of byte[] below: byte[] img = new byte[78000] ... Recept raw data from a fingerprint scanner ... This array contains the data of a image with 300 of height and 260 of width (total = 300 x 260 = 78000). Each position in the array (byte = 256) is a pixel (256 shades of gray). How I create the Image object with this data? Thanks for all response, :laugh: Alexsander "Axia" Antunes

    H Offline
    H Offline
    Heath Stewart
    wrote on last edited by
    #5

    The Bitmap class and related classes and structs actually support this. You can use the constructor Bitmap(int, int, int, PixelFormat, IntPtr) to create a bitmap of a known size, source format, and the address of the first element in the data buffer. Basically, it uses similar functionality from GDI like the CreateDIBitmap API (although internally it uses GDI+ APIs). This is untested, but should give you some idea

    byte[] img = new byte[7800];
    // Fill img
    GCHandle gch = GCHandle.Alloc(img);
    Bitmap bmp = new Bitmap(300, 260, 2400, PixelFormat.Format25bppRgb,
    gch.AddrOfPinnedObject());
    gch.Free();

    I don't currently have any raw data to try it out on, but this seems to be the way that is similar to how it's done in good ol' C/C++. Let me know if it works. My digital camera outputs RAW images and eventually I was going to write a new front end since the one that came with it sucks.

    Microsoft MVP, Visual C# My Articles

    M A 2 Replies Last reply
    0
    • H Heath Stewart

      This actually wouldn't work. A Bitmap (as do all images) contain header information. This raw data does not. This is a similar situation with digital cameras and digital SLRs that can save RAW image data. They need to have a program that translates the RAW data into an image.

      Microsoft MVP, Visual C# My Articles

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #6

      Heath Stewart wrote: A Bitmap (as do all images) contain header information Well, what about if he writes them in that stream too? Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

      H 1 Reply Last reply
      0
      • H Heath Stewart

        The Bitmap class and related classes and structs actually support this. You can use the constructor Bitmap(int, int, int, PixelFormat, IntPtr) to create a bitmap of a known size, source format, and the address of the first element in the data buffer. Basically, it uses similar functionality from GDI like the CreateDIBitmap API (although internally it uses GDI+ APIs). This is untested, but should give you some idea

        byte[] img = new byte[7800];
        // Fill img
        GCHandle gch = GCHandle.Alloc(img);
        Bitmap bmp = new Bitmap(300, 260, 2400, PixelFormat.Format25bppRgb,
        gch.AddrOfPinnedObject());
        gch.Free();

        I don't currently have any raw data to try it out on, but this seems to be the way that is similar to how it's done in good ol' C/C++. Let me know if it works. My digital camera outputs RAW images and eventually I was going to write a new front end since the one that came with it sucks.

        Microsoft MVP, Visual C# My Articles

        M Offline
        M Offline
        Mazdak
        wrote on last edited by
        #7

        Ok, I got the answer. :-) Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

        1 Reply Last reply
        0
        • M Mazdak

          Heath Stewart wrote: A Bitmap (as do all images) contain header information Well, what about if he writes them in that stream too? Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #8

          It would be possible to create all the information and serialize that (not .NET serialization, mind you) that to a stream, but it is incredibly error-prone and pain in the butt. For an app I wrote I did something similar that extracts icons and I have to serialize these correctly using the chain of sturctures required. Believe me, you don't want to do that! As you saw in my other answer, .NET does provide a way to do this. You could always P/Invoke native APIs like CreateDIBitmap to accomplish this as well. These basically take minimal information and create and serialize the structures for your. I was about to answer that way when I found that you can't (easily) get a Bitmap from a handle :wtf: and noticed the other constructor I've never cared to find before (that basically does the same thing I was going to mention with native APIs).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • H Heath Stewart

            The Bitmap class and related classes and structs actually support this. You can use the constructor Bitmap(int, int, int, PixelFormat, IntPtr) to create a bitmap of a known size, source format, and the address of the first element in the data buffer. Basically, it uses similar functionality from GDI like the CreateDIBitmap API (although internally it uses GDI+ APIs). This is untested, but should give you some idea

            byte[] img = new byte[7800];
            // Fill img
            GCHandle gch = GCHandle.Alloc(img);
            Bitmap bmp = new Bitmap(300, 260, 2400, PixelFormat.Format25bppRgb,
            gch.AddrOfPinnedObject());
            gch.Free();

            I don't currently have any raw data to try it out on, but this seems to be the way that is similar to how it's done in good ol' C/C++. Let me know if it works. My digital camera outputs RAW images and eventually I was going to write a new front end since the one that came with it sucks.

            Microsoft MVP, Visual C# My Articles

            A Offline
            A Offline
            Alexsander Antunes
            wrote on last edited by
            #9

            In the code below: Bitmap bmp = new Bitmap(300, 260, 2400, PixelFormat.Format24bppRgb, gch.AddrOfPinnedObject()); What is the third parameter (2400)? In th VS Documentation is the stride parameter, what is this? Thanks for all, :) Alexsander "Axia" Antunes

            H 1 Reply Last reply
            0
            • A Alexsander Antunes

              In the code below: Bitmap bmp = new Bitmap(300, 260, 2400, PixelFormat.Format24bppRgb, gch.AddrOfPinnedObject()); What is the third parameter (2400)? In th VS Documentation is the stride parameter, what is this? Thanks for all, :) Alexsander "Axia" Antunes

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #10

              It's the number of bites in a single row. Since your image is 300 pixels wide and you're dealing with a byte[], which should actually be 300 to represent 300 bytes. I was multiplying by 8 to get the number of bits, which is incorrect (not sure what I was thinking).

              Microsoft MVP, Visual C# My Articles

              A 2 Replies Last reply
              0
              • H Heath Stewart

                It's the number of bites in a single row. Since your image is 300 pixels wide and you're dealing with a byte[], which should actually be 300 to represent 300 bytes. I was multiplying by 8 to get the number of bits, which is incorrect (not sure what I was thinking).

                Microsoft MVP, Visual C# My Articles

                A Offline
                A Offline
                Alexsander Antunes
                wrote on last edited by
                #11

                My image is 300 of heigth and 260 of width. How value is for stride? 2080(260 * 8 bits) or 2400 (300 * 8 bits) :) Alexsander "Axia" Antunes

                H 1 Reply Last reply
                0
                • H Heath Stewart

                  It's the number of bites in a single row. Since your image is 300 pixels wide and you're dealing with a byte[], which should actually be 300 to represent 300 bytes. I was multiplying by 8 to get the number of bits, which is incorrect (not sure what I was thinking).

                  Microsoft MVP, Visual C# My Articles

                  A Offline
                  A Offline
                  Alexsander Antunes
                  wrote on last edited by
                  #12

                  My image is 300 of heigth and 260 of width. How value is for stride? 2080(260 * 8 bits) or 2400 (300 * 8 bits) :) But in 2 cases I have the following error: Additional information: Identification is not fixed. Alexsander "Axia" Antunes

                  H 1 Reply Last reply
                  0
                  • A Alexsander Antunes

                    My image is 300 of heigth and 260 of width. How value is for stride? 2080(260 * 8 bits) or 2400 (300 * 8 bits) :) Alexsander "Axia" Antunes

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #13

                    No, like I said - I made a mistake. Just use 260 for the value, and swap 260 and 300 for the first two params since I accidentally reversed your width and height.

                    Microsoft MVP, Visual C# My Articles

                    1 Reply Last reply
                    0
                    • A Alexsander Antunes

                      My image is 300 of heigth and 260 of width. How value is for stride? 2080(260 * 8 bits) or 2400 (300 * 8 bits) :) But in 2 cases I have the following error: Additional information: Identification is not fixed. Alexsander "Axia" Antunes

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #14

                      One what like do you get this? Debug your application and find out exactly where the error is happening.

                      Microsoft MVP, Visual C# My Articles

                      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