Using DirectX screenshot

Source: Internet
Author: User

There are many online DirectX screenshots of the article, but most of the screen, very few windows, this article is related to both, first of all, how to intercept the entire screen, and then how to intercept a window, in fact, the difference is not very small, just a parameter setting is different, and finally we will extend to any region.

First of all, take a look at the function of the screenshot, the core of course is d3dxsavesurfacetofile, first look at the function prototype

1 HRESULT d3dxsavesurfacetofile (
2 LPCTSTR Pdestfile,
3 D3dximage_fileformat Destformat,
4 Lpdirect3dsurface9 Psrcsurface,
5 CONST paletteentry * Psrcpalette,
6 CONST RECT * psrcrect
7);

The first parameter is a pointer to the device, not much.

The second parameter is the type of file, supported by a number of types, mainly the following

Bmp,jpg,tga,png,dds,ppm,dib,hdr,pfm

Here we use the bmp-of the throne chart format

The third parameter is a pointer to surface, which is the surface that holds the data

The fourth parameter is the palette for surface, which is not used, and is set to NULL

The last parameter is the rectangular area of surface, where we can intercept only the data of a rectangular area on surface, and the difference between capturing a full screen and capturing a window is on the setting of this parameter.

The other functions are explained below.

Now to define our screenshot function, first we need a device pointer, because in the DX, any operation is closely related to the device, so the device pointer is almost every DX function to use the parameter, we this function is no exception, the next need a window handle, when we intercept the window, the window handle passed in, When we intercept the entire screen, we pass in null directly. Finally we need a string parameter to specify the corresponding file name, as follows

1 BOOL screenshot (Lpdirect3ddevice9 Lpdevice, HWND hwnd, tchar* FileName)

Detailed steps:

First we need to get the display mode, notice here is the display mode of the video card, not the display mode of the device, because the display mode of the device is both windowed mode and full screen mode, so its resolution is indeterminate, and the display mode of the video card is always the maximum resolution, We need to create the entire screen area corresponding to the surface, when the entire screen, the direct save, when the window is intercepted, we will save the window corresponding to the area

The code to get the video display mode is as follows

1 HRESULT HR;
2
3//Get Adapter display mode
4 D3ddisplaymode mode;
5 if (FAILED (hr = Lpdevice->getdisplaymode (0, &mode)))
6 return HR;
7

The following begins the creation of the surface, which corresponds to the entire screen

Code

Next get the data for the screen, which is actually copying the data from the memory into the system memory

1//Get the screen data
2 if (FAILED (hr = lpdevice->getfrontbufferdata (0, surf)))
3 {
4 Surf->release ();
5 return HR;
6}
7

Next we decide whether to intercept the window or intercept the screen, it is very simple, just determine if the HWND is NULL, if it is a window to set the window corresponding to the rectangular area can be

Code

Last step, Save!

1//Save the screen date to file
2 hr = D3dxsavesurfacetofile (FileName, d3dxiff_bmp, surf, NULL, rect);
3
4 Surf->release ();
5
6 return HR;
7

Full code

Code

So how to achieve arbitrary area screenshots, I think we have thought of, assuming the use of mouse drag and drop, note the mouse down and lift the coordinates, construct a rect, and then pass to the D3dxsavesurfacetofile function on it, you need to note that, Since the mouse drag to the direction is arbitrary, so in the construction of the RECT should pay attention to the right < left or bottom < top case, the following method can be processed

Code

Happy coding!

==the end==

Reprinted from: http://www.cnblogs.com/graphics/archive/2009/11/25/1610914.html

Using DirectX screenshot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.