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. Problem Copying Raw RGB Data to Bitmap

Problem Copying Raw RGB Data to Bitmap

Scheduled Pinned Locked Moved C#
helpgraphicsdata-structures
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.
  • J Offline
    J Offline
    Jaffer Mumtaz
    wrote on last edited by
    #1

    Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.

    static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
    {
    using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
    {
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
    Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
    bmp.UnlockBits(bmpData);
    bmp.Save(filename);
    }
    return 1;
    }

    It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,

    L B 2 Replies Last reply
    0
    • J Jaffer Mumtaz

      Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.

      static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
      {
      using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
      {
      BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
      Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
      bmp.UnlockBits(bmpData);
      bmp.Save(filename);
      }
      return 1;
      }

      It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Indeed, GetPixel/SetPixel is not the way to perform operations on an entire image. Here are 3 ideas that may help: 1. you could write a little loop that swaps the bytes in the byte array before executing the code shown. 2. you could try a Bitmap constructor that takes an IntPtr to raw data (may get the same result you have now); make sure to read MSDN's remark though. 3. you could apply a ColorMatrix transformation once the bitmap is filled. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Indeed, GetPixel/SetPixel is not the way to perform operations on an entire image. Here are 3 ideas that may help: 1. you could write a little loop that swaps the bytes in the byte array before executing the code shown. 2. you could try a Bitmap constructor that takes an IntPtr to raw data (may get the same result you have now); make sure to read MSDN's remark though. 3. you could apply a ColorMatrix transformation once the bitmap is filled. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        J Offline
        J Offline
        Jaffer Mumtaz
        wrote on last edited by
        #3

        Thanks for the timely reply Luc. Can you give some pointers/links/example on how to apply ColorMatrix? Thanks and Regards,

        1 Reply Last reply
        0
        • J Jaffer Mumtaz

          Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.

          static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
          {
          using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
          {
          BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
          Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
          bmp.UnlockBits(bmpData);
          bmp.Save(filename);
          }
          return 1;
          }

          It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          This is because the pixel format expects the bytes in the other order from what you're providing (BGR, I think. The 4 byte one is BGRA iirc). I would transform the bytes on the way in:

          byte[] newImageData = new byte[imageData.Length];
          for(int i = 0; i < newImageData.Length; i += 3){
          newImageData[i] = imageData[i + 2];
          newImageData[i + 1] = imageData[i + 1];
          newImageData[i + 2] = imageData[i];
          }

          Unless the image is really large, that should be negligible in time and memory terms and easier than post-processing the image with a ColorFilter.

          J 1 Reply Last reply
          0
          • B BobJanova

            This is because the pixel format expects the bytes in the other order from what you're providing (BGR, I think. The 4 byte one is BGRA iirc). I would transform the bytes on the way in:

            byte[] newImageData = new byte[imageData.Length];
            for(int i = 0; i < newImageData.Length; i += 3){
            newImageData[i] = imageData[i + 2];
            newImageData[i + 1] = imageData[i + 1];
            newImageData[i + 2] = imageData[i];
            }

            Unless the image is really large, that should be negligible in time and memory terms and easier than post-processing the image with a ColorFilter.

            J Offline
            J Offline
            Jaffer Mumtaz
            wrote on last edited by
            #5

            You are saying that bitmap is BGR, but I tried using SetPixel with RGB pattern and it worked fine. Does SetPixel works differently than copying RGB data?

            B 1 Reply Last reply
            0
            • J Jaffer Mumtaz

              You are saying that bitmap is BGR, but I tried using SetPixel with RGB pattern and it worked fine. Does SetPixel works differently than copying RGB data?

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              Yes. SetPixel takes a Color, copying byte data just takes raw bytes in the appropriate format.

              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