先判断文件是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