http://www.delphi2007.net/DelphiMultimedia/html/delphi_20061024164425208.html
Function PictureResiz(aFilename{原图},dFileName{转换后的图片}: String): String;
var
Image: TPicture;
bmp, tmpbmp: TBitmap;
aDesiredFileName: String;
begin
Image := TPicture.Create;
bmp := TBitmap.Create;
tmpbmp := TBitmap.Create;
try
Image.LoadFromFile(aFilename);
bmp.Assign(Image.Graphic);
tmpbmp.Width := liWidth; //指定图片宽度
tmpbmp.Height := liHeight; //指定图片高度
SetStretchBltMode(tmpbmp.Canvas.Handle, STRETCH_DELETESCANS); //平滑处理
StretchBlt(tmpbmp.Canvas.Handle, 0, 0, tmpbmp.Width, tmpbmp.Height,
bmp.Canvas.Handle, 0, 0, bmp.Width, bmp.Height, SRCCOPY);
Image.Graphic.Assign(tmpbmp);
aDesiredFileName:=dFileName;
if FileExists(aDesiredFileName) then DeleteFile(aDesiredFileName);
Image.SaveToFile(aDesiredFileName);
Result := aDesiredFileName;
finally
bmp.Free;
tmpbmp.Free;
Image.Free;
end;
end;
先判断文件是bmp还是jpg的
然后再分别处理
uses
jpeg;
//--------------------
var
MyJpeg: TJpegImage;
MyBitmap : TBitmap ;
begin
MyBitmap := TBitmap.Create();
MyJpeg:= TJpegImage.Create();
MyBitmap.LoadFromFile(\'c:\windows\desktop\aa.BMP\'); // Load the Bitmap from a file
MyJpeg.Assign(MyBitmap); // Assign the BitMap to MyJpeg object
// MyJpeg.Canvas.StretchDraw(TRect(0,0, nImageWidth, nImageHeight), MyBitmap);
MyJpeg.CompressionQuality:=StrToInt(\'75\');
MyJpeg.Compress;
MyJpeg.SaveToFile(\'c:\windows\desktop\test.JPG\'); // Save the JPEG to Disk
end;
jpg转bmp反过来写就可以了
转换的时候图片大小也要改变, happyggy(TObject说:我是上帝~~~),你的代码不能设置图片大小吧. 比如:500*500的jpg或bmp图片,要变成100*100的jpg图片.
再加一个变量
//==========================
var
MyJpeg: TJPEGImage;
MyBitmap: TBitmap;
desbitmap: TBitmap;
begin
myjpeg := TJPEGImage.Create;
MyBitmap := TBitmap.Create;
desbitmap := TBitmap.Create;
MyJpeg.LoadFromFile(\'c:\123.jpg\');
MyBitmap.Assign(MyJpeg);
desbitmap.Height := 100;
desbitmap.Width := 100;
desbitmap.Canvas.CopyRect(desbitmap.Canvas.ClipRect, MyBitmap.Canvas, MyBitmap.Canvas.ClipRect);
Image1.Picture.Bitmap := desbitmap;
MyBitmap.Free;
MyJpeg.Free;
desbitmap.Free;
end;
有问题加群32179979或26929954