
我写过一个 从屏幕上截图到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;
}
// bmibmiHeaderbiWidth, bmibmiHeaderbiHeight, bmibmiHeaderbiBitCount
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",bmibmiHeaderbiWidth, bmibmiHeaderbiHeight);
hDC = CreateCompatibleDC(NULL); // Gdi32lib
CloseClipboard();
bmibmiHeaderbiCompression = BI_RGB;
// possible to use part of imgage with img_w,img_h
imageBytes = GetBitmapBytes(bmibmiHeaderbiWidth, bmibmiHeaderbiHeight, bmibmiHeaderbiBitCount);
printf("pass GetBitmapBytes=%d \n",imageBytes);
img = (char ) malloc(imageBytes);
if (!img) {
printf("No enought memory for img !\n"); exit(0);
}
// BITMAPFILEHEADER hdr;
hdrbfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdrbfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (bmibmiHeaderbiClrUsed sizeof(RGBQUAD)) + bmibmiHeaderbiSizeImage;
hdrbfReserved1 = 0;
hdrbfReserved2 = 0;
hdrbfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (bmibmiHeaderbiClrUsed sizeof(RGBQUAD));
scanLineCount = GetDIBits(hDC,h_bitmap,0,bmibmiHeaderbiHeight, img, &bmi, DIB_RGB_COLORS);
strcpy(nameout,"keyb_tmpbmp");
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 -------------------- /
直接用:
BITMAP bmp;
bmpIamgeGetBitmap(&bmp);
int nWidth = bmpbmWidth;
int nHeight = bmpbmHeight;
在ImageView中的image,可以使用getWidth()和getHeight()来获取宽度和高度,但是获得的image宽度和高度不是很精确的;对于背景,你首先要获取背景的Drawable对象,然后将Drawable对象转换为BitmapDrawable,这样你就可以将背景作为Bitmap对象并获取其宽度和高度了。代码如下:Bitmap b = ((BitmapDrawble)imageViewgetBackground())getBitmap(); int w = bgetWidth(); int h = bgetHeight(); or do like this wayimageViewsetDrawingCacheEnabled(true); Bitmap b = imageViewgetDrawingCache(); int w = bgetWidth(); int h = bgetHeight(); 或者也可以像下面这样:imageViewsetDrawingCacheEnabled(true); Bitmap b = imageViewgetDrawingCache(); int w = bgetWidth(); int h = bgetHeight(); 上面的代码仅仅可以为你获取当前ImageView的大小:imageViewgetWidth(); imageViewgetHeight(); 如果你要获取Drawable image对象的大小,可用如下代码:Drawable d = getResources()getDrawable(Rdrawableyourimage); int h = dgetIntrinsicHeight(); int w = dgetIntrinsicWidth();
以上就是关于根据hbitmap如何得到图片大小或者保存为文件全部的内容,包括:根据hbitmap如何得到图片大小或者保存为文件、在MFC中如何获得一个CBitmap的宽和高、如何获取image的高度和宽度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)