ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 在DELPHI中图片转换

在DELPHI中图片转换

原创 Linux操作系统 作者:wmlm 时间:2006-04-10 12:09:38 0 删除 编辑

原来只是从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博客 ” ,链接:https://blog.itpub.net/271063/viewspace-826233/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-08

  • 博文量
    398
  • 访问量
    3716774