怎样将DDB转化为DIB

johnhowl 2006-01-22 11:28:01
请问我在内存设备环境中生成的位图对象怎样转化成DIB对象,并以BMP或JPG格式储存.
...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluestone 2006-01-25
  • 打赏
  • 举报
回复

dwCompression参数传BI_RGB
HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;


ASSERT( bitmap.GetSafeHandle() );

// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;

// If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

// Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = (WORD)(bm.bmPlanes * bm.bmBitsPixel);
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors> 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);

// We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);

// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}

lpbi = (LPBITMAPINFOHEADER)hDIB;

*lpbi = bi;

// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

bi = *lpbi;

// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;

// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}

// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE);
if (NULL != handle)
hDIB = handle;
else{
GlobalFree(hDIB);

// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}

// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;

// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table

if( !bGotBits )
{
GlobalFree(hDIB);

SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}

SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
handsomerun 2006-01-24
  • 打赏
  • 举报
回复
转换DDB到DIB


步骤:
初始化BITMAPINFOHEADER数据结构。 用位图信息决定位图的宽高与字节数。
最好使用BI_RGB压缩。
实现并选择逻辑调色板。
决定位图使用的字节数。首先为BITMAPINFOHEADER和颜色表分配内存,然后调
用GetDIBits()去计算位图字节数。
给最终的位图尺寸分配内存块,包括BITMAPINFOHEADER和颜色表与位图字节数。
最终再调用GetDIBits()得到位图的字节数。
// DDBToDIB
- Creates a DIB from a DDB
// bitmap
- Device dependent bitmap
// dwCompression
Type of compression - see BITMAPINFOHEADER
// pPal
Logical palette
HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
ITMAP
m;
ITMAPINFOHEADER
i;
PBITMAPINFOHEADER
pbi;
WORD
wLen;
ANDLE
DIB;
ANDLE
andle;
DC
DC;
PALETTE
hPal;
SSERT( bitmap.GetSafeHandle() );
/ The function has no arg for bitfields
f( dwCompression == BI_BITFIELDS )
return NULL;
/ If a palette has not been supplied use defaul palette
Pal = (HPALETTE) pPal->GetSafeHandle();
f (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
/ Get bitmap information
itmap.GetObject(sizeof(bm),(LPSTR)&bm);
/ Initialize the bitmapinfoheader
i.biSize
= sizeof(BITMAPINFOHEADER);
i.biWidth
= bm.bmWidth;
i.biHeight
= bm.bmHeight;
i.biPlanes
= 1;
i.biBitCount
= bm.bmPlanes * bm.bmBitsPixel;
i.biCompression
dwCompression;
i.biSizeImage
= 0;
i.biXPelsPerMeter
0;
i.biYPelsPerMeter
0;
i.biClrUsed
= 0;
i.biClrImportant
0;
/ Compute the size of the infoheader and the color table
nt nColors = (1 << bi.biBitCount);
f( nColors > 256 )
nColors = 0;
wLen = bi.biSize + nColors * sizeof(RGBQUAD);
/ We need a device context to get the DIB from
DC = GetDC(NULL);
Pal = SelectPalette(hDC,hPal,FALSE);
ealizePalette(hDC);
/ Allocate enough memory to hold bitmapinfoheader and color table
DIB = GlobalAlloc(GMEM_FIXED,dwLen);
f (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
pbi = (LPBITMAPINFOHEADER)hDIB;
lpbi = bi;
/ Call GetDIBits with a NULL lpBits param, so the device driver
/ will calculate the biSizeImage field
etDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
i = *lpbi;
/ If the driver did not fill in the biSizeImage field, then compute it
/ Each scan line of the image is aligned on a DWORD (32bit) boundary
f (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
i.biSizeImage = (bi.biSizeImage * 3) / 2;
/ Realloc the buffer so that it can hold all the bits
wLen += bi.biSizeImage;
f (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
lse{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
/ Get the bitmap bits
pbi = (LPBITMAPINFOHEADER)hDIB;
/ FINALLY get the DIB
OOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L,
// Start scan line
(DWORD)bi.biHeight,
// # of scan lines
(LPBYTE)lpbi
/ address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi,
// address of bitmapinfo
(DWORD)DIB_RGB_COLORS);
// Use RGB for color table
f( !bGotBits )
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
electPalette(hDC,hPal,FALSE);
eleaseDC(NULL,hDC);
eturn hDIB;
菜牛 2006-01-22
  • 打赏
  • 举报
回复
加一个位图头即可。
1,01.zipDisplaying a 256 color bitmap在程序中显示256色的位图(6KB)2,02.zipCreating a bitmap object from a BMP file从位图文件中创建位图对象(6KB)3,03.zipAn auto-sizing bitmap picture control一个自适应大小的位图控件(16KB)4,04.zipWriting a bitmap to a BMP file将一个位图写到BMP文件中(11KB)5,05.zipBitmap background in MDI Client在多文档客户程序中增加位图底图(4KB)6,06.zipConverting a bitmap to a region将一个位图转换成一个区域(7KB)7,07.zipConverting a bitmap to a region - memory leak fix 将一个位图转换成一个区域--内存泄露的修正(4KB)8,08.zipTransparent Bitmap实现透明的位图(7KB)9,09.zipCopying a bitmap to clipboard拷贝一个位图到剪贴板(5KB)10,10.zipConverting DDB to DIB将一个设备相关的位图转换成设备无关的位图(6KB)11,11.zipConverting DIB to DDB将一个设备无关的位图转换成设备相关的位图(5KB)12,12.zipCreating a DIB section from a BMP file 从BMP文件中创建一个设备无关的位图(5KB)13,13.zipGetting the dimensions of a bitmap得到一个位图的尺寸(4KB)14,14.zipDraw bitmap with grayed 3D effect画一副3维灰边的位图(6KB)15,15.zipDrawing a bitmap显示位图(7KB)16,16.zipDrawing a bitmap from a BMP file从BMP文件中装入位图并显示(6KB)17,17.zipEBGFX Library 推荐一个强大的EBGFX图形库(15KB)18,18.zipEmboss text and other shape on your bitmap 将文字以浮雕方式嵌入你的位图(7KB)19,19.zipApply a 3D bitmap pattern on text or other shapes将文字上色(3维的位图模板)(6KB)20,20.zipEncapsulated Dib API压缩设备无关位图的API(5KB)21,21.zipAn enhanced DIBLOOK sample 一个增强的DIBLOOK例子(5KB)22,22.zipFade in / Fade out Images using Palette animation使用生动的调色板淡入/淡出位图(8KB)23,23.zipPainting the background for a CFormView derived class在CFromView中画背景图(7KB)24,24.zipGradient Fill 有坡度的填充(5KB)25,25.zipPaint a Gradient Color Background显示渐变的背景(6KB)26,26.zipDrawing an image in grayscale画位图到一个灰色刻度中(5KB)27,27.zipFading from color to grayscale a

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