Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rendered content of a TWebBrowser saved as JPEG loses quality?

I use the procedure below to create a JPG file from a TWebbrowser That is resulting in a JPG looking OK Then I load this JPG to a TcxImage control from DevExpress in order to print it. And that messes up my image so it isn't possible to see the map (it is a portion of a map from Google Maps) The code for loading the image is

imgPrint.Picture.LoadFromFile(lImage);

I don't quite get why this is looking so bad already on screen. I do it this way in order to be able to print the map. It could also be done direct from the TWebBrowser but ther I have no control of the output size and adding my own headers and footers are tricky.

procedure TfrmJsZipExplorer.actSaveExecute(Sender: TObject);
var
  ViewObject : IViewObject;
  r : TRect;
  Bitmap: TBitmap;
begin
  if WebBrowser1.Document <> nil then
    begin
      WebBrowser1.Document.QueryInterface(IViewObject, ViewObject) ;
      if Assigned(ViewObject) then
        try
          Bitmap := TBitmap.Create;
          try
            r := Rect(0, 0, WebBrowser1.Width, WebBrowser1.Height) ;
            Bitmap.Height := WebBrowser1.Height;
            Bitmap.Width := WebBrowser1.Width;
            ViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Application.Handle, bitmap.Canvas.Handle, @r, nil, nil, 0);
            with TJPEGImage.Create do
              try
                Assign(Bitmap);
                SaveToFile(lImagefile);
              finally
                Free;
            end;
          finally
            Bitmap.Free;
          end;
        finally
          ViewObject._Release;
        end;
    end;
end;
like image 797
OZ8HP Avatar asked Dec 30 '25 10:12

OZ8HP


1 Answers

How to improve a saved JPEG image quality ?

You may set the CompressionQuality property of your saved image to the lowest compression, but highest image quality value. That will improve visual quality of the output image. Setting this property to 100 will result in better image quality, but larger file size:

with TJPEGImage.Create do
try
  Assign(Bitmap);      
  CompressionQuality := 100;
  SaveToFile(lImagefile);
finally
  Free;
end;

It is necessary to use JPEG format for this image archive ?

If you're not limited only to JPEG format for you image archive, consider to use a different format, such as PNG. If you'd decide to use a PNG format with TPNGImage class, there's the CompressionLevel property, which allows you to specify the compression level of a saved image and which directly affects the size of the output file, but unlike the JPEG format compression with keeping the same visual quality. Setting this property to 9 will result in full compression to be used, which produces just smaller file size, the quality remains same as if no compression (0 value) would be used:

uses
  PNGImage;

with TPNGImage.Create do
try
  Assign(Bitmap);
  CompressionLevel := 9;
  SaveToFile(lImagefile);
finally
  Free;
end;
like image 111
TLama Avatar answered Jan 02 '26 04:01

TLama