[Lazarus] How to save screenshot to jpg and png format files
R.Smith
ryansmithhe at gmail.com
Thu Apr 29 17:59:57 CEST 2021
On 2021/04/29 17:49, Bo Berglund via lazarus wrote:
> I have an application which can capture a regon of the desktop into an image.
> It uses TBgraBitmap since it has a method for grabbing data from the screen.
>
> So I can assign this to the clipboard and also save to a file:
>
> var
> MyCapture : TBgraBitmap;
> Clip: TRect;
> ...
> begin
> ...
> Clip := Bounds(Self.Left, Self.Top, Self.Width, Self.Height);
> MyCapture := TBgraBitmap.Create();
> MyCapture.TakeScreenShot(Clip);
> Clipboard.Assign(MyCapture.Bitmap);
> MyCapture.Bitmap.SaveToFile(ChangeFileExt(ParamStr(0), '.jpg'));
> MyCapture.Free;
> ...
> end;
>
> But the SaveToFile results in a file that is NOT a jpeg file at all...
>
> How could I save it to a specific (known) format, like jpg or png?
>
Essentially you are asking a BGRA Bitmap to save itself to file, it is
irrelevant whether the file ends in .jpg or .moonmission (although a
TPicture or some custom image components may check the file extension).
What you need is to create a JPEG object, put the data in it and then
use its JPEG-machinery to save the file.
I haven't tested this, typing from my head so may make syntax errors or
such, but the idea is sound:
var
jpg : TJPEGImage;
...
jpg := TJPEGImage.Create;
jpg.Assign(MyCapture.Bitmap);
... perhaps adjust the JPEG quality parameters etc.
jpg.SaveToFIle('somefile.jpg');
jpg.Free;
...
Note that this "jpg.SaveToFIle('somefile.jpg')" statement will ALWAYS
save JPEG data to the file, even if the given filename ends in .png or
.bmp or .mooseknuckle
More information about the lazarus
mailing list