MFT screenshot 2 BMP image, black and white

Source: Internet
Author: User
Tags bmp image
Hbitmap copyscreentobitmap (lprect, int m_xscreen, int m_yscreen)
{
HDC hscrdc, hmemdc;
Hbitmap, holdbitmap;
Int Nx1, nx2, NY1, ny2;
Int nwidth, nheight;
If (isrectempty (lprect ))
{
Return false;
}
// Create a device description table for the screen
Hscrdc = createdc ("display", null );
// Create a compatible memory device description table for the screen device description table
Hmemdc = createcompatibledc (hscrdc );
// Obtain the coordinates of the selected region
Nx1 = lprect-> left;
NY1 = lprect-> top;
Nx2 = lprect-> right;
Ny2 = lprect-> bottom;
// Make sure that the selected area is visible
If (Nx1 <0)
Nx1 = 0;
If (NY1 <0)
NY1 = 0;
If (nx2> m_xscreen)
Nx2 = m_xscreen;
If (ny2> m_yscreen)
Ny2 = m_yscreen;
Nwidth = nx2-Nx1;
Nheight = ny2-NY1;
// Create a bitmap compatible with the on-screen device description table
Hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight );
// Select the new bitmap to the memory device description table.
Holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap );
// Copy the screen device description table to the memory device description table.
Bitblt (hmemdc, 0, 0, nwidth, nheight, hscrdc, Nx1, NY1, srccopy );
// Obtain the handle of the screen bitmap.
Hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap );
// Clear
Deletedc (hscrdc );
Deletedc (hmemdc );
Return hbitmap;
}

Bool savebitmaptofile (hbitmap, char * lpfilename)
{
HDC;
// The number of bytes per pixel in the current resolution
Int ibits;
// The number of bytes in the bitmap.
Word wbitcount;
// Defines the color palette size, pixel bytes in the bitmap, bitmap file size, and number of written file bytes.
DWORD dwpalettesize = 0, dwbmbitssize = 0, dwdibsize = 0, dwwritten = 0;
// Bitmap attribute Structure
Bitmap bitmap;
// Bitmap file header Structure
Bitmapfileheader bmfhdr;
// Bitmap header Structure
Bitmapinfoheader Bi;
// Point to the bitmap information header Structure
Lpbitmapinfoheader lpbi;
// Defines the file, allocates memory handles, and color palette handles.
Handle FH, hdib, hpal, holdpal = NULL;

// Calculate the number of bytes occupied by each pixel in a bitmap file
HDC = createdc ("display", 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
Wbitcount = 24;
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. biclrimportant = 0;
Bi. biclrused = 0;
Dwbmbitssize = (bitmap. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight;
// Allocate memory for bitmap content
Hdib = globalalloc (ghnd, dwbmbitssize + dwpalettesize + sizeof (bitmapinfoheader ));
Lpbi = (lpbitmapinfoheader) globallock (hdib );
* Lpbi = Bi;
// Process the palette
Hpal = getstockobject (default_palette );
If (hpal)
{
HDC =: getdc (null );
Holdpal =: selectpalette (HDC, (hpalette) hpal, false );
Realizepalette (HDC );
}

// Obtain the new pixel value under the color palette
Getdibits (HDC, hbitmap, 0, (uint) bitmap. bmheight, (lpstr) lpbi + sizeof (bitmapinfoheader)
+ Dwpalettesize, (bitmapinfo *) lpbi, dib_rgb_colors );
// Restore the palette
If (holdpal)
{
Selectpalette (HDC, (hpalette) holdpal, true );
Realizepalette (HDC );
Releasedc (null, HDC );
}
// Create a 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 the bitmap file header
Bmfhdr. 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 the bitmap file header
Writefile (FH, (lpstr) & bmfhdr, sizeof (bitmapfileheader), & dwwritten, null );
// Write the remaining contents of the bitmap file
Writefile (FH, (lpstr) lpbi, dwdibsize, & dwwritten, null );
// Clear
Globalunlock (hdib );
Globalfree (hdib );
Closehandle (FH );
Return true;

}

 

void cmainframe: onzt ()
{< br> // todo: add the Program Code

Hbitmap g_screenbmp = NULL;
// Obtain the screen pixels in the X direction ()
Int m_xscreen = getsystemmetrics (sm_cxscreen );
// Obtain screen pixels in Y direction ()
Int m_yscreen = getsystemmetrics (sm_cyscreen );
Rect g_qq_dlgrt;
G_qq_dlgrt.left = 0;
G_qq_dlgrt.top = 0;
G_qq_dlgrt.right = m_xscreen;
G_qq_dlgrt.bottom = m_yscreen;
G_screenbmp = copyscreentobitmap (& g_qq_dlgrt, m_xscreen, m_yscreen );
Cstring strtime = ctime: getcurrenttime (). Format ("% Y % m % d % H % m % s ");
Cstring STR = "C :\\" + strtime + ". BMP ";
Lpstr filename = Str. getbuffer ();
Savebitmaptofile (g_screenbmp, filename );

}

 

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.