victorporof
30-04-2011 14:02:25
I've been searching everywhere for this, and could not find anything even close to efficient. Best thing was here: MogreImageToBitmap, but I guess having an O(n^2) speed is very bad, especially when someone has to do this every frame.
Long story short, here's the code:
Now let's say you want to convert your RenderWindow contents to a Bitmap:
And with a Mogre Image:
Long story short, here's the code:
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);
public void Handle(PixelBox pixels)
{
unsafe
{
Bitmap bmp = new Bitmap((int)pixels.box.Width, (int)pixels.box.Height, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
CopyMemory(bmpData.Scan0, pixels.data, pixels.box.Width * pixels.box.Height * 3);
bmp.UnlockBits(bmpData);
//do stuff with the bitmap object
//ex: bmp.Save("test.bmp");
}
}
Now let's say you want to convert your RenderWindow contents to a Bitmap:
public void Grab(ref RenderWindow window)
{
PixelFormat pf = PixelFormat.PF_R8G8B8;
var bytes = new byte[(int)(window.Width * window.Height * PixelUtil.GetNumElemBytes(pf))];
unsafe
{
fixed (byte* bytePtr = bytes)
{
var pixels = new PixelBox(window.Width, window.Height, 1, pf, new IntPtr(bytePtr));
window.CopyContentsToMemory(pixels);
Handle(pixels);
}
}
}
And with a Mogre Image:
public void Grab(ref RenderWindow window)
{
PixelFormat pf = PixelFormat.PF_R8G8B8;
var bytes = new byte[(int)(window.Width * window.Height * PixelUtil.GetNumElemBytes(pf))];
unsafe
{
fixed (byte* bytePtr = bytes)
{
var pixels = new PixelBox(window.Width, window.Height, 1, pf, new IntPtr(bytePtr));
window.CopyContentsToMemory(pixels);
using (var image = new Image())
{
image.LoadDynamicImage((byte*)pixels.data.ToPointer(), pixels.box.Width, pixels.box.Height, pf);
Handle(image.GetPixelBox());
}
}
}
}