Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2021-04

How to save screenshot to jpg and png format files

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

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?

Bo Berglund Developer in Sweden

Reply by email

R.Smith
to Bo Berglund
Bo Berglund wrote 24 lines
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

Reply by email

Patrick Chevalley
to Bo Berglund: I have an application which can capture a regon of the desktop into an image. It uses…

Hi,

Replace :
MyCapture.Bitmap.SaveToFile(ChangeFileExt(ParamStr(0), '.jpg'));
by :
MyCapture.SaveToFile(ChangeFileExt(ParamStr(0), '.jpg'));

This work because Bgrabitmap set automatically the image writer based on the extension.

Patrick

"Bo Berglund wrote 33 lines
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? -- Bo Berglund Developer in Sweden -- _______________________________________________ lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus

Reply by email

Bo Berglund
to Patrick Chevalley
Patrick Chevalley wrote 8 lines
Replace : MyCapture.Bitmap.SaveToFile(ChangeFileExt(ParamStr(0), '.jpg')); by : MyCapture.SaveToFile(ChangeFileExt(ParamStr(0), '.jpg')); This work because Bgrabitmap set automatically the image writer based on the extension.

Correct!
I could remove *all* the extra code I had added to create jpg, png and bmp containers. Instead I could save directly from the MyCapture object, which has built-in magic to save files correctly based on the commonly used extesnions in the file name.

Problem solved, thanks!

Bo Berglund Developer in Sweden

Reply by email