[xiaopei]写的仅对有Handle的控件有效,俺稍微改进一下,可以试用于从TControl继承下来的所有控件,如例中的 Image1 :)
procedure TForm1.Button1Click(Sender: TObject);
procedure SaveAsJpeg(BMP: TBitmap; FileName: string);
var
Jpg: TJPEGImage;
begin
Jpg := TJPEGImage.Create;
try
Jpg.Assign(BMP);
Jpg.CompressionQuality := 100;
Jpg.Compress;
Jpg.SaveToFile(FileName);
finally
FreeAndNil(Jpg);
end;
end;
var
Bitmap:TBitmap;
DC:HDC;
ATopLeft: TPoint;
begin
SavePictureDialog1.FileName := '';
with Image1 do
begin
DC := GetDC(0);
ATopLeft := ClientToScreen(Point(0, 0));
if DC <> 0 then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := Width ;
Bitmap.Height := Height;
StretchBlt(Bitmap.Canvas.Handle, 0, 0, Width, Height, dc,
ATopLeft.X, ATopLeft.Y, Width, Height, SRCCOPY);
with SavePictureDialog1 do
if Execute then
begin
if UpperCase(ExtractFileExt(FileName)) = '.JPG' then
SaveAsJpeg(Bitmap, FileName)
else if UpperCase(ExtractFileExt(FileName)) = '.BMP' then
Bitmap.SaveToFile(FileName);
end;
finally
FreeAndNil(Bitmap);
ReleaseDC(0,DC);
end;
end;
end;
end;