得到HBitmap如何存为文件?

fbiboss 2009-07-13 04:29:05
以下的代码如何存为一个JPG文件,不引用单元文件,只用API怎么做??
function GetBitmapFromDesktop: HBitmap;
var
DC, MemDC: HDC;
Bitmap, OBitmap: HBitmap;
BitmapWidth, BitmapHeight: integer;
begin
DC := GetDC(GetDesktopWindow);
MemDC := CreateCompatibleDC(DC);
BitmapWidth := GetDeviceCaps(DC, 8);
BitmapHeight := GetDeviceCaps(DC, 10);
Bitmap := CreateCompatibleBitmap(DC, BitmapWidth, BitmapHeight);
OBitmap := SelectObject(MemDC, Bitmap);
BitBlt(MemDC, 0, 0, BitmapWidth, BitmapHeight, DC, 0, 0, SRCCOPY);
SelectObject(MemDC, OBitmap);
DeleteDC(MemDC);
ReleaseDC(GetDesktopWindow, DC);
Result := Bitmap;
end;
...全文
181 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
minizhuxianchun 2009-07-14
  • 打赏
  • 举报
回复
在函数最后一行加:Stream.SaveToFile('c:\mini.bmp');
fbiboss 2009-07-14
  • 打赏
  • 举报
回复
怎么存为BMP文件??
minizhuxianchun 2009-07-14
  • 打赏
  • 举报
回复
procedure TBitmap.WriteStream(Stream: TStream; WriteSize: Boolean);
const
PalSize: array [Boolean] of Byte = (sizeof(TRGBQuad), sizeof(TRGBTriple));
var
Size, ColorCount: DWORD;
HeaderSize: DWORD;
BMF: TBitmapFileHeader;
Save: THandle;
BC: TBitmapCoreHeader;
Colors: array [Byte] of TRGBQuad;
begin
FillChar(BMF, sizeof(BMF), 0);
BMF.bfType := $4D42;
if FImage.FSaveStream <> nil then
begin
Size := FImage.FSaveStream.Size;
if WriteSize then
Stream.WriteBuffer(Size, sizeof(Size));
Stream.Write(FImage.FSaveStream.Memory^, FImage.FSaveStream.Size);
Exit;
end;
DIBNeeded;
with FImage do
begin
Size := 0;
if FDIBHandle <> 0 then
begin
InternalGetDIBSizes(FDIBHandle, HeaderSize, Size, FDIB.dsbmih.biClrUsed);
if FOS2Format then
begin // OS2 format cannot have partial palette
HeaderSize := sizeof(BC);
if FDIB.dsbmih.biBitCount <= 8 then
Inc(HeaderSize, sizeof(TRGBTriple) * (1 shl FDIB.dsbmih.biBitCount));
end;
Inc(Size, HeaderSize + sizeof(BMF));

FillChar(BMF, sizeof(BMF), 0);
BMF.bfType := $4D42;

Canvas.RequiredState([csHandleValid]);
Save := GDICheck(SelectObject(FCanvas.FHandle, FDIBHandle));
ColorCount := GetDIBColorTable(FCanvas.FHandle, 0, 256, Colors);
SelectObject(FCanvas.FHandle, Save);
// GetDIBColorTable always reports the full palette; trim it back for partial palettes
if (0 < FDIB.dsbmih.biClrUsed) and (FDIB.dsbmih.biClrUsed < ColorCount) then
ColorCount := FDIB.dsbmih.biClrUsed;
if (not FOS2Format) and (ColorCount = 0) and (FPalette <> 0) and not FHalftone then
begin
ColorCount := PaletteToDIBColorTable(FPalette, Colors);
if FDIB.dsbmih.biBitCount > 8 then
begin // optional color palette for hicolor images (non OS2)
Inc(Size, ColorCount * sizeof(TRGBQuad));
Inc(HeaderSize, ColorCount * sizeof(TRGBQuad));
end;
end;

BMF.bfSize := Size;
BMF.bfOffBits := sizeof(BMF) + HeaderSize;
end;

if WriteSize then Stream.WriteBuffer(Size, SizeOf(Size));

if Size <> 0 then
begin
FixupBitFields(FDIB);
if (ColorCount <> 0) then
begin
if (FDIB.dsbmih.biClrUsed = 0) or (FDIB.dsbmih.biClrUsed <> ColorCount) then
FDIB.dsbmih.biClrUsed := ColorCount;
if FOS2Format then RGBQuadToTriple(Colors, Integer(ColorCount));
end;
if FOS2Format then
begin
with BC, FDIB.dsbmih do
begin
bcSize := sizeof(BC);
bcWidth := biWidth;
bcHeight := biHeight;
bcPlanes := 1;
bcBitCount := biBitCount;
end;
Stream.WriteBuffer(BMF, sizeof(BMF));
Stream.WriteBuffer(BC, sizeof(BC));
end
else
begin
Stream.WriteBuffer(BMF, Sizeof(BMF));
Stream.WriteBuffer(FDIB.dsbmih, Sizeof(FDIB.dsbmih));
if (FDIB.dsbmih.biBitCount > 8) and
((FDIB.dsbmih.biCompression and BI_BITFIELDS) <> 0) then
Stream.WriteBuffer(FDIB.dsBitfields, 12);
end;
Stream.WriteBuffer(Colors, ColorCount * PalSize[FOS2Format]);
Stream.WriteBuffer(FDIB.dsbm.bmBits^, FDIB.dsbmih.biSizeImage);
end;
end;
end;
Dcdu 2009-07-13
  • 打赏
  • 举报
回复
老大,不引用单元的话你就写出所有你函数里要使用到的函数。DELPHI都有源码的,你可以直接COPY过来使用。
bdmh 2009-07-13
  • 打赏
  • 举报
回复
去了解jpg文件格式吧

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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