习习,我蓝呀!
最近正在写一个程序,需要操作数码相机,高的我头疼. 这个问题就是因为
TWAIN接口提供的是DIB的格式,我需要把他转换成Jpg格式保存.不过,我
最后没有用你的代码,因为我后来发现TWAIN提供了一个接口:
TWAIN_DrawDibToDC(DC:Hdc, 0, 0, DibW, DibH, hDIB, 0, 0);
可以直接把DIB画到DC上面,这样转成jpg就方便了.
另外,这短代码你看看(我网上找到的),可以把hdib转成bitmap:
function DIBToDDB(hDIB: THandle): HBitmap;
var
lpbi: PBitmapInfoHeader;
hbm: HBitmap;
Pal, OldPal: HPalette;
dc: HDC;
nSize: UnsignedInt32;
pLP: PLogPalette;
nColors, i: Integer;
lpDIBBits: Pointer;
bmInfo: PBitmapInfo;
bmicoloraddr: PChar;
bmisum: PChar;
bmisumncolor: PChar;
begin
if (hDIB = 0) then
begin
DIBToDDB := 0;
exit;
end;
dc := GetDC(0);
pal := 0;
lpbi := PBitmapInfoHeader(hDIB);
if (lpbi^.biClrUsed > 0) then
nColors := lpbi^.biClrUsed
else nColors := 1 shl lpbi^.biBitCount;
bmicoloraddr := PChar(@(bmInfo^.bmiColors));
bmiSum := bmiColorAddr + (bmInfo^.bmiHeader.biClrUsed * sizeof(DWORD));
if bmInfo^.bmiHeader.biCompression = BI_BITFIELDS then
bmiSum := bmiSum + (3 * sizeof(DWORD));
bmisumncolor := bmiColorAddr + (nColors * sizeof(DWORD));
if bmInfo^.bmiHeader.biBitCount > 8 then
lpDIBBits := Pointer(bmiSum)
else lpDIBBits := Pointer(bmisumncolor);
if (nColors <= 256 and (GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE)) then
begin // Create and select a logical palette if needed
nSize := sizeof(TLogPalette) + (sizeof(TPaletteEntry) * nColors);
GetMem(pLP, nSize);
pLP^.palVersion := $0300;
pLP^.palNumEntries := ncolors;
for i := 0 to nColors do
begin
pLP^.palPalEntry.peRed := bmInfo.bmiColors.rgbRed;
pLP^.palPalEntry.peGreen := bmInfo.bmiColors.rgbGreen;
pLP^.palPalEntry.peBlue := bmInfo.bmiColors.rgbBlue;
pLP^.palPalEntry.peFlags := 0;
end;
pal := CreatePalette(pLP^);
FreeMem(pLP);
OldPal := SelectPalette(dc, pal, False);// select and realize the palette
RealizePalette(dc);
end;
hbm := CreateDIBitmap(dc,
(PBitmapInfoHeader(lpbi))^,
LongInt(CBM_INIT),
lpDIBBits,
(PBitmapInfo(lpbi))^,
DIB_RGB_COLORS);
if pal <> 0 then
SelectPalette(dc, Oldpal, False);
ReleaseDC(0, dc);
DIBToDDB := hbm;
end;