简单学习了下SDK截图显示到DC和存为文件

 

https://p-blog.csdn.net/images/p_blog_csdn_net/vincent_1011/EntryImages/20090723/TestBitBlt.jpg下载改为rar格式

截图存为文件那个是直接拿别人的代码

 

///

 

笔记。。。。。。。。。。

 

    HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
    HDC dctest    =    ::GetDC(tt.m_hWnd);    //GetDC()如果传NULL的话就是取整个显示器的
    BitBlt(dctest,
        0,0,
        100, 100,
        hdcScreen,
        0,0,
               SRCCOPY);
上面步骤就可以把显示器的的图显示在tt这个窗口的左上100*100的区域。(MFC测试程序)
有个地方要注意,如果显示器的那部分区域给其它程序窗口覆盖的话,也会一并显示在tt这个窗口上,
可以看得出下面的3-4步是为5步的hdcCompatible能装得下1步创建的DC做准备
截图的话,就是以下步骤:
1,创建一个显示器的DC            使用CreateDC("DISPLAY", NULL, NULL, NULL);
2,创建一个兼容显示器的内存DC        使用CreateCompatibleDC(hdcScreen);
3,创建一个兼容显示器的位图对像        使用CreateCompatibleBitmap(1返回值,GetDeviceCaps(hdc, HORZRES),GetDeviceCaps(hdc,

VERTRES));
4,由于2创建的DC还没有GDI对像        使用SelectObject(CompatibleHDC, BmpScreen);把3创建的位图对像选进去,有点像设置

CompatibleHDC这个结构字段的意思
5,这时候就可以把显示器的DC复制到内存的
DC    BitBlt(hdcCompatible,
               0,0,
               bmp.bmWidth, bmp.bmHeight,
               hdcScreen,
               0,0,
               SRCCOPY))

6,这时候图像数据只在内存DC中。用内存DC或者3中返回的位图对像句柄都可以关系到图像信息。要显示的话,可再调用BitBlt把内存DC作为

源复制到需要显示的地方的DC。如果要保存成文件的话,就用3中返回的位图对像

这个只是创建一个位图
HBITMAP CreateCompatibleBitmap(
  HDC hdc,        // handle to DC
  int nWidth,     // width of bitmap, in pixels
  int nHeight     // height of bitmap, in pixels
);

The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device

identified by the hdc parameter. This bitmap can be selected into any memory device context that is compatible with the

original device.

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory

device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color

bitmap, use the hDC that was used to create the memory device context, as shown in the following code:

    HDC memDC = CreateCompatibleDC ( hDC );
    HBITMAP memBM = CreateCompatibleBitmap ( hDC );
    SelectObject ( memDC, memBM );


Memory Device Contexts
A memory DC stores images in memory before sending them to an output device. This allows you to treat a portion of memory as

a virtual device. Create a memory DC for a device by calling the CreateCompatibleDC function and supplying a handle to the DC

of the device. When you call CreateCompatibleDC, Windows CE creates a temporary monochrome bitmap that is 1 pixel by 1 pixel.

Then it selects the bitmap into the DC. Before you begin drawing with this DC, use SelectObject to select a bitmap with the

appropriate width and height into the DC. Then you can use the DC to store images.

Windows CE does not support writable memory DCs. The functionality described here is for use in loading bitmaps and

transferring them to surfaces in DirectDraw.


==========================================================================================================
Capturing an Image
You can use a bitmap to capture an image, and you can store the captured image in memory, display it at a different location

in your application's window, or display it in another window.

In some cases, you may want your application to capture images and store them only temporarily. For example, when you scale

or zoom a picture created in a drawing application, the application must temporarily save the normal view of the image and

display the zoomed view. Later, when the user selects the normal view, the application must replace the zoomed image with a

copy of the normal view that it temporarily saved.

To store an image temporarily, your application must call CreateCompatibleDC to create a DC that is compatible with the

current window DC. After you create a compatible DC, you create a bitmap with the appropriate dimensions by calling the

CreateCompatibleBitmap function and then select it into this device context by calling the SelectObject function.

After the compatible device context is created and the appropriate bitmap has been selected into it, you can capture the

image. The BitBlt function captures images. This function performs a bit block transfer — that is, it copies data from a

source bitmap into a destination bitmap. However, the two arguments to this function are not bitmap handles. Instead, BitBlt

receives handles that identify two device contexts and copies the bitmap data from a bitmap selected into the source DC into

a bitmap selected into the target DC. In this case, the target DC is the compatible DC, so when BitBlt completes the

transfer, the image has been stored in memory. To redisplay the image, call BitBlt a second time, specifying the compatible

DC as the source DC and a window (or printer) DC as the target DC.

The following example code, from an application that captures an image of the entire desktop, creates a compatible device

context and a bitmap with the appropriate dimensions, selects the bitmap into the compatible DC, and then copies the image

using the BitBlt function.

// Create a normal DC and a memory DC for the entire screen. The
// normal DC provides a "snapshot" of the screen contents. The
// memory DC keeps a copy of this "snapshot" in the associated
// bitmap.
 
hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);

//个人理解:为显示器创建一个内存DC
hdcCompatible = CreateCompatibleDC(hdcScreen);
 
// Create a compatible bitmap for hdcScreen.
//个人理解:创建一个兼容hdcScreen的位图,注意:返回值是位图类型的
hbmScreen = CreateCompatibleBitmap(hdcScreen,
                     GetDeviceCaps(hdcScreen, HORZRES), //Width, in millimeters, of the physical screen.
                     GetDeviceCaps(hdcScreen, VERTRES));
 
if (hbmScreen == 0)
    errhandler("hbmScreen", hwnd);
 
// Select the bitmaps into the compatible DC.
//个人理解:给这个内存DC设置像素,这个像素在上面创建hbmScreen的时候指定了,(因为CreateCompatibleDC创建出来的内存DC貌似是默认

//的1 pixel by 1 pixel
//这个SelectObject的作用:specifying the height, width, and color organization required.
//前面也说过DC只是一个数据结构,可以把这个函数想像成给DC结构的各个成员赋值
if (!SelectObject(hdcCompatible, hbmScreen))
    errhandler("Compatible Bitmap Selection", hwnd);
 
        // Hide the application window.
 
        ShowWindow(hwnd, SW_HIDE);
 
         //Copy color data for the entire display into a
         //bitmap that is selected into a compatible DC.
 
        if (!BitBlt(hdcCompatible,
               0,0,
               bmp.bmWidth, bmp.bmHeight,
               hdcScreen,
               0,0,
               SRCCOPY))
 
        errhandler("Screen to Compat Blt Failed", hwnd);
 
        // Redraw the application window.
 
        ShowWindow(hwnd, SW_SHOW);
=========================================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值