當前位置:編程學習大全網 - 源碼下載 - C語言運行出全屏的的圖像怎樣截取?

C語言運行出全屏的的圖像怎樣截取?

MS VC++ 的 c 程序可以實現。

方法:

(1)用鍵盤按鍵程序模擬法,把圖像發送到clipboard

(2)把clipboard圖像存入.bmp 圖像文件(或別的格式)。

編譯:

cl simu_keyboard.c user32.lib Gdi32.lib

特殊頭文件:

#include <Windows.h>

#include <Winuser.h>

#include <memory.h>

提示:

(1)按鍵程序模擬子程序:

void snapscreen_2_clipboard()

{

keybd_event(VK_SNAPSHOT,0x2C,0,0);

keybd_event(VK_SNAPSHOT,0x2C,KEYEVENTF_KEYUP,0);

}

(2)clipboard圖像存入.bmp 圖像文件

FILE *fout;

/* --------------------------------------------------------------

* dib

int GetBytesPerPixel(int depth);

int GetBytesPerRow(int width, int depth);

int GetBitmapBytes(int width, int height, int depth);

* --------------------------------------------------------------*/

int GetBytesPerPixel(int depth)

{ return (depth==32 ? 4 : 3);

}

int GetBytesPerRow(int width, int depth)

{

int bytesPerPixel = GetBytesPerPixel(depth);

int bytesPerRow = ((width * bytesPerPixel + 3) & ~3);

return bytesPerRow;

}

// bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, bmi.bmiHeader.biBitCount

int GetBitmapBytes(int width, int height, int depth)

{

return height * GetBytesPerRow(width, depth);

}

void save_clipboard_img_to_bmp()

{

char nameout[80];

HANDLE h_bitmap,h_dib;

BITMAPINFO bmi;

HDC hDC;

int imageBytes;

BITMAPFILEHEADER hdr;

int scanLineCount;

unsigned char *img;

if (!OpenClipboard(NULL)) {

printf("Can not open clipboard\n");

exit(0);

};

if (DEBUG ==1) printf("pass open clipboard\n");

// HANDLE GetClipboardData(UINT uFormat);

h_bitmap = GetClipboardData(CF_BITMAP);

h_dib = GetClipboardData(CF_DIB);

if (h_bitmap ==NULL || h_dib ==NULL){

printf("I got NULL bitmap: ");

} else { printf("I got bitmap: ");};

memcpy(&bmi,h_dib,sizeof(bmi));

printf("%d x %d \n",bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight);

hDC = CreateCompatibleDC(NULL); // Gdi32.lib.

CloseClipboard();

bmi.bmiHeader.biCompression = BI_RGB;

// possible to use part of imgage with img_w,img_h

imageBytes = GetBitmapBytes(bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, bmi.bmiHeader.biBitCount);

printf("pass GetBitmapBytes=%d \n",imageBytes);

img = (char *) malloc(imageBytes);

if (!img) {

printf("No enought memory for img !\n"); exit(0);

}

// BITMAPFILEHEADER hdr;

hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"

hdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)

+ (bmi.bmiHeader.biClrUsed * sizeof(RGBQUAD)) + bmi.bmiHeader.biSizeImage;

hdr.bfReserved1 = 0;

hdr.bfReserved2 = 0;

hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)

+ (bmi.bmiHeader.biClrUsed * sizeof(RGBQUAD));

scanLineCount = GetDIBits(hDC,h_bitmap,0,bmi.bmiHeader.biHeight, img, &bmi, DIB_RGB_COLORS);

strcpy(nameout,"keyb_tmp.bmp");

if ( (fout = fopen(nameout,"wb") ) == NULL ) {

printf("\007Cann't open output file: %s ", nameout);exit(1);

};

fwrite( &hdr, sizeof(BITMAPFILEHEADER ), 1, fout );

fwrite( &bmi, sizeof(BITMAPINFO), 1, fout );

fwrite( img, sizeof(unsigned char),imageBytes, fout );

fclose(fout);

printf("Output in %s\n",nameout);

}

/* -------end dib and bmp ----- */

  • 上一篇:ssr是什麽
  • 下一篇:請教,基於React的Tab組件內含iFrame的方案
  • copyright 2024編程學習大全網