[00011]-[2015-08-26]-[00]-[windows Programming---MFC screenshot--->bmp format--->jpg format]

Source: Internet
Author: User

Hbitmap Copyscreentobitmap (LPRECT LPRECT)
{
HDC HSCRDC, hmemdc;//screen and memory device description table
Hbitmap hbitmap, holdbitmap;//bitmap handle
int NX, NY, nX2, ny2;//selected area coordinates
int nwidth, nheight;//bitmap width and height
int XSCRN, yscrn;//screen resolution

if (Isrectempty (LpRect))
return NULL;

HSCRDC = CreateDC ("DISPLAY", NULL, NULL, NULL);//Create a Device Description table for the screen

HMEMDC = CreateCompatibleDC (HSCRDC);//Create a compatible memory device description table for the Screen device description table

NX = lprect->left;
NY = lprect->top;
NX2 = lprect->right;
NY2 = lprect->bottom;

XSCRN = GetDeviceCaps (HSCRDC, horzres);//Get Screen horizontal Resolution
YSCRN = GetDeviceCaps (HSCRDC, vertres);

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;

Hbitmap = CreateCompatibleBitmap (HSCRDC, nwidth, nheight);//Create a bitmap that is compatible with the Screen Device description table
Holdbitmap = (hbitmap) SelectObject (HMEMDC, HBITMAP);//Select the new bitmap in the Memory Device description table
BitBlt (HMEMDC, 0, 0, nwidth, nheight, HSCRDC, NX, NY, srccopy);//Copy the screen Device description table to the Memory Device description table
Hbitmap = (hbitmap) SelectObject (HMEMDC, holdbitmap);//Get a handle to the screen bitmap

DeleteDC (HSCRDC);
DeleteDC (HMEMDC);

return hbitmap;
}

/*
lpFileName: Bitmap file name
*/
BOOL Savebitmaptofile (hbitmap hbitmap, LPSTR lpfilename)
{
HDC hdc;//Device Description Table

int ibits;//The number of bytes per pixel at the current display resolution
WORD wbitcount;//the number of bytes per pixel in the bitmap
DWORD dwpalettesize = 0, dwbmbitssize, dwdibsize, dwwritten;//palette size, bitmap data size, bitmap file size, number of bytes written to file
BITMAP bitmap;//Bitmap Property structure
Bitmapfileheader bmfhdr;//Bitmap File header
Bitmapinfoheader bi;//Bitmap Information header
Lpbitmapinfoheader lpbi;//pointing to bitmap information header structure

HANDLE FH, hdib;//definition file, allocating memory handle
Hpalette HPal, holdpal=null;//palette handle

Calculate the number of bytes per pixel in a bitmap file
HDC = CreateDC ("DISPLAY", NULL, NULL, NULL);
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 <= 24)
Wbitcount = 24;
Else
Wbitcount = 32;
if (Wbitcount <= 8)
Dwpalettesize = (1 << wbitcount) * sizeof (RGBQUAD);//Calculate palette size

Set Bitmap information header structure
GetObject (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 + +)/+) * 4 * bitmap.bmheight;

Hdib = GlobalAlloc (ghnd, dwbmbitssize + dwpalettesize + sizeof (bitmapinfoheader));//Allocate memory for bitmap content
Lpbi = ( Lpbitmapinfoheader) GlobalLock (HDIB);
*LPBI = bi;
//Processing palette
HPal = (hpalette) getstockobject (Default_palette);
if (HPal)
{
HDC = GetDC (NULL);
Holdpal = SelectPalette (HDC, HPal, FALSE);
RealizePalette (HDC);
}
//Gets the new pixel value under the palette
GetDIBits (HDC, Hbitmap, 0, (UINT) Bitmap.bmheight, (LPSTR) LPBI + sizeof (bitmapinfoheader) + Dwpalettesize, (bitmapinfo*) LPBI, dib_rgb_colors);

if (holdpal)//Restore palette
{
SelectPalette (HDC, Holdpal, TRUE);
RealizePalette (HDC);
ReleaseDC (NULL, HDC);
}
//create bitmap file
fh = CreateFile (lpfilename, generic_write, 0, NULL, create_always, File_attribute_normal | File_flag_sequential_scan, NULL);
if (fh = = INVALID_HANDLE_VALUE)
return FALSE;

