C生成曲线BMP http://zhidao.baidu.com/question/222913503.html 屏幕截屏 http://topic.csdn.net/u/20120822/13/e52e7b2a-e151-4985-9aa7-934d9f38c8de.html #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include<Windows.h> typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; typedef long LONG; #pragma pack (2) int SaveBitmapToFile(BITMAP *bitmap, LPCWSTR lpFileName,char *lpBuf) { DWORD dwWritten; BITMAPFILEHEADER bmfHdr; BITMAPINFOHEADER bi; HANDLE fh=NULL; bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth= bitmap->bmWidth; bi.biHeight = bitmap->bmHeight; bi.biPlanes = 1; bi.biBitCount = bitmap->bmBitsPixel*8; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (fh == INVALID_HANDLE_VALUE) return FALSE; bmfHdr.bfType = 0x4D42; // "BM" bmfHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+bitmap->bmWidth*bitmap->bmHeight*bitmap->bmBitsPixel; bmfHdr.bfReserved1 = 0; bmfHdr.bfReserved2 = 0; bmfHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER); WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); WriteFile(fh, ( char *)&bi,sizeof (BITMAPINFOHEADER), &dwWritten, NULL); WriteFile(fh, ( char *)lpBuf,bitmap->bmWidth*bitmap->bmHeight*bitmap->bmBitsPixel, &dwWritten, NULL); FlushFileBuffers(fh); CloseHandle(fh); return true; } int GetBitmapFromScreen(LPCWSTR lpFileName) { char *lpBuf; HBITMAP hBitmap,hOld ; HDC hDC,hcDC; BITMAP bb; BITMAPINFO b; HANDLE hp,fh=NULL; DWORD dwX,dwY; dwX=GetSystemMetrics(SM_CXSCREEN); dwY=GetSystemMetrics(SM_CYSCREEN); hDC=GetDC(NULL); hcDC=CreateCompatibleDC(hDC); hBitmap=CreateCompatibleBitmap(hDC,dwX,dwY); hOld=(HBITMAP)SelectObject(hcDC,hBitmap); BitBlt(hcDC,0, 0,dwX,dwY, hDC, 0, 0, SRCCOPY); bb.bmWidth=dwX; bb.bmHeight =dwY; bb.bmPlanes = 1; bb.bmWidthBytes=bb.bmWidth*3; bb.bmBitsPixel=3; bb.bmType=0; b.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); b.bmiHeader.biWidth =dwX; b.bmiHeader.biHeight =dwY; b.bmiHeader.biPlanes =1; b.bmiHeader.biBitCount =3*8; b.bmiHeader.biCompression =BI_RGB; b.bmiHeader.biSizeImage =0; b.bmiHeader.biXPelsPerMeter=0; b.bmiHeader.biYPelsPerMeter=0; b.bmiHeader.biClrUsed =0; b.bmiHeader.biClrImportant =0; b.bmiColors[0].rgbBlue =8; b.bmiColors[0].rgbGreen =8; b.bmiColors[0].rgbRed =8; b.bmiColors[0].rgbReserved =0; hp=GetProcessHeap(); lpBuf=( char *)HeapAlloc(hp,HEAP_ZERO_MEMORY,bb.bmHeight*bb.bmWidth*4); GetDIBits(hcDC,hBitmap,0,dwY,lpBuf,&b,DIB_RGB_COLORS); SaveBitmapToFile(&bb,lpFileName,lpBuf); ReleaseDC(NULL,hDC); DeleteDC(hcDC); DeleteObject(hBitmap); DeleteObject(hOld); HeapFree(hp,0,lpBuf); return true; } int _tmain(int argc, _TCHAR* argv[]) { LPCWSTR path = L "E:\\e.bmp"; GetBitmapFromScreen(path); return 0; }
标签:NULL,bb,bmiHeader,曲线,bi,生成,BMP,fh,bitmap From: https://www.cnblogs.com/roak/p/16937991.html