System.Drawing.Bitmap -> Mogre.Image


13-07-2008 12:15:19


In first time sorry for my poor English ...

I'm trying to load a Bitmap using this code :

// Image_Source is a System.Drawing.Bitmap
// Image_Source.PixelFormat = Format24bppRgb

System.IO.MemoryStream Str_Data = new System.IO.MemoryStream();

Image_Source.Save(Str_Data, System.Drawing.Imaging.ImageFormat.Png);

Mogre.ManagedDataStream mds = new Mogre.ManagedDataStream(Str_Data);

Mogre.DataStreamPtr dsptr = new Mogre.DataStreamPtr(mds);
Mogre.Image img = new Mogre.Image();

// --> pbm 1
img.Load(dsptr, "png");


// --> pbm 2
img.LoadRawData(dsptr, (uint)Image_Source.Width , (uint)Image_Source.Height, Mogre.PixelFormat.PF_R8G8B8);

pbm 1 generate an error :
OGRE EXCEPTION(7:InternalErrorException): Error decoding image in FreeImageCodec::decode at ..\src\OgreFreeImageCodec.cpp (line 343)

pbm 2 generate an other error :
OGRE EXCEPTION(2:InvalidParametersException): Stream size does not match calculated image size in Image::loadRawData at ..\src\OgreImage.cpp (line 286)

Someone could say where i'm wrong ?
Thanks a lot.


22-07-2008 14:18:48

iirc i posted something like that some time ago... in context of a video player i guess...

so what i did is basicly access the data of the bitmap using bimtapdata and scan0 (should be a IntPtr iirc) and blit the contents to the texture using the blit functions of mogre


23-07-2008 01:32:38

This is Mogre.Image -> System.Drawing.Bitmap, but just reverse the code and it will work.

It's brute force, but I couldn't find any other way. No amount of copying memory, pinning pointers or waving wands would make it work without doing this.

public static Bitmap MogreImageToBitmap(Mogre.Image img) {
Bitmap bmp = new Bitmap ((int)img.Width, (int)img.Height);
for (int x = 0; x < bmp.Width; x++) {
for (int y = 0; y < bmp.Height; y++) {
Mogre.ColourValue color = img.GetColourAt (x, y, 0);
bmp.SetPixel (x, y, Color.FromArgb ((int)color.GetAsARGB ()));

return bmp;