
Picture
control的类是CStatic。可以用如下语句获取bitmap。
CStatic
PictureBox;
HBITMAP
bmp
=
PictureBoxGetBitmap();
现成获取ID的函数没有,不过也很简单啊,继承CStatic添加一个记录ID值的功能就好啦。
MFC 程序里用:
HBITMAP hBmp = CreateDIBitmap( dcm_hDC,// handle to device context
&bmiHeader, // pointer to BITMAPINFOHEADER
CBM_INIT, // initialization flag
lpDIBBits, // pointer to initialization data
&bmInfo, // pointer to bitmap color-format data
DIB_RGB_COLORS); // color-data usage
bitmapAttach( hBmp ); // 用这句,就把 BITMAP 读入了 CBitmap bitmap 对象里。
BITMAP 需要很多辅助信息,才能知道里面的数据的意义,所以用 CBitmap对象存放,便于使用。
我的实用程序:
/ =========================================================================
LoadBMPImage - Loads a BMP file and creates a bitmap GDI object
also creates logical palette for it
Returns - TRUE for success (check success or not)
sBMPFile - Full path of the BMP file, (input file name)
bitmap - The bitmap object to initialize, (bring back img in it)
pPal - Will hold the logical palette Can be NULL (bring back)
==========================================================================/
BOOL CVPICDoc::LoadBMPImage(LPCTSTR sBMPFile, CBitmap &bitmap, CPalette pPal)
{
BITMAPFILEHEADER bmfHeader;
// Read file header
CFile file;
if( !fileOpen( sBMPFile, CFile::modeRead) ) return FALSE;
if (fileRead((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return FALSE;
// Check File type should be 'BM'
if (bmfHeaderbfType != ((WORD) ('M' << 8) | 'B'))
return FALSE;
read_bmp_o = bmfHeaderbfOffBits / 8;
// Get length of the remainder of the file and allocate memory
DWORD nPackedDIBLen = fileGetLength() - sizeof(BITMAPFILEHEADER);
HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nPackedDIBLen);
if (hDIB == 0)
return FALSE;
// Read the remainder of the bitmap file
// BITMAPINFOHEADER + possible-Color-Table + DIB-bits
if (fileReadHuge((LPSTR)hDIB, nPackedDIBLen) != nPackedDIBLen )
{
::GlobalFree(hDIB);
return FALSE;
}
BITMAPINFOHEADER &bmiHeader = (LPBITMAPINFOHEADER)hDIB ;
BITMAPINFO &bmInfo = (LPBITMAPINFO)hDIB ;
read_bmp_w = bmiHeaderbiWidth;
read_bmp_h = bmiHeaderbiHeight;
read_bmp_c = bmiHeaderbiBitCount;
read_bmp_size = bmiHeaderbiSizeImage;
if (read_bmp_size == 0) {
read_bmp_size = ((((read_bmp_w read_bmp_c) + 31) & ~31) >> 3) read_bmp_h;
};
read_bmp_l = read_bmp_size / read_bmp_h; // scan line width
// If bmiHeaderbiClrUsed is zero we have to infer the number
// of colors from the number of bits used to specify it
int nColors = bmiHeaderbiClrUsed bmiHeaderbiClrUsed :
1 << bmiHeaderbiBitCount;
LPVOID lpDIBBits;
if( bmInfobmiHeaderbiBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfobmiColors + bmInfobmiHeaderbiClrUsed) +
((bmInfobmiHeaderbiCompression == BI_BITFIELDS) 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfobmiColors + nColors);
// Create the logical palette
if( pPal != NULL )
{
// Create the palette
if( nColors <= 256 )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) nColors);
LOGPALETTE pLP = (LOGPALETTE ) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry[i]peRed = bmInfobmiColors[i]rgbRed;
pLP->palPalEntry[i]peGreen = bmInfobmiColors[i]rgbGreen;
pLP->palPalEntry[i]peBlue = bmInfobmiColors[i]rgbBlue;
pLP->palPalEntry[i]peFlags = 0;
}
pPal->CreatePalette( pLP );
delete[] pLP;
}
}
CClientDC dc(NULL);
CPalette pOldPalette = NULL;
if( pPal )
{
pOldPalette = dcSelectPalette( pPal, FALSE );
dcRealizePalette();
}
HBITMAP hBmp = CreateDIBitmap( dcm_hDC,// handle to device context
&bmiHeader, // pointer to BITMAPINFOHEADER
CBM_INIT, // initialization flag
lpDIBBits, // pointer to initialization data
&bmInfo, // pointer to bitmap color-format data
DIB_RGB_COLORS); // color-data usage
bitmapAttach( hBmp );
if( pOldPalette )
dcSelectPalette( pOldPalette, FALSE );
::GlobalFree(hDIB);
return TRUE;
}
---------------
如果你喜欢 c 程序,喜欢把孤零零的点阵数据放入数组也可以。
这是完整的程序,读入1个 bmp 文件,允许裁剪出其中的一部分。数据按行读入。buff 存一行数据,你修改一下,改成大数组,就可存放全部数据。但你仍需BITMAPINFO,BITMAPFILEHEADER等数据才有实用价值。完整的程序:
#include <windowsh>
#include <stdioh>
#include <timeh>
#include <stringh>
#include <stdlibh>
int DEBUG=0;
unsigned char buff;
FILE fin;
FILE fout;
main(int argc, char argv[] )
{
char namein[72], nameout[72];
int clip_x0, clip_y0, clip_x1, clip_y1;
int buff_len, buff_len2;
int height, width; / input bmp /
int height2, width2; / output bmp /
int color_depth;
int no_off = 0;
int i,j,k,jj;
int delta;
unsigned char zero, junk;
BITMAPFILEHEADER bfh, bfh2;
BITMAPINFOHEADER bih, bih2;
BITMAPINFO binfo, binfo2;
bfh = (BITMAPFILEHEADER ) malloc( sizeof(BITMAPFILEHEADER));
bih = (BITMAPINFOHEADER ) malloc( sizeof(BITMAPINFOHEADER));
binfo = (BITMAPINFO ) malloc( sizeof(BITMAPINFO));
bfh2 = (BITMAPFILEHEADER ) malloc( sizeof(BITMAPFILEHEADER));
bih2 = (BITMAPINFOHEADER ) malloc( sizeof(BITMAPINFOHEADER));
binfo2 = (BITMAPINFO ) malloc( sizeof(BITMAPINFO));
junk = (unsigned char ) malloc(sizeof(unsigned char));
zero = (unsigned char ) malloc(sizeof(unsigned char));
zero[0] = 0xff;
clip_x0 = 0; clip_x1 = 99;
clip_y0 = 0; clip_y1 = 99;
if (argc < 2) {
fprintf(stderr,"\007========================================================\n");
fprintf(stderr," Usage: %s -in namein -out nameout -x0 0 -x1 0 -y0 99 -y1 99\n", argv[0]);
fprintf(stderr,"========================================================\n");
exit(-1);
};
strcpy(namein,"BB120bmp");
strcpy(nameout,"tmpbmp");
for (i =1; i<argc-1;i++) {
if (strcmp("-in",argv[i]) == 0) strcpy (namein,argv[i+1]);
if (strcmp("-out",argv[i]) == 0) strcpy (nameout,argv[i+1]);
if (strcmp("-x0",argv[i]) == 0) sscanf (argv[i+1],"%d", &clip_x0);
if (strcmp("-x1",argv[i]) == 0) sscanf (argv[i+1],"%d", &clip_x1);
if (strcmp("-y0",argv[i]) == 0) sscanf (argv[i+1],"%d", &clip_y0);
if (strcmp("-y1",argv[i]) == 0) sscanf (argv[i+1],"%d", &clip_y1);
if (strcmp("-debug",argv[i]) == 0) sscanf (argv[i+1],"%d", &DEBUG);
if (strcmp("-no_off",argv[i]) == 0) sscanf (argv[i+1],"%d", &no_off);
} / end for /
if ( (fin = fopen(namein,"rb" )) == NULL ) {
printf("Cann't open input file: %s\n",namein); exit(1);};
fread(bfh2, sizeof(BITMAPFILEHEADER),1,fin);
fread(binfo2, sizeof(BITMAPINFO),1,fin);
rewind(fin);
fread(bfh, sizeof(BITMAPFILEHEADER),1,fin);
fread(binfo, sizeof(BITMAPINFO),1,fin);
width = binfo->bmiHeaderbiWidth;
height = binfo->bmiHeaderbiHeight;
color_depth = binfo->bmiHeaderbiBitCount;
buff_len = (width (color_depth / 8 ) / 4 ) 4;
if (buff_len < width color_depth / 8 ) buff_len = buff_len + 4;
buff = (unsigned char ) malloc( (buff_len) sizeof(unsigned char));
if (DEBUG == 1) {printf("buff length = %d\n", buff_len); };
if ( (fout = fopen(nameout,"wb" )) == NULL ) {
printf("Cann't open input file: %s\n",nameout); exit(1);};
/ consider 8 bit image only here /
if (clip_x0 < 0) clip_x0 = 0;
if (clip_x1 >= width ) clip_x1 = width - 1 ;
if (clip_y0 < 0) clip_y0 = 0;
if (clip_y1 >= height) clip_y1 = height - 1 ;
width2 = (clip_x1 - clip_x0) + 1;
height2 = (clip_y1 - clip_y0) + 1;
buff_len2 = (width2 (color_depth / 8) / 4) 4;
if (buff_len2 < width2 (color_depth / 8) ) buff_len2 = buff_len2 + 4;
delta = buff_len2 - width2 (color_depth / 8);
binfo2->bmiHeaderbiWidth = width2;
binfo2->bmiHeaderbiHeight = height2;
binfo2->bmiHeaderbiSizeImage = width2 height2;
bfh2->bfSize = sizeof(BITMAPFILEHEADER) + bfh->bfOffBits - sizeof(BITMAPINFO) + buff_len2 height2 ;
fwrite(bfh2, sizeof(BITMAPFILEHEADER),1,fout);
fwrite(binfo2, sizeof(BITMAPINFO),1,fout);
j = bfh->bfOffBits - sizeof(BITMAPINFO);
if ( color_depth <=16 && no_off == 0)
{
if (j > 0 && j < bfh->bfSize)
{
for (i=0;i < bfh->bfOffBits ;i++){
fread(junk,sizeof(unsigned char), 1, fin);
fwrite(junk,sizeof(unsigned char), 1, fout);
}; };
};
j = (clip_x0) (color_depth / 8);
k = (width2 ) (color_depth / 8);
jj = 0;
Lab_read: / try goto instead of for /
fread( &buff[0], sizeof(unsigned char), buff_len, fin);
if (jj < clip_y0 || jj > clip_y1) goto skip;
fwrite(&buff[j],sizeof(unsigned char),k,fout);
if (delta !=0) {
for (i=0;i < delta; i++){
fwrite(&zero[0], sizeof(unsigned char),1, fout);
};
};
skip: jj = jj + 1;
if (jj < height) goto Lab_read;
fclose(fin);
fclose(fout);
printf("\007Output in %s\n",nameout);
}
int GetDIBits( HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // first scan line to set
UINT cScanLines, // number of scan lines to copy
LPVOID lpvBits, // array for bitmap bits
LPBITMAPINFO lpbi, // bitmap data buffer
UINT uUsage // RGB or palette index);
}
lpvBits 就是二进制图像。。
BitmapgetAllocationByteCount() 方法获取 Bitmap 占用的字节大小
默认情况下 BitmapFactory 使用 BitmapConfigARGB_8888 的存储方式来加载内容,而在这种存储模式下,每一个像素需要占用 4 个字节。
实际上 BitmapFactory 在解析的过程中,会根据当前设备屏幕密度和所在的 drawable 目录来做一个对比,根据这个对比值进行缩放 *** 作。
在 Android 中,各个 drawable 目录对应的屏幕密度分别为下:
PS: OptionsinMutable 置为 true,这里如果不置为 true 的话,BitmapFactory 将不会重复利用 Bitmap 内存
复用 inBitmap 之前,需要调用 canUseForInBitmap 方法来判断 reuseBitmap 是否可以被复用。这是因为 Bitmap 的复用有一定的限制:
在不压缩的前提下,不建议一次性将整张图加载到内存,而是采用分片加载的方式来显示部分内容,然后根据手势 *** 作,放大缩小或者移动显示区域。
BitmapRegionDecoder 将加载到内存中,可以以绝对路径、文件描述符、输入流的方式传递给 BitmapRegionDecoder
当需要在界面上同时展示一大堆的时候,比如 ListView、RecyclerView 等,由于用户不断地上下滑动,某个 Bitmap 可能会被短时间内加载并销毁多次。这种情况下通过使用适当的缓存,可以有效地减缓 GC 频率保证加载效率,提高界面的响应速度和流畅性。
LruCache(Least Recently Used)算法的核心思想就是 最近最少使用算法 。
内部维护了一个 LinkHashMap 的链表,通过put数据的时候判断是否内存已经满了,如果满了,则将最近最少使用的数据给剔除掉,从而达到内存不会爆满的状态。
1,加载一张,使用GDI+的Bitmap类,创建一个对象,Bitmap bitmap=Bitmap::FromFile("examplepng");
2,从Bitmap对象中获得HBITMAP句柄,HBITMAP句柄指向位图,
HBITMAP hBMP; bitmap->GetHBITMAP(NULL,hBMP);
3,从句柄中找到位图结构体,位图结构体(BITMAP)包含了关于位图的所有信息
BITMAP bitmap; GetObject(hBMP,sizeof(BITMAP),&bitmap);
4,参照BITMAP结构体,就能得到你要的字节数组
bitmapbmBits;
不过得到的不一定是字节数组,因为图像有的用3个字节表示一个像素,有的是一个位标识一个像素。
不可能。你可以用bitmap先存了,然后再用filestream读。然后覆盖掉
当然这个方法很猥琐。
建议你查查msdn,看看有没有其他的流是可以用bitmap构造的……
以上就是关于请问mfc中如何获取一个picture control控件中的bitmap的id全部的内容,包括:请问mfc中如何获取一个picture control控件中的bitmap的id、C++ 怎样把一个BITMAP中的数据读到一个数组里、怎么通过hbitmap句柄 获取到该图像的所有二进制数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)