在DELPHI中图片转换

原来只是从oracle中取出图片,没有做过图片的转换。正好有公安局提出能否将相片转换为单色图片做临时证用。回家后,查找点资料,实现还比较简单。现总结如下:

[@more@]

原来只是从oracle中取出图片,没有做过图片的转换。正好有公安局提出能否将相片转换为单色图片做临时证用。回家后,查找点资料,实现还比较简单。现总结如下:

Function BmpToJpg(BmpFile :String; JpgFile :String;Compress :Integer):Boolean;
Var
Bitmap :TBitmap;
Jpgimage :TjpegImage;
Begin
Result :=False;
Try
bitmap :=TBitmap.Create ;
JpgImage :=TjpegImage.Create ;
bitmap.LoadFromFile(BmpFile);
jpgImage.CompressionQuality :=Compress;
JpgImage.Performance :=jpBestQuality;
jpgImage.Grayscale :=True;
jpgImage.Smoothing :=True;
JpgImage.Assign(Bitmap);
JpgImage.Compress ;
JpgImage.SaveToFile(JpgFile);
Result :=True;
Except
End;
Bitmap.Free;
JpgImage.Free ;
End;

//将JPG图象转化为256色的BMP图象
Function JpgtoBmp(JpgFile :String; BmpFile :String;Compress :Integer):Boolean;
Var
Bitmap :TBitmap;
Jpgimage :TjpegImage;
Begin
Result :=False;
Try
bitmap :=TBitmap.Create ;
JpgImage :=TjpegImage.Create ;
jpgImage.LoadFromFile(JpgFile);
JpgImage.Performance :=jpBestQuality;
jpgImage.Grayscale :=True;
jpgImage.Smoothing :=True; bitmap.Assign(jpgImage);
bitmap.Dormant ;
bitmap.SaveToFile(bmpfile);
Result :=True;
Except
End;
Bitmap.Free;
JpgImage.Free ;

End;

还有一个老外的解决方法:

Question/Problem/Abstract:
I had some trouble to save a permanent grayscaled Jpg because the Grayscale property of the TJpeg is only a visual property.
So here some small code that did the trick for me. There might be more efficient solutions.
Answer:

MyJPG01 => original
MyBMP => help var
MyJPG02 => grayscaled JPG

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Result := false;
TRY //Finally
TRY //Except
// create some stuff
MyJpg01 := TJPEGImage.Create;
MyJpg02 := TJPEGImage.Create;
MyBMP := TBitmap.Create;

// LOAD JPG-file
MyJpg01.OnProgress := ProgressPercent;
ProgressBar_Processing.Position := 0;
MyJpg01.Grayscale := true;
MyJpg01.LoadFromFile( FileName_IN );

// TO BMP TO MAKE PROPERTIES PERMANENT
MyBMP.Width := MyJpg01.Width;
MyBMP.Height := MyJpg01.Height;
MyBMP.Canvas.Draw(0,0, MyJpg01);

// BACK JPG
MyJpg02.OnProgress := ProgressPercent;
ProgressBar_Processing.Position := 0;
MyJpg02.CompressionQuality := 80; // as an example
MyJpg02.Assign(MyBmp);
MyJpg02.SaveToFile( FileName_OUT );

Result := true;

EXCEPT
Panel_ERR.Caption := 'Error on convert ' + FileName_OUT;
Result := False;
END; //Except

FINALLY
MyJpg01.Free;
MyJpg02.Free;
MyBMP.Free;
ProgressBar_Processing.Position := 0;
END; // Finnaly

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/271063/viewspace-826233/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/271063/viewspace-826233/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值