C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2010, 05:28 PM   #1
Registered User
 
xds4lx's Avatar
 
Join Date: Nov 2001
Posts: 626
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.
__________________
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

Last edited by xds4lx; 01-11-2010 at 05:34 PM.
xds4lx is offline   Reply With Quote
Old 01-11-2010, 05:39 PM   #2
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 117
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));
}
theoobe is offline   Reply With Quote
Old 01-11-2010, 05:41 PM   #3
Registered User
 
xds4lx's Avatar
 
Join Date: Nov 2001
Posts: 626
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
xds4lx is offline   Reply With Quote
Old 01-11-2010, 06:00 PM   #4
Registered User
 
xds4lx's Avatar
 
Join Date: Nov 2001
Posts: 626
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
xds4lx is offline   Reply With Quote
Old 01-15-2010, 04:03 PM   #5
Cat
Registered User
 
Join Date: May 2003
Posts: 1,219
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.
Cat is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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