(*//
标题:图片之间互相转换
说明:支持Icon,Jpeg,Bmp格式
设计:Zswang
日期:2002-01-25
支持:wjhu111@21cn.com
//*)
///////Begin Source
uses
jpeg;
function BmpToIco(mBitmap: TBitmap; mIcon: TIcon;
mSize32: Boolean = True): Boolean;
var
vIconWidth: Integer;
vIconHeight: Integer;
vBitmapMask: TBitmap;
vBitmapColor: TBitmap;
vIconInfo: TIconInfo;
begin
Result := True;
if mSize32 then begin
vIconWidth := 32;
vIconHeight := 32;
end else begin
vIconWidth := 16;
vIconHeight := 16;
end;
vBitmapMask := TBitmap.Create;
vBitmapColor := TBitmap.Create;
try
vBitmapMask.Width := vIconWidth;
vBitmapMask.Height := vIconHeight;
vBitmapMask.Canvas.Brush.Color := clBlack;
vBitmapMask.Canvas.Rectangle(0, 0, vIconWidth, vIconHeight);
vBitmapColor.Width := vIconWidth;
vBitmapColor.Height := vIconHeight;
StretchBlt(vBitmapColor.Canvas.Handle, 0, 0, vIconWidth, vIconHeight,
mBitmap.Canvas.Handle, 0, 0, mBitmap.Width, mBitmap.Height, SRCCOPY);
vIconInfo.fIcon := True;
vIconInfo.xHotspot := 0;
vIconInfo.yHotspot := 0;
vIconInfo.hbmMask := vBitmapMask.Handle;
vIconInfo.hbmColor := vBitmapColor.Handle;
mIcon.Handle := CreateIconIndirect(vIconInfo);
except
Result := False;
end;
vBitmapMask.Free;
vBitmapColor.Free;
end; { BmpToIco }
function IcoToBmp(mIcon: TIcon; mBitmap: TBitmap): Boolean;
var
vIconWidth: Integer;
vIconHeight: Integer;
begin
Result := True;
try
vIconWidth := mIcon.Width;
vIconHeight := mIcon.Height;
mBitmap.Width := vIconWidth;
mBitmap.Height := vIconHeight;
mBitmap.Canvas.FillRect(Rect(0, 0, vIconWidth, vIconHeight));
mBitmap.Canvas.Draw(0, 0, mIcon);
except
Result := False;
end;
end; { IcoToBmp }
function JpegToBmp(mJPEGImage: TJPEGImage; mBitmap: TBitmap): Boolean;
begin
Result := True;
try
mBitmap.Assign(mJPEGImage);
except
Result := False;
end;
end; { JpegToBmp }
function BmpToJpeg(mBitmap: TBitmap; mJPEGImage: TJPEGImage;
mCompressionQuality: Integer = 75): Boolean;
begin
Result := True;
try
mJPEGImage.Assign(mBitmap);
mJPEGImage.CompressionQuality := mCompressionQuality;
mJPEGImage.Compress;
except
Result := False;
end;
end; { BmpToJpeg }
///////End Source
///////Begin Demo
procedure TForm1.Button7Click(Sender: TObject);
var
vJPEGImage: TJPEGImage;
begin
if RadioButton1.Checked then begin
vJPEGImage := TJPEGImage.Create;
try
Image2.Picture.Icon.Assign(nil);
TJPEGImage(Image3.Picture).Assign(nil);
BmpToIco(Image1.Picture.Bitmap, Image2.Picture.Icon, CheckBox1.Checked);
BmpToJpeg(Image1.Picture.Bitmap, vJPEGImage, SpinEdit1.Value);
TJPEGImage(Image3.Picture).Assign(vJPEGImage);
finally
vJPEGImage.Free;
end;
end else if RadioButton2.Checked then begin
vJPEGImage := TJPEGImage.Create;
try
Image1.Picture.Bitmap.Assign(nil);
TJPEGImage(Image3.Picture).Assign(nil);
IcoToBmp(Image2.Picture.Icon, Image1.Picture.Bitmap);
BmpToJpeg(Image1.Picture.Bitmap, vJPEGImage, SpinEdit1.Value);
TJPEGImage(Image3.Picture).Assign(vJPEGImage);
finally
vJPEGImage.Free;
end;
end else begin
vJPEGImage := TJPEGImage.Create;
try
vJPEGImage.Assign(TJPEGImage(Image3.Picture));
Image1.Picture.Bitmap.Assign(nil);
Image2.Picture.Icon.Assign(nil);
JpegToBmp(vJPEGImage, Image1.Picture.Bitmap);
BmpToIco(Image1.Picture.Bitmap, Image2.Picture.Icon, CheckBox1.Checked);
finally
vJPEGImage.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if SavePictureDialog1.Execute then
Image1.Picture.Bitmap.SaveToFile(SavePictureDialog1.FileName);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if OpenPictureDialog2.Execute then
Image2.Picture.Icon.LoadFromFile(OpenPictureDialog2.FileName);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if SavePictureDialog2.Execute then
Image2.Picture.Icon.SaveToFile(SavePictureDialog2.FileName);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if OpenPictureDialog3.Execute then
Image3.Picture.LoadFromFile(OpenPictureDialog3.FileName);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if SavePictureDialog3.Execute then
Image3.Picture.SaveToFile(SavePictureDialog3.FileName);
end;
///////End Demo