TJpegImage.Compress and SaveToStream lazarus / 39-Graphics ---------------------------------------------------------------------- Aidex · 2021-03-30 08:16 Hi! I want to save a bitmap as a JPEG into a MemoryStream. Unfortunately, I am not able to load it from the stream again (results a black bitmap in correct size). In Delphi 7 it had worked. In Delphi there is the method TJpegImage.Compress(), but it doesn't seem to exist in FP. Could the missing Compress() method be the problem? Is Compress() not needed or is there an alternative? Thanks! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i jpg:=TJpegImage.Create;jpg.Assign(bmp);jpg.CompressionQuality:=90;// jpg.Compress;   missing in FP ?jpg.SaveToStream(m);jpg.Destroy; ---------------------------------------------------------------------- loaded · 2021-03-30 10:48 Hi, You must reset the position value before restoring from stream. Tested working code sample ; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  jpg:TJpegImage;  m:TMemoryStream;begin  m:=TMemoryStream.Create;  m.Position:=0;   jpg:=TJpegImage.Create;  jpg.Assign(image1.Picture.Bitmap);  jpg.CompressionQuality:=90;  jpg.SaveToStream(m);  jpg.Destroy;   m.Position:=0; // <-------------  image2.Picture.LoadFromStream(m); end; ---------------------------------------------------------------------- Aidex · 2021-03-30 11:17 Thanks for your help!  I had already done the positioning m.Position:=0; My source code is a bit more complex ... then the problem is somewhere in my complex source code. Ok, now that I know it's not the missing .Compress, I'll have to go troubleshooting some more. Converting an old Delphi program does have more pitfalls in the details than I had thought.  Thanks again. ---------------------------------------------------------------------- wp · 2021-03-30 11:42 Compress has been introduced in FPC 3.2. But it does nothing else than saving to a temp stream and reading it back - so, forget about it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TJPEGImage.Compress;var  TempStream: TMemoryStream;begin  TempStream := TMemoryStream.Create;  try    FreeSaveStream;    SaveToStream(TempStream);    TempStream.Position := 0;    LoadFromStream(TempStream);  finally    TempStream.Free;  end;end; Delphi's online help is talking non-sense here (translated from German): "By changing property "CompressionQuality" no compression can be achieved.  Always use the method "Compress" for this purpose". I checked it: the following code always results in a low-quality image no matter whether Compress is called or not, even with Delphi 7: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  bmp: TBitmap;  jpg: TJpegImage;begin  bmp := TBitmap.Create;  try    bmp.SetSize(100, 100);    bmp.Canvas.Brush.Color := clWhite;    bmp.Canvas.FillRect(0, 0, 100, 100);    bmp.Canvas.TextOut(0, 0, 'Test');     jpg := TJpegImage.Create;    try      jpg.Assign(bmp);      jpg.CompressionQuality := 5;   // very low quality to see in the save image whether compression has been performed.      jpg.Compress;      jpg.SaveToFile('test.jpg');    finally      jpg.Free;    end;   finally    bmp.Free;  end;end; ---------------------------------------------------------------------- Jiyahana · 2024-02-05 09:39 Yes, here is alternative method Code: [Select]uses   Classes, SysUtils, Graphics, BGRABitmap, BGRABitmapTypes; procedure SaveBitmapToStreamAsJPEG(Bitmap: TBitmap; Stream: TStream; CompressionQuality: integer); var   Bmp32: TBGRABitmap; begin   Bmp32 := TBGRABitmap.Create(Bitmap);   try     Bmp32.SaveToStreamAs(Bitmap, Stream, ifJPEG, CompressionQuality);   finally     Bmp32.Free;   end; end; and If you want to manually compress your images then you can choose online application such as jpeg compressor it compress images with losing the quality. ---------------------------------------------------------------------- TRon · 2024-02-27 00:06 completely off-topic rant Great. Dumb AI answers that find their way in the forums. Ideal showcase to confirm that AI is only about (most common) predictability. Perfect technology to let the sheep be sheep (or actually degenerate them into something even more horrific). ---------------------------------------------------------------------- lainz · 2024-02-27 00:17 Quote from: TRon on February 27, 2024, 01:06:27 amcompletely off-topic rant Great. Dumb AI answers that find their way in the forums. Ideal showcase to confirm that AI is only about (most common) predictability. Perfect technology to let the sheep be sheep (or actually degenerate them into something even more horrific). AI works for me (TM) most of the time, but you need to give it the correct promt. I generated a PDF in Android with AI, not so complex but I've finished it in less than an hour, coding it by myself can take the double of time or more. Edit: But I agree, these bad answers should not be published in the forum.. ---------------------------------------------------------------------- TRon · 2024-02-27 00:46 Quote from: lainz on February 27, 2024, 01:17:51 amAI works for me (TM) most of the time, but you need to give it the correct promt. The "problem" is, that although it can be used to ease up writing repetitive code (at least repetitive for/to the AI), that the answers are based on (common available) input and used to predict a most commonly accepted answer. There is nothing unique, refreshing or smart about the generated code, or it must be you prompted it to combine different variations of the theme that no-one has though of before (but then you are the one that combined them). Thus basically a one-size-fits-all with a little flavour of randomness attached to it so that it appears to be .......  That is working great if you expected that and are able to live with such an answer (not saying it is wrong perse, but just based on/for the masses). ---------------------------------------------------------------------- jamie · 2024-02-27 01:05 I use TZipper , it can be used for streams.