Page 1 of 1

Make a screenshot

Posted: Mon Jan 22, 2007 4:30 pm
by Kaki
Hi everybody

Well, I want to make a screenshot of the scene and save it in an image file (.bmp or .png). I don't know how I can do that.

I tried this code but it doens't save the image :

Code: Select all

SColor tmpColors[4]; 
SColor tColor(100,255,255,255); 
tmpColors[0] = tColor; 
tmpColors[1] = tColor; 
tmpColors[2] = tColor; 
tmpColors[3] = tColor; 
ITexture  *images = driver->getTexture("./monimage.bmp"); 
driver->draw2DImage( 				images, 
                rect<s32>(0,0,342,224), 
	rect<s32>(0,0,342,224), 
	0, 
	tmpColors, 
	true
               ); 	
Can someone help me ? Thanks in advance

Posted: Mon Jan 22, 2007 4:35 pm
by hybrid
You should render the scene and call createScreenShot which will give you an image of the scene. The image can be saved using the image writers. Check the demo code for the actual code required. Or read the API docs.

Posted: Mon Jan 22, 2007 4:50 pm
by Kaki
I use the createScreenShot function :

Code: Select all

IImage* myImage = driver->createScreenShot();

Now, can you tell me how to save myImage ?

Posted: Mon Jan 22, 2007 5:00 pm
by vitek
Hybrid already told you where to look...
Check the demo code for the actual code required. Or read the API docs.
Why haven't you looked there? You've already downloaded the code, all you have to do is open it up in a text editor and have a look at it.

Regardless, you can write the image out by calling driver->writeImageToFile(myImage, "screenshot.jpg");.

Travis

Posted: Mon Jan 22, 2007 5:20 pm
by Kaki
Check the demo code for the actual code required
I don't see what it is.

I use that :

Code: Select all

driver->writeImageToFile(myImage, "screenshot.jpg"
It works fine. Thanks Travis.

Posted: Mon Jan 22, 2007 5:42 pm
by vitek
There is a demo application that comes as part of the Irrlicht SDK download. You just need to look at the source code for the demo. It has code that does lots of stuff that users frequently ask about.

I'm glad you got it working.

Travis

Posted: Mon Jan 22, 2007 9:31 pm
by c.h.r.i.s
Hi! I have another Question about screenshots:

Why the gui is visible on the picture? I think i have made it invisible before taking the screenshot...

IGUIElement* thegui = guienv->getRootGUIElement();
thegui->setVisible(false);
IImage* myImage = driver->createScreenShot();
driver->writeImageToFile(myImage, "photo.bmp");
thegui->setVisible(true);

Posted: Mon Jan 22, 2007 10:15 pm
by greenya
c.h.r.i.s wrote: Why the gui is visible on the picture? I think i have made it invisible before taking the screenshot...

IGUIElement* thegui = guienv->getRootGUIElement();
thegui->setVisible(false);
IImage* myImage = driver->createScreenShot();
driver->writeImageToFile(myImage, "photo.bmp");
thegui->setVisible(true);
You should to redraw whole the scene before screenshoting for all setVisible()s take effect i guess.

Posted: Mon Jan 22, 2007 11:51 pm
by vitek
By definition a screenshot includes everything that you see on the screen. The screenshot is actually a capture of the back or front buffer, so if you draw the gui then you will see the gui in the screenshot.

And greenya is right. You need to render the entire scene and then take your screenshot. If you just use the scene manager to render stuff, then that is pretty easy.

Code: Select all

void takePhotoOfScene(IVideoDriver* driver, ISceneManager* smgr, const char* fileName)
{
  // redraw the entire scene
  if (driver->beginScene(true, true, video::SColor(255, 0, 0, 0)))
  {
    smgr->drawAll();

    driver->endScene();
  }

  // take the screenshot
  IImage* photo = driver->createScreenShot();
  if (photo)
  {
    driver->writeImageToFile(photo, fileName);
    photo->drop();
  }
}
Unfortunately this might cause the screen to appear for a second without the GUI. This may be okay for your application. If it is not, I suggest you implement a render to texture solution and then just write that texture out using the writeImageToFile() function.

Travis

Posted: Tue Feb 13, 2007 11:42 am
by c.h.r.i.s
hehe...that is exactly what i wantet....thanks a lot

Posted: Tue Feb 13, 2007 11:58 am
by c.h.r.i.s
i think i have found a bug.

i use this photo function in a non fullscreen window. it works very good. I can resize the window and takeing photos without any problem. But if i click on the maximize button and then on my photo button the application breaks whith following message: An unhandled exception of type 'System.AccessViolationException' occurred in blabla.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Posted: Tue Feb 13, 2007 5:28 pm
by vitek
What version? There was a bug that would cause a crash. I'm not exactly sure what it was, but it was fixed before Irrlicht 1.2.

Travis

Posted: Wed Feb 14, 2007 8:59 am
by c.h.r.i.s
Hi! Im using version 1.2.

easy fix maybe

Posted: Wed Feb 14, 2007 2:29 pm
by Athlon_Jedi
im thinking the problem is comming from the fact that the shot will be made to match what ever reslution and screen startup settings its originaly coded for .

i.e . if you have your app set to go windowed it will work in that mode only unless you add a if statement or possibly a case to define the different modes you want it to work in.

maybe something like this:

psudocode

Code: Select all


if (device == fullscreen)
{
//code goes here
}
else if ( device == windowed)
{
//more code here
}
end if

might be way off base here but thats what im thinkin

Posted: Wed Feb 14, 2007 9:14 pm
by c.h.r.i.s
Hi! I mean the maximize button of the engine window and not the "pure" fullscreen mode. If the window is maximized that error will occure. In fullscreen mode it works perfect.