RenderWindow CopyContentsToMemory AccessViolationExp[SOLVED]

AliAkdurak

14-12-2010 09:50:46

Hello everyone

While trying to do printing function for our result screens I encountered a problem while trying to get a Image from the Render Windows vision. My Code goes like this:


public void CopyContentsToImage()
{
PixelBox pixels = new PixelBox(RenderWindow.Width, RenderWindow.Height, 1, RenderWindow.SuggestPixelFormat());
RenderWindow.CopyContentsToMemory(pixels);
unsafe
{
Result.LoadDynamicImage((byte*)pixels.data.ToPointer(), pixels.box.Width, pixels.box.Height, RenderWindow.SuggestPixelFormat());
}
ImageLoadDynamicSyncSemaphore.Release();
}


Now this function is only called by the same thread which renders the scene. This is called by the thread from the frame started event. But it still gives an AccessViolationException. I thought it may be because the render targets contents are untouchable while in render but did not seems so now. Any Idea's where I should investigate.

RenderWindow CopyContentsToMemory AccessViolationException

Edit: Forgot to mention that Ogre.log say nothing about this exception as it is not an ogre exception hence why I think it may be because I call it with something inappropriate.

smiley80

14-12-2010 10:38:23

You have to allocate some memory before you can do that:
PixelFormat pf = this.window.SuggestPixelFormat();
var bytes = new byte[(int)(this.window.Width * this.window.Height * PixelUtil.GetNumElemBytes(pf))];
unsafe
{
fixed (byte* bytePtr = bytes)
{
var pixels = new PixelBox(this.window.Width, this.window.Height, 1, pf, new IntPtr(bytePtr));
this.window.CopyContentsToMemory(pixels);
using (var image = new Image())
{
image.LoadDynamicImage((byte*)pixels.data.ToPointer(), pixels.box.Width, pixels.box.Height, pf);
// do stuff with image
}
}
}

AliAkdurak

14-12-2010 12:27:27

Thank you it works perfectly.