Title: Convert a color JPG image to a permanent grayscaled
Question: 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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -