Bitmap to ofImage

I need to convert a .NET Bitmap to an ofImage, how could I do this? So far I can retrieve a byte[] from the Bitmap, is this what needs to be converted to unsigned char* to setFromPixels?


Yes, but you’ll want to be careful that the order of the bytes is the same as ofImage expects as they might be BGR instead of RGB (I don’t know a ton about the .NET Bitmap so that might not be a worry).

I am testing an image of resolution 400x234, I assume I should be getting 400*234*3 = 280800 bytes, when I’m actually retrieving 11948 bytes from the Bitmap.

This is how I convert from Bitmap to byte[].

        private static byte[] ImageToByte(Bitmap img)  
            ImageConverter converter = new ImageConverter();  
            return (byte[])converter.ConvertTo(img, typeof(byte[]));  

Well, C# byte is Unsigned 8-bit integer, so it should be equivalent to an unsigned char, but I’m not sure how ImageConverter works with that stuff. Also, how are you mixing C# and C++?

I got it to work, but I have the problem you mentioned. Windows’ Bitmap is BGR… I’m thinking what would be the best way to change that while retrieving the pixels. It’s done with Bitmap.LockBits, here’s an example:

About mixing C++ and C#, I opened a thread about that a few days ago:
Linking to this source
Hope it helps someone.

Back to this thread… any idea on how to convert from BGR to RGB efficiently?

Back to this post, the example I posted before only works if the resolution of your the Bitmap is a multiple of 4. Apparently .NET Bitmaps usually have padding bytes to make sure the data in memory occupies a multiple of 4 set of images.

When the resolution of the image stored in the Bitmap has

(Width*3) % 4 != 0  

(i.e. the width multiplied by the bytes per pixel, we assume 3 for RGB images, is not a multiple of 4) then using the setFromPixels property we get a skewed image, as the extra padding bytes will not give reliable pixel information but will still be copied to our ofImage.

I’m researching a bit through ofImage and I see it uses FIBITMAP, which we are not supposed to touch by any means. Even though, I don’t see how else I can set a ofImage from a .NET Bitmap. I know I can convert from .NET Bitmap to FIBITMAP. But setting the ofImage from the FIBITMAP is as unclear as doing it from the Bitmap.

From ofImage.h:

		ofImage.h [...]  
// ----------  stuff one shouldn't touch at all ! ------------------  
		FIBITMAP 			* bmp;  
		// when OF users ask for pixels  
		// we will get them contiguous memory  
		// ie, non-aligned  
		// see the pixel access chapter of the freeImage pdf  
		// these pixel will also be RGB  
		// on both platforms  
		// bmp will be RGB / BGR depending  
		// on the endianess of the platform.  
		unsigned char 		* imgPixels;  
		inline bool 		isValid();  
		inline void 		swap(FIBITMAP *dst);  

Any help is appreciated!

I can’t be a ton of help as I haven’t dug into the ofImage code too deeply, but I think you could look at ofImage::getBmpFromPixels() to see how FreeImage works underneath and just subclass ofImage to make something that will allow you to pass in custom bitmap data and still have a functional ofImage. Hope that helps a little.

1 Like