Make a screenshot

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Kaki
Posts: 41
Joined: Thu Oct 12, 2006 12:19 pm
Location: France

Make a screenshot

Post 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
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Kaki
Posts: 41
Joined: Thu Oct 12, 2006 12:19 pm
Location: France

Post by Kaki »

I use the createScreenShot function :

Code: Select all

IImage* myImage = driver->createScreenShot();

Now, can you tell me how to save myImage ?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Kaki
Posts: 41
Joined: Thu Oct 12, 2006 12:19 pm
Location: France

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post 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);
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

hehe...that is exactly what i wantet....thanks a lot
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

Hi! Im using version 1.2.
Athlon_Jedi
Posts: 156
Joined: Wed Jul 21, 2004 4:29 am
Location: Mishawaka, In

easy fix maybe

Post 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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post 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.
Post Reply