Need TImage/TBitmap from first Page in PreviewForm without Background

Hello,

after starting the Print Preview the Report renders the first page and then I need the image (TBitmap) from this page without the grayed background of the PreviewForm.Viewer.ScreenDevice. Where can I find this?

So far I use

TppViewer(ppReport.PreviewForm.Viewer).ScreenDevice.PageImage

in the body of TppReport.OnEndFirstPass event, but this is not perfect as it may include the grayed viewer background and additional it is depending on the zoom options.

I need the pure Page Image without any scaling, borders or margin background. Is this possible?

Thanks,
Utzel

Comments

  • Hi Utzel,

    The ScreenDevice renders the page(s) and background all as a single image. If you would like to extract individual pages as images, I suggest using the ImageDevice. This will give you just the page image without background and scaled as you need. See the following article on using this device.

    http://rbwiki.digital-metaphors.com/output/image/image-device-fundamentals/

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Thank you for the very quick answer!

    Could I call this from within printing, e.g. in background of showing the preview to the end-user? Otherwise I would have to run the report twice, once for my internal image and once for the regular preview.

    The goal is to save a preview image and show this in the next time when the user is looking for a report but without to execute all of the given report templates before.
  • Hi Utzel,

    Unfortunately due to the threading nature of the screen device, it is not possible to export to image and screen simultaneously. Since I'm guessing you only need an image of the first page, it would be fairly simple (and fast) to export the first page of a report to image before or after the preview has finished and save that file as the preview image.


    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • Hi Nico,

    but we have the TppViewer(ppReport.PreviewForm.Viewer).ScreenDevice.PageImage with margin background, so it would be perfect when the ScreenDevice additional could have a property (e.g.) PageImageOrigin like the PageImage without the background. Would that be realizable for a future release? I think that could be helpful for other developer too.

    Otherwise okay, then I have to start the report twice. But we have some big data reports. Could you explain what the process should look like in order to lose as little time as possible with additional data access, please? And how can I ensure that only the first page is saved as an image when the first start, and not the entire report?

    Best regards and thanks a lot!
  • I tried the following script based on a tip from this wiki and your tips:
    
      FPreview := TppPreviewPlugIn.CreatePreviewPlugin(Panel1);
      FPreview.StatusBarTbx := FStatusBar;
      FPreview.Viewer.Report := FReport;
    
      if FPreview.Viewer.Busy then
        Exit;
    
      FPreview.BeforePreview;
    
      if FReport.InitializeParameters then
      begin
        sRep := FReport.TextFileName;
        sImg := TPath.ChangeExtension(FReport.Template.Filename, 'jpg');
        if TFile.Exists(sImg) then
          TFile.Delete(sImg);
    
        FReport.DeviceType := dtJPEG;
        FReport.TextFileName := sImg;
        FReport.ShowPrintDialog := False;
        FReport.Print;
    
        FPreview.Viewer.Reset;
        FReport.DeviceType := dtScreen;
        FReport.TextFileName := sRep;
        FReport.ShowPrintDialog := True;
        FReport.PrintToDevices;
      end;
    
    Is this the best practice to save the pure page image of page 1 everytime a user shows the preview panel?

    In the first call I unfortunately have to use "Print" instead of "PrintToDevices" because otherwise no image will be saved. This has the disadvantage that dialogs are shown during the output. Is there no better solution for this?

    Another problem is the file name. For dtPDF this works as expected, but for dtJPEG (dtBMP ...) the file name is extended with an unwanted "_1". How can I fix it?
  • Hi Utzel,

    Rather than altering the DeviceType, try creating your own TppImageDevice descendent. Also, you can implement the ImageDevice.OnAssignFilename event to take control over the default image naming (underscore + page number). Below is some sample code.


    procedure TForm1.Button1Click(Sender: TObject);
    var
    lImageDevice: TppBMPDevice;
    begin

    //Export the first page to image
    lImageDevice := TppBMPDevice.Create(nil);

    try
    lImageDevice.FileName := 'c:\temp\PreviewImage.bmp';
    lImageDevice.OnAssignFileName := ehImageDevice_AssignFileName;
    lImageDevice.Publisher := ppReport1.Publisher;

    ppReport1.PageLimit := 1;

    ppReport1.PrintToDevices;

    finally
    lImageDevice.Free;

    end;

    //Preview the entire Report
    ppReport1.PageLimit := 0;
    ppReport1.Print;

    end;

    procedure TForm1.ehImageDevice_AssignFileName(aSender: TObject);
    begin
    //Remove the default filename suffix
    if aSender is TppImageDevice then
    TppImageDevice(aSender).ImageFileName := 'c:\temp\PreviewImage.bmp'

    end;
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • That's working. Thank you for your support!
Sign In or Register to comment.