在DELPHI中圖片轉換

wmlm發表於2006-04-10

原來只是從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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章