Set Bitmap file Header
Bmfhdr.bftype = 0x4d42;//File type: "BM"
dwdibsize = sizeof (Bitmapfileheader) + sizeof (Bitmapinfoheader) + dwpalettesize + dwbmbitssize;
bmfhdr.bfsize = dwdibsize;//bitmap File size
bmfhdr.bfreserved1 = 0;
Bmfhdr.bfreserved2 = 0;
Bmfhdr.bfoffbits = (DWORD) sizeof (Bitmapfileheader) + (DWORD) sizeof (Bitmapinfoheader) + dwpalettesize;

WriteFile (FH, (LPSTR) &bmfhdr, sizeof (Bitmapfileheader), &dwwritten, NULL);//write Bitmap file header
WriteFile (FH, (LPSTR) LPBI, Dwdibsize, &dwwritten, NULL);//write the remaining contents of the bitmap file

GlobalUnlock (HDIB);
GlobalFree (HDIB);
CloseHandle (FH);

return TRUE;
}

// ------------------------------------------------------------------------------------------------------------- ------------------- //

void Cmydlg::onbutton1 ()
{
Hbitmap Hbitmap, LPSTR lpfilename
Rect rect = {0, 0, 1900, 1000};
Hbitmap hbmp = Copyscreentobitmap (&rect);
Savebitmaptofile (hbmp, "a.bmp");
MessageBox ("Save bmp file Successful");
}

int Getencoderclsid (const wchar* format, clsid* pClsid)
{
UINT num = 0; Number of Image encoders
UINT size = 0; Size of the image encoder array in bytes
imagecodecinfo* pimagecodecinfo = NULL;
Getimageencoderssize (&num, &size);
if (size = = 0)
return-1; Failure

Pimagecodecinfo = (imagecodecinfo*) (malloc (size));
if (Pimagecodecinfo = = NULL)
return-1; Failure

Getimageencoders (num, size, pimagecodecinfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp (PIMAGECODECINFO[J). MimeType, format) = = 0)
{
*pclsid = Pimagecodecinfo[j]. Clsid;
Free (pimagecodecinfo);
Return J; Success
}
}
Free (pimagecodecinfo);
return-1; Failure
}

void Savecurscreenjpg (LPCWSTR pszfilename, int xs, int ys, int quality)
{
HWND hwnd =:: GetDesktopWindow ();
HDC hdc = GETWINDOWDC (NULL);
int x = GetDeviceCaps (hdc, horzres);
int y = GetDeviceCaps (hdc, vertres);
Hbitmap hbmp =:: CreateCompatibleBitmap (hdc, x, y), hold;
HDC HMEMDC =:: CreateCompatibleDC (HDC);
Hold = (HBITMAP):: SelectObject (HMEMDC, hbmp);
BitBlt (HMEMDC, 0, 0, x, y, hdc, 0, 0, srccopy);
SelectObject (HMEMDC, hold);

Bitmap bit (XS, YS), Bit2 (hbmp, NULL);
Graphics g (&bit);
G.scaletransform (float) xs/x, (float) ys/y);
G.drawimage (&bit2, 0, 0);

CLSID Encoderclsid;
EncoderParameters encoderparameters;

Encoderparameters.count = 1;
Encoderparameters.parameter[0]. Guid = encoderquality;
Encoderparameters.parameter[0]. Type = Encoderparametervaluetypelong;
Encoderparameters.parameter[0]. NumberOfValues = 1;
Encoderparameters.parameter[0]. Value = &quality;

Getencoderclsid (L "Image/jpeg", &encoderclsid);
Bit. Save (pszFileName, &encoderclsid, &encoderparameters);

::D eleteobject (hbmp);
::D eleteobject (HMEMDC);
Return
}

// ------------------------------------------------------------------------------------------------------------- ------------------- //

Support for GDI + libraries is required for screenshot saving to JPG format

void Cmydlg::onbutton2 ()
{
Todo:add your control notification handler code here
Savecurscreenjpg (L "1.jpg", 1440, 900, 100);
}

[00011]-[2015-08-26]-[00]-[windows Programming---MFC screenshot--->bmp format--->jpg format]

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.