图像转换 DIB->JPG , 300分(200分)

  • 主题发起人 主题发起人 cAkk
  • 开始时间 开始时间
C

cAkk

Unregistered / Unconfirmed
GUEST, unregistred user!
已知一个DIB格式的图像句柄HDib,征求将其转换成JPG格式并且保存到文件.

要求: 不用临时文件.
 
到这里看一看
<a href="http://www.efg2.com/lab">这里</a>
 
现写的:

procedure TForm1.Button1Click(Sender: TObject);
var hbm:HBITMAP; hdcImage:HDC; bm:BITMAP; jpg:TJpegImage; b:TBitmap;
begin
hbm:=LoadImage(0, 'c:/test.bmp', IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE or LR_CREATEDIBSECTION);
hdcImage := CreateCompatibleDC(0);
SelectObject(hdcImage, hbm);
GetObject(hbm, sizeof(bm), @bm);
b:=TBitmap.Create;
b.Width:=bm.bmWidth;
b.Height:=bm.bmHeight;
BitBlt(b.canvas.handle, 0, 0, bm.bmWidth, bm.bmHeight, hdcImage, 0, 0, SRCCOPY);
DeleteDC(hdcImage);
jpg:=TJpegImage.Create;
jpg.Assign(b);
jpg.SaveToFile('c:/test.jpg');
jpg.Free;
b.Free;
end;
 
我回家琢磨一下
 
呵呵,我成了您手下的程序员了。:-)
 
习习,我蓝呀!
最近正在写一个程序,需要操作数码相机,高的我头疼. 这个问题就是因为
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 &amp;gt; 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 &amp;gt; 8 then
lpDIBBits := Pointer(bmiSum)
else lpDIBBits := Pointer(bmisumncolor);

if (nColors &amp;lt;= 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 &amp;lt;&amp;gt; 0 then
SelectPalette(dc, Oldpal, False);
ReleaseDC(0, dc);
DIBToDDB := hbm;
end;
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
778
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
818
DelphiTeacher的专栏
D
后退
顶部