以下的代码如何存为一个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;
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;
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;