/Save the BMP bitmap file from the background of the window handle and display it on the picture controlvoidGetscreenbmp (HWND hwnd,intLeftintTopintWidthintHeight, char*path) {HDC PDC;//Source DC//It is not possible to use GETDC to get the DC if it is a window handle or it will be a black screen . if(hwnd==:: GetDesktopWindow ()) {PDC= CREATEDCA ("DISPLAY", NULL, NULL, NULL); } Else{PDC=:: GetDC (HWND);//Get screen DC (0 is full screen, handle is window) } intBitperpixel =:: GetDeviceCaps (PDC, Bitspixel);//Get color Mode if(Width = =0&& height = =0)//default width and height for fullscreen{width=:: GetDeviceCaps (PDC, horzres);//Set Image width fullscreenHeight =:: GetDeviceCaps (PDC, vertres);//set Image height fullscreen} HDC MEMDC;//Memory DCMEMDC =:: CreateCompatibleDC (PDC); Hbitmap Membitmap, Oldmembitmap;//Build and screen-compatible bitmapMembitmap =:: CreateCompatibleBitmap (PDC, width, height); Oldmembitmap= (HBITMAP):: SelectObject (MEMDC, Membitmap);//Select the Membitmap into the memory DC if(hwnd==:: GetDesktopWindow ()) {BitBlt (MEMDC,0,0, width, height, PDC, left, top, srccopy);//Image width height and intercept position } Else { BOOLBret =::P Rintwindow (hwnd, MEMDC, pw_clientonly); if(!Bret) {BitBlt (MEMDC,0,0, width, height, PDC, left, top, srccopy);//Image width height and intercept position } } //The following code saves the bitmap in MEMDC to the fileBITMAP bmp; :: GetObject (Membitmap,sizeof(BITMAP), &bmp);;//Get bitmap InformationFILE *FP; Fopen_s (&FP, Path,"w+b");//picture save path and wayBitmapinfoheader BiH= {0};//Bitmap Information HeaderBih.bibitcount = Bmp.bmbitspixel;//byte size per pixelBih.bicompression =Bi_rgb; Bih.biheight= Bmp.bmheight;//HeightBih.biplanes =1; Bih.bisize=sizeof(Bitmapinfoheader); Bih.bisizeimage= Bmp.bmwidthbytes * Bmp.bmheight;//image Data SizeBih.biwidth = Bmp.bmwidth;//widthBitmapfileheader BFH= {0};//Bitmap File HeaderBfh.bfoffbits =sizeof(Bitmapfileheader) +sizeof(Bitmapinfoheader);//offset of bitmap dataBfh.bfsize = bfh.bfoffbits + bmp.bmwidthbytes * bmp.bmheight;//total size of fileBfh.bftype = (WORD)0x4d42; Fwrite (&BFH,1,sizeof(Bitmapfileheader), FP);//write to bitmap file headerFwrite (&bih,1,sizeof(Bitmapinfoheader), FP);//Write bitmap information header byte* p =New byte[Bmp.bmwidthbytes * bmp.bmheight];//request memory to save bitmap dataGetDIBits (MEMDC, (HBITMAP) Membitmap,0, Height, p, (lpbitmapinfo)&bih, dib_rgb_colors);//Get Bitmap DataFwrite (P,1, Bmp.bmwidthbytes * bmp.bmheight, FP);//Write bitmap Data Delete[] p; Fclose (FP); HWND Sbithwnd=GetDlgItem (G_hwnd, idc_static_img); /*returns the in-memory bitmap handle restores the original memory DC bitmap handle cannot be used directly with Membitmap I'm testing it.*/hbitmap oleimage=(HBITMAP):: SelectObject (MEMDC, Oldmembitmap); Oleimage=(HBITMAP) SendMessage (Sbithwnd, Stm_setimage, Image_bitmap, (LPARAM) oleimage);#if0/*This method can also display bitmaps to the picture control*/HDC BITDC=NULL; BITDC=:: GetDC (Sbithwnd); BitBlt (BITDC,0,0, Bmp.bmwidth, Bmp.bmheight, MEMDC,0,0, srccopy);//memory DC mapped to screen DCReleaseDC (Sbithwnd, BITDC); /*If you need to convert the bitmap*/ /*CImage image; Image. Create (nwidth, nheight, Nbitperpixel); BitBlt (image. GetDC (), 0, 0, nwidth, nheight, hdcsrc, 0, 0, srccopy); :: ReleaseDC (NULL, HDCSRC); Image. ReleaseDC (); Image. Save (path, gdiplus::imageformatpng);//imageformatjpeg*/#endifDeleteObject (MEMBITMAP); DeleteObject (Oleimage); DeleteDC (MEMDC); ReleaseDC (HWND,PDC);}
The C + + screen specifies that the window handle returns a bitmap handle in the background