根据hbitmap如何得到图片大小或者保存为文件?
我从屏幕上截图下来,得到的是HBITMAP,我怎么样才能得到这张图片?我想用SOCKET把图发送出去,但不知道hbitmap的大小,或者怎么把hbitmap存为bmp文件...
我从屏幕上截图下来,得到的是HBITMAP,我怎么样才能得到这张图片?我想用SOCKET把图发送出去,但不知道hbitmap的大小,或者怎么把hbitmap存为bmp文件?hbitmap是这样得到的
hbitmap=CreateCompatibleBitmap(hscrdc,w,h);
holdbit=SelectObject(hmemdc,hbitmap);
BitBlt(hmemdc,0,0,w,h,hscrdc,x,y,0xcc0020);
hbitmap=(HBITMAP*)SelectObject(hmemdc,holdbit);
先谢谢你,但是我还是想得到hbitmap指向的图片,有没有方法?
又遇到个问题,用什么方法可以把图片字节流还原成图片文件保存? 展开
hbitmap=CreateCompatibleBitmap(hscrdc,w,h);
holdbit=SelectObject(hmemdc,hbitmap);
BitBlt(hmemdc,0,0,w,h,hscrdc,x,y,0xcc0020);
hbitmap=(HBITMAP*)SelectObject(hmemdc,holdbit);
先谢谢你,但是我还是想得到hbitmap指向的图片,有没有方法?
又遇到个问题,用什么方法可以把图片字节流还原成图片文件保存? 展开
展开全部
我写过一个 从屏幕上截图到clipboard里 (相当于按print-screen键,或 我这里有一个 按 Alt+print-screen键),然后存为BMP的子程序。你稍作修改(例如改一下存放的文件名,添主程序和函数调用)就可以用。
// ---dib functions 函数原型-----
int GetBytesPerPixel(int depth);
int GetBytesPerRow(int width, int depth);
int GetBitmapBytes(int width, int height, int depth);
void save_clipboard_img_to_bmp();
// ---------函数定义---------
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 -------------------- */
// ---dib functions 函数原型-----
int GetBytesPerPixel(int depth);
int GetBytesPerRow(int width, int depth);
int GetBitmapBytes(int width, int height, int depth);
void save_clipboard_img_to_bmp();
// ---------函数定义---------
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 -------------------- */
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询