Save DC content as BMP files, screenshots can also (ZT)

Source: Internet
Author: User

Hbitmap Copyscreentobitmap (lprect lprect)

Lprect represents the selected area

{

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

Make sure the selection is not an empty rectangle

if (Isrectempty (lprect))

return NULL;

Create a Device Description table for the screen

HSCRDC = CreateDC ("DISPLAY", NULL, NULL, NULL);

To create a compatible memory device description table for the Screen device description table

HMEMDC = CreateCompatibleDC (HSCRDC);

Get the selected area coordinates

NX = lprect->left;

NY = lprect->top;

NX2 = lprect->right;

NY2 = lprect->bottom;

Get screen resolution

XSCRN = 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 table

Hbitmap = CreateCompatibleBitmap

(HSCRDC, nwidth, nheight);

Select the new bitmap in 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, NX, NY, srccopy);

Get the handle to the screen bitmap

Hbitmap = (hbitmap) SelectObject (HMEMDC, holdbitmap);//This time you can get the handle to the Savebitmaptofile save to the file, I continue to lazy write here

 //Clear
 bitmapinfoheader BiH;
 bih.bisize =sizeof (Bitmapinfoheader);
 bih.biwidth = 800;
 bih.biheight = 600;
 bih.biplanes = 1;
 bih.bibitcount= 8;//8bit bitmap
 bih.bicompression = Bi_rgb;
 bih.bisizeimage = 0;
 bih.bixpelspermeter = 0;
 bih.biypelspermeter = 0;
 bih.biclrused = 0;
 bih.biclrimportant = 0;
 dword DWBMBITSSIZE=800*600*8/8;
 handle Hdib=globalalloc (ghnd,dwbmbitssize+256*sizeof (Rgbquad) +sizeof (Bitmapinfoheader));//Allocate memory, GlobalAlloc allocating memory can share allocated memory with DLL programs (can be released by non-C + + compiled DLLs), malloc words can only have C + + themselves know how to release the

The assigned content includes Bitmapinfoheader + PALETTE (palette) + bitmap content
LPVOID ptr= (LPVOID) GlobalLock (HDIB);
* (bitmapinfoheader*) ptr=bih;//save that Bitmapinfoheader to the memory we allocate
HDC Xdc=getdc (NULL);
Hpalette hpal= (Hpalette) getstockobject (Default_palette);
Hpalette holdpal= (Hpalette) SelectPalette (Xdc,hpal,false);
RealizePalette (XDC);
if (! GetDIBits (hscrdc,hbitmap,0,600,
(LPSTR) ptr+sizeof (bitmapinfoheader) +256*sizeof (Rgbquad),
(Bitmapinfo *) PTR,
dib_rgb_colors))

The parameters describe the DC from which (must have the correct palette), the bitmap handle, from which line (scanline), how many rows, save the bitmap content, a save bitmapinfo structure (GetDIBits will help us perfect the damned palette), Color of the way, to get BMP must be dib_rgb_colors


{
Return hbitmap;//If GetDIBits returns 0, then it fails ...
}
HANDLE fh = CreateFile ("C://b.bmp", Generic_write,

0, NULL, Create_always,

File_attribute_normal | File_flag_sequential_scan,
NULL);

if (fh = INVALID_HANDLE_VALUE)

return FALSE;

Set Bitmap file Headers
Bitmapfileheader Bmfhdr;
Bmfhdr.bftype = 0x4d42; "BM"

DWORD dwdibsize = sizeof (Bitmapfileheader)

+ sizeof (Bitmapinfoheader)

+ 256*sizeof (rgbquad) + dwbmbitssize;

Bmfhdr.bfsize = dwdibsize;

bmfhdr.bfreserved1 = 0;

Bmfhdr.bfreserved2 = 0;

Bmfhdr.bfoffbits = (DWORD) sizeof

(Bitmapfileheader)

+ (DWORD) sizeof (Bitmapinfoheader)

+ 256*sizeof (Rgbquad);

Write Bitmap file headers
DWORD Dwwritten;
WriteFile (FH, (LPSTR) &BMFHDR, sizeof

(Bitmapfileheader), &dwwritten, NULL);

Write the rest of the bitmap file

WriteFile (FH, (LPSTR) PTR, dwdibsize,

&dwwritten, NULL);

DeleteDC (HSCRDC);

DeleteDC (HMEMDC);

return bitmap handle

return hbitmap;

}

int Savebitmaptofile (Hbitmap hbitmap,

LPSTR lpfilename)//hbitmap for just the screen bitmap handle

{//lpfilename is a bitmap file name

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

Define palette size, pixel byte size in bitmap,

Bitmap file size, number of bytes written to file

DWORD dwpalettesize=0,

Dwbmbitssize,

Dwdibsize, Dwwritten;

BITMAP BITMAP;

Bitmap Property Structure

Bitmapfileheader Bmfhdr;

Bitmap File Header structure

Bitmapinfoheader bi;

Bitmap Information Header structure

Lpbitmapinfoheader LPBI;

Point to bitmap information header structure

HANDLE FH, Hdib;
Hpalette Hpal,holdpal=null;

Definition file, allocating memory handle, palette handle

Calculates 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

Wbitcount = 24;

Calculate Palette Size
wbitcount=8;
if (Wbitcount <= 8)

Dwpalettesize = (1 << wbitcount) *

sizeof (RGBQUAD);

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+31)/32 * 4

*bitmap.bmheight;

allocating memory for bitmap content

Hdib = GlobalAlloc (ghnd,dwbmbitssize+

Dwpalettesize+sizeof (Bitmapinfoheader));

LPBI = (lpbitmapinfoheader) globallock (HDIB);

*LPBI = bi;

Working with palettes

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);

Restore Palette

if (Holdpal)

{

SelectPalette (HDC, 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 Bitmap file Headers

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 Bitmap file headers

WriteFile (FH, (LPSTR) &BMFHDR, sizeof

(Bitmapfileheader), &dwwritten, NULL);

Write the rest of the bitmap file

WriteFile (FH, (LPSTR) LPBI, Dwdibsize,

&dwwritten, NULL);

Clear

GlobalUnlock (HDIB);

GlobalFree (HDIB);

CloseHandle (FH);

}

I am also a half understand the state.

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.