Why does the rendering of TWebBrowser content saved as JPEG lose quality?

I am using the following procedure to create a JPG file from TWebbrowser This makes the JPG look OK Then I load this JPG into the TcxImage control from DevExpress to print it. And it messed up my image so that it is not possible to see the map (this is part of the map from Google Maps) Code to load image

imgPrint.Picture.LoadFromFile(lImage);

      

I don't quite understand why it looks so bad already on screen. I do it this way so that I can print the map. This can also be done directly from TWebBrowser, but I have no control over the output size and adding my own headers and footers is more difficult.

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;

      

+3


source to share


1 answer


How can I improve the saved JPEG image quality?

You can set the property of the CompressionQuality

saved image to the lowest compression but the highest image quality value. This will improve the visual quality of the output image. Setting this property to 100 will result in better image quality, but a larger file size:

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

      



This image archive must use the JPEG format

If you are not limited to just the JPEG format for your image archive, consider using a different format like PNG. If you choose to use the PNG format with a class TPNGImage

there CompressionLevel

, that allows you to specify the compression level of the saved image and which directly affects the size of the output file, but as opposed to JPEG compression while maintaining the same visual quality. Setting this property to 9 will result in full compression being used, which only produces a smaller file size, the quality remains the same as if no compression (value 0) was used:

uses
  PNGImage;

with TPNGImage.Create do
try
  Assign(Bitmap);
  CompressionLevel := 9;
  SaveToFile(lImagefile);
finally
  Free;
end;

      

+4


source







All Articles