Creating Bitmap from byte[]

This is a discussion on Creating Bitmap from byte[] within the C# Programming forums, part of the General Programming Boards category; I cant seem to get this to work. I have a program in C++ that returns to my C# app ...

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    Creating Bitmap from byte[]

    I cant seem to get this to work. I have a program in C++ that returns to my C# app through a DLL a char* (byte[]) on the C# side. The image data is stored as RGBA, I am trying to turn this into a bitmap, however when trying to create it from a stream i get an exception of:

    Parameter is not valid.

    I also tried:
    Code:
    Bitmap bmp = new Bitmap(width,height);
    
    BitmapData bmpData = bmp.LockBits(new Rectangle(0,0,width,height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(imgData, 0, bmpData.Scan0, imgData.Length);
    bmp.UnlockBits(bmpData);
    Which sort of works however all the image colors are now way off, and I know this is due to the PixelFormat, is there no way to do this without swapping pixels in the data?

    [Edit]
    Well I tried swapping all the bytes from RGBA to ARGB and that just made it look even worse.
    Last edited by xds4lx; 01-11-2010 at 04:34 PM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I know you've said stream doesn't work, but maybe try this anyway? It works for me...

    Code:
    public static Bitmap bytes2bmp(byte[] data)
    {
        return new Bitmap(new System.IO.MemoryStream(data));
    }

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    That is basically what I tried, from what I understand you can only create the file from the stream if the stream has the bitmap header information in it.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well I solved the problem. Even though the image was in RGBA format it was being displayed wrong, I swapped the R and B ad now the image shows up correctly, using the LockBits and UnlockBits method.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by xds4lx View Post
    Well I solved the problem. Even though the image was in RGBA format it was being displayed wrong, I swapped the R and B ad now the image shows up correctly, using the LockBits and UnlockBits method.
    It's probably endian issues. 32bppARGB (which is what pixel format C# is expecting) is stored as a 32 bit number with alpha in the most-significant-byte, red in the second-most-significant-byte, etc.

    Now, on a big-endian machine this would be the same as the order of the bytes within the 32-bit integer, but on a little-endian machine, blue is stored and read first (at the lowest byte address), then green, red, alpha. So if you're writing bytewise, you need to write B G R A in order to read it as 32bppARGB.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating ICON form BITMAP files
    By Uwar in forum Windows Programming
    Replies: 0
    Last Post: 12-21-2009, 04:32 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. Copying Bitmap Resources without Creating HDC's
    By bartybasher in forum Windows Programming
    Replies: 1
    Last Post: 08-02-2004, 06:35 AM
  5. Help creating a Bitmap
    By Kristian25 in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2003, 04:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21