MFC screenshot method

Source: Internet
Author: User

//get the current screen and save the pictureLRESULT cfeetscanview::saveviewbmp (WPARAM WPARAM, LPARAM LPARAM) {CRect rect;  This->getclientrect (&rect); Rect.left= -; Rect.top= the;    CBitmap M_bitmap; M_bitmap.m_hobject=Copyscreentobitmap (rect); CharSzfilename[_max_path]; CharSzpath[_max_path]; Getconfigmgr ()-getmoduledirectory (szpath); sprintf (szFileName,"%s\\image.png", szpath);    Savebitmaptofile (M_bitmap,szfilename); return 0;} Hbitmap Cfeetscanview::copyscreentobitmap (LPRECT LPRECT) {//screen and memory device description tableHDC hscrdc=0, HMEMDC; //Bitmap Handlehbitmap hbitmap, Holdbitmap; //selected area coordinates    intNX, NY, nX2, nY2; //bitmap width and height    intnwidth, nheight; //Screen Resolution    intXscrn, YSCRN; //Make sure the selected range is not empty rectangle//if (Isrectempty (lpRect))//return NULL; //Create a Device description table for the screenHSCRDC = CreateDC ("DISPLAY", NULL, NULL, NULL); //    intok=1; if(Hscrdc==null)//If creation fails{OK=0; }    //Create a compatible memory device description table for the Screen Device description tableHMEMDC =CreateCompatibleDC (HSCRDC); //get selected area coordinatesNX = lprect->Left ; NY= lprect->top; NX2= lprect->Right ; NY2= lprect->Bottom; //Get screen resolutionXSCRN =GetDeviceCaps (HSCRDC, horzres); YSCRN=GetDeviceCaps (HSCRDC, vertres); //Make sure the selected area is visible    if(NX <0) NX=0; if(NY <0) NY=0; if(NX2 >xscrn) nX2=Xscrn; if(NY2 >yscrn) nY2=Yscrn; Nwidth= NX2-NX; Nheight= NY2-NY; //Create a bitmap that is compatible with the Screen Device description tableHbitmap =CreateCompatibleBitmap (HSCRDC, nwidth, nheight); //Select the new bitmap in the Memory Device description tableHoldbitmap =(HBITMAP) SelectObject (HMEMDC, HBITMAP); //Copy the screen Device description table to the Memory Device description tableBitBlt (HMEMDC,0,0, nwidth, NHEIGHT,HSCRDC, NX, NY, srccopy); //get a handle to the screen bitmapHbitmap =(HBITMAP) SelectObject (HMEMDC, Holdbitmap); //ClearDeleteDC (HSCRDC);         DeleteDC (HMEMDC); //returns a bitmap handle    returnHbitmap;} BOOL Cfeetscanview::savebitmaptofile (hbitmap hbitmap, LPCTSTR lpfilename) {hdc hdc;//Device Description Table    intIbits;//the number of bytes per pixel at the current display resolutionWORD Wbitcount;//the number of bytes per pixel in the bitmapDWORD dwpalettesize=0,//Defines the size of the palette, the pixel byte size in the bitmap, the bitmap file size, the number of bytes written to the filedwbmbitssize, Dwdibsize, Dwwritten; BITMAP BITMAP; //Bitmap Property StructureBitmapfileheader Bmfhdr;//Bitmap file header structureBitmapinfoheader bi;//Bitmap Information Header structureLpbitmapinfoheader LPBI;//point to bitmap information header structureHANDLE FH, Hdib, Hpal,holdpal=null;//define file, allocate memory handle, palette handle//calculate the number of bytes per pixel in a bitmap fileHDC HWNDDC = CreateDC ("DISPLAY", Null,null,null); HDC=:: CreateCompatibleDC (HWNDDC); Ibits= GetDeviceCaps (HDC, Bitspixel) *GetDeviceCaps (HDC, PLANES);     DeleteDC (HDC); if(Ibits <=1) Wbitcount=1; Else if(Ibits <=4) Wbitcount=4; Else if(Ibits <=8) Wbitcount=8; Else if(Ibits <= -) Wbitcount= -; ElseWbitcount= - ; //Calculate palette Size    if(Wbitcount <=8) Dwpalettesize= (1<< wbitcount) *sizeof(Rgbquad); //set Bitmap information header structureGetObject (HBITMAP,sizeof(BITMAP), (LPSTR) &Bitmap); Bi.bisize=sizeof(Bitmapinfoheader); Bi.biwidth=Bitmap.bmwidth; Bi.biheight=Bitmap.bmheight; Bi.biplanes=1; Bi.bibitcount=Wbitcount; Bi.bicompression=Bi_rgb; Bi.bisizeimage=0; Bi.bixpelspermeter=0; Bi.biypelspermeter=0; Bi.biclrused=0; Bi.biclrimportant=0; Dwbmbitssize= ((Bitmap.bmwidth * wbitcount+ to)/ +) *4*Bitmap.bmheight; //allocating memory for bitmap contentHdib = GlobalAlloc (ghnd,dwbmbitssize+dwpalettesize+sizeof(Bitmapinfoheader)); LPBI=(Lpbitmapinfoheader) GlobalLock (HDIB); *LPBI =bi; //working with color palettesHPal =Getstockobject (Default_palette); if(hPal) {HDC=:: GetDC (NULL); Holdpal=:: SelectPalette (HDC, (Hpalette) HPal, FALSE);     RealizePalette (HDC); }     //gets the new pixel value under the paletteGetDIBits (HDC, Hbitmap,0, (UINT) Bitmap.bmheight, (LPSTR) LPBI+sizeof(Bitmapinfoheader)+dwpalettesize, (lpbitmapinfo) LPBI, dib_rgb_colors); //Restore Palette    if(Holdpal) {SelectPalette (HDC, (Hpalette) Holdpal, TRUE);         RealizePalette (HDC);     :: ReleaseDC (NULL, HDC); }     //Create a bitmap fileFH =CreateFile (lpFileName, Generic_write,0, NULL, Create_always, File_attribute_normal|File_flag_sequential_scan, NULL); if(FH = =Invalid_handle_value)returnFALSE; //Set Bitmap file headerBmfhdr.bftype =0x4d42;//"BM"Dwdibsize =sizeof(Bitmapfileheader)+sizeof(Bitmapinfoheader)+ Dwpalettesize +dwbmbitssize; Bmfhdr.bfsize=dwdibsize; Bmfhdr.bfreserved1=0; Bmfhdr.bfreserved2=0; Bmfhdr.bfoffbits= (DWORD)sizeof(Bitmapfileheader)+ (DWORD)sizeof(Bitmapinfoheader)+dwpalettesize; //write to bitmap file headerWriteFile (FH, (LPSTR) &AMP;BMFHDR,sizeof(Bitmapfileheader), &dwwritten, NULL); //write the remaining contents of the bitmap fileWriteFile (FH, (LPSTR) LPBI, Dwdibsize,&dwwritten, NULL); //ClearGlobalUnlock (HDIB);     GlobalFree (HDIB);     CloseHandle (FH); returnTRUE;}//90-degree display of the current picturecdc* pDC1 =M_left_pic.    GetDC (); Graphics Graph (pDC1-GETSAFEHDC ());    CRect rect; M_left_pic.    GetClientRect (rect); CharSzfilename[_max_path]; CharSzpath[_max_path]; Getconfigmgr ()-getmoduledirectory (szpath); sprintf (szFileName,"%s\\image.png", szpath);    WCHAR Wszwidestring[_max_path];    wcscpy (wszwidestring, CT2CW (szFileName));    Image Image (wszwidestring); REAL width=(REAL) image.    GetWidth (); REAL Height=(REAL) image.    GetHeight (); intNX =0; intNY =0; Point point90[]={point (NX+height,ny), point (NX+ height,ny+width), point (Nx,ny)}; Graph. DrawImage (&image, Point90,3); ReleaseDC (pDC1);

MFC Screen Methods

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.