设备无关位图DIB的显示

下面是以自定义的WINAPI方式的函数
 
HGLOBAL WINAPI ReadDIBFile(CFile &file)
{
BITMAPFILEHEADER header;
DWORD dwbitsize;
HGLOBAL hdib;
LPSTR pdib;
dwbitsize=file.getlength();
file.read(&header,sizeof(header));//读入header指向的内存
hdib=GlobalAlloc(GMEM_MOVEABLE,dwBitSize);
pdib=(char*)GlobalLock(hdib);
file.readhuge(pdib,dwbitsize-sizeof(header));//将位图文件数据写入锁定内存块
//BITMAPINFO结构定义,至此,先读入header指向的内存(BITMAPINFOHEADER),
//接着再写入位图文件数据(RGBQUAD),符合BITMAPINFO结构。
//
//The BITMAPINFO structure defines the dimensionsand
// color information for a DIB.
//typedef struct tagBITMAPINFO {  // BITMAPINFOHEADER bmiHeader;  // RGBQUAD          bmiColors[1]; //} BITMAPINFO, *PBITMAPINFO; 
//
//
Globalunlock(hdib);
return hdib;
}
 
调用上面的自定义API显示
HGLOBAL h_mem;
LPSTR *lpMEM;
h_mem=::ReadDIBFile(bmpfile);
lpMEM=(LPSTR)globallock(h_mem);
bitblt(1,1,20,20,0,0,(LPBITMAPINFO)lpMEM,SRCCOPY)
 
 
关于GlobalLock
Memory Management
GlobalLock

Locks a global memory object and returns a pointer to the firstbyte of the object's memory block.

Note  Theglobal functions are slower than other memory management functionsand do not provide as many features. Therefore, new applicationsshould use the heapfunctions. However, the global functions are still used withDDE and the clipboard functions.

Copy Code
LPVOID GlobalLock(  HGLOBAL hMem);
Parameters
hMem
[in] A handle to the global memory object. This handle isreturned by either the GlobalAlloc or GlobalReAlloc function.
Return Values

If the function succeeds, the return value is a pointer to thefirst byte of the memory block.

If the function fails, the return value is NULL. To get extendederror information, call GetLastError.

Remarks

The internal data structures for each memory object include alock count that is initially zero. For movable memory objects,GlobalLock increments the count by one, and the GlobalUnlock function decrements the count by one. For eachcall that a process makes to GlobalLock for an object, itmust eventually call GlobalUnlock. Locked memory will not bemoved or discarded, unless the memory object is reallocated byusing the GlobalReAlloc function. The memory block of a locked memoryobject remains locked until its lock count is decremented to zero,at which time it can be moved or discarded.

Memory objects allocated with GMEM_FIXED always have a lockcount of zero. For these objects, the value of the returned pointeris equal to the value of the specified handle.

If the specified memory block has been discarded or if thememory block has a zero-byte size, this function returns NULL.

Discarded objects always have a lock count of zero.

BITMAPINFO结构
 

BITMAPINFO

The BITMAPINFO structure defines the dimensions and colorinformation for a DIB.

typedef struct tagBITMAPINFO {   BITMAPINFOHEADER bmiHeader;   RGBQUAD          bmiColors[1]; } BITMAPINFO, *PBITMAPINFO; 
Members
bmiHeader
Specifies a BITMAPINFOHEADERstructure that contains information about the dimensions of colorformat.

.

bmiColors
The bmiColors member contains one of the following:
  • An array of RGBQUAD.The elements of the array that make up the color table.
  • An array of 16-bit unsigned integers that specifies indexesinto the currently realized logical palette. This use ofbmiColors is allowed for functions that use DIBs. WhenbmiColors elements contain indexes to a realized logicalpalette, they must also call the following bitmap functions:

    CreateDIBitmap

    CreateDIBPatternBrush

    CreateDIBSection

    The iUsage parameter of CreateDIBSection must beset to DIB_PAL_COLORS.

The number of entries in the array depends on the values of thebiBitCount and biClrUsed members of the BITMAPINFOHEADERstructure.

The colors in the bmiColors table appear in order ofimportance. For more information, see the Remarks section.

Remarks

A DIB consists of two distinct parts: a BITMAPINFOstructure describing the dimensions and colors of the bitmap, andan array of bytes defining the pixels of the bitmap. The bits inthe array are packed together, but each scan line must be paddedwith zeroes to end on a LONG data-type boundary. If theheight of the bitmap is positive, the bitmap is a bottom-up DIB andits origin is the lower-left corner. If the height is negative, thebitmap is a top-down DIB and its origin is the upper leftcorner.

A bitmap is packed when the bitmap array immediately follows theBITMAPINFO header. Packed bitmaps are referenced by a singlepointer. For packed bitmaps, the biClrUsed member must beset to an even number when using the DIB_PAL_COLORS mode so thatthe DIB bitmap array starts on a DWORD boundary.

Note  ThebmiColors member should not contain palette indexes if thebitmap is to be stored in a file or transferred to anotherapplication.

Unless the application has exclusive use andcontrol of the bitmap, the bitmap color table should containexplicit RGB values.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值