Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Text quality in metafile created at runtime assigned to ppImage

edited October 2017 in General

RB18, D7.

I'm trying to generate a metafile at run-time, and then assign it to a
ppImage to be printed in a group header.

The following code works, but the text is pretty ugly. It appears as
non-antialiased scaled text (lots of "pixelation" in the chars).

Any suggestions on how I can get better text quality?

TIA.


const
TRAY_WIDTH = 9600;
TRAY_DEPTH = 3400;
xOffset = 40;
yOffset = -50;

HeaderBandBeforePRint:

var
MetaFile: TMetafile;
MFCanvas: TMetafileCanvas;
maxwidth: Integer;
maxHt: Integer;
aPic: TPicture;
begin
MetaFile := TMetafile.Create;
try
maxwidth := ppImageVLM.spWidth;
maxHt := Trunc((maxwidth / TRAY_WIDTH ) * TRAY_DEPTH);


aPic:=TPicture.Create;
aPic.Bitmap.Width := ppImageVLM.spWidth;
aPic.Bitmap.Height := maxHt;
aPic.Bitmap.Canvas.Font.Name := 'Tahoma';

MetaFile.Width := TRAY_WIDTH;
MetaFile.Height:= TRAY_DEPTH;

MFCanvas := TMetafileCanvas.Create(MetaFile,
aPic.Bitmap.Canvas.Handle);

try
MFCanvas.Brush.Color := clYellow;
MFCanvas.FloodFill(0,0,clYellow,fsBorder);
MFCanvas.Brush.Color := clSkyBlue;
MFCanvas.Font.Name := 'Tahoma';
MFCanvas.Font.Size := 100;
MFCanvas.Rectangle(0, 0, TRAY_WIDTH, TRAY_DEPTH);
MFCanvas.TextOut(100,100, 'TESTME');
finally
MFCanvas.Free;
end;



aPic.Bitmap.Canvas.StretchDraw(Rect(0, 0, maxwidth, maxHt), MetaFile);

ppImageVLM.Picture.Assign(aPic);

finally
MetaFile.Free;
aPic.free;
end;



Cheers,
EdB

Comments

  • edited October 2017
    Hi Ed,

    The reason you are getting a low quality image is likely due to the fact
    that you are converting the metafile to a bitmap. Is there a reason you
    are doing this?

    If you would like the metafile to display inside a TppImage, simply
    assign it to the TppImage.Picture.Graphic (or Metafile) property. For
    instance...

    ...

    MetaFile := TMetafile.Create;
    MFCanvas := TMetafileCanvas.Create(MetaFile, THandle(GetDC(0)));

    //Draw to canvas

    MFCanvas.Free;

    ppImageVLM.Picture.Metafile := Metafile

    ...

    The preferred way to draw directly to the report canvas however is to
    use the TppPaintBox. This way you can avoid the metafile altogether and
    draw directly to its canvas instead. See the following article on how
    it can be used.

    http://www.digital-metaphors.com/rbWiki/RCL/Fundamentals/How_To...Use_the_PaintBox_Component

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited October 2017
    Thanks for follow-up Nico

    I'm looking to use metafile so I can let it scale for me...

    I'm modeling about 50 shelving units - each is 96x34 inches - expressed
    in 100th of an inch.

    Each shelf has any combination of bins - from the full size of the
    shelf to a non-homogeneous set of NxM bins.

    I can set up the metafile with width x height of 9600x3400, then just
    draw each bin as a rect based on the bottom-left to top-right of the
    bin size (with bin size in inches x 100).

    On-screen this works great in a PaintBox on a form (on resize of the
    form, the paintbox gets invalidated and redraws to scale).


    Using the metafile in the report, I share most of the code and the
    report output looks efeectively the same as screen only - except for
    the text output.

    NOTE: When I modify the code to use your changes below, I get a blank
    image on the report.

    EdB

  • edited October 2017
    Hi Ed,

    Below is the code I used to test your case. I also sent an example to
    your email.

    Be sure your TppImage has the Stretch property set to True.

    procedure TForm1.Button1Click(Sender: TObject);
    const
    TRAY_WIDTH = 9600;
    TRAY_DEPTH = 3400;
    var
    MetaFile: TMetafile;
    MFCanvas: TMetafileCanvas;
    lDC: THandle;
    begin

    MetaFile := TMetafile.Create;

    try
    MetaFile.Width := TRAY_WIDTH;
    MetaFile.Height:= TRAY_DEPTH;

    lDC := GetDC(0);
    MFCanvas := TMetafileCanvas.Create(MetaFile, lDC);

    try
    MFCanvas.Brush.Color := clYellow;
    MFCanvas.FloodFill(0,0,clYellow,fsBorder);
    MFCanvas.Brush.Color := clSkyBlue;
    MFCanvas.Font.Name := 'Tahoma';
    MFCanvas.Font.Size := 100;
    MFCanvas.Rectangle(0, 0, TRAY_WIDTH, TRAY_DEPTH);
    MFCanvas.TextOut(100,100, 'TESTME');

    finally
    MFCanvas.Free;
    end;

    ppImage1.Picture.Graphic := MetaFile;

    ppReport1.Print;

    finally
    MetaFile.Free;
    end;

    end;

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited October 2017

    Hi Nico,

    I received the email just as I was pulling to the customer site.

    Worked perfectly - many, MANY thanks.

    You'd think that after the dozen years or so that I've used RB I'd be
    used to great support for a great product - but you folks are /always/
    raising the bar - I can't compliment DM enough.

    Thanks.

    EdB

This discussion has been closed.