1

I have written a Print Preview option for a larger application. Mostly it works to my satisfaction, except sometimes the image is truncated.

The code below is a much cut-down version that shows the problem. I have a TPanel on which I am putting a TImage, which for now is just a white rectangle with a red line going from the top left corner to the bottom right.

In the larger application, I want to choose between A4 portrait size and A4 landscape. My little app here has two buttons to choose which page size is required.

Here's the problem. When I start the app and select Portrait, it works correctly:

portrait-portrait

But, when I then click Landscape, the image is truncated. You can see which way the red line is going:

portrait-landscape

On the other hand, when I stop the app and restart it and select Landscape, it works ok:

landscape-landscape

When I then click Portrait, it is this image that is now truncated:

landscape-portrait

How can I get this to work?

My code is below:

const float A4width = 8.27/4, A4height = 11.69/4;

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
    image = new TImage(Owner);
    image->Parent = Panelpreview;
    image->Left = 16;
    image->Top = 16;
}

// Portrait button click
void __fastcall TForm1::BportraitClick(TObject *Sender)
{
    fheight = A4height;
    fwidth = A4width;

    showPreview();
}

//Landscape button click
void __fastcall TForm1::BlandscapeClick(TObject *Sender)
{
    fheight = A4width;
    fwidth = A4height;

    showPreview();
}

// Draw the picture
void TForm1::showPreview()
{
    int pagewidth, pageheight;
    TRect imagerect;

    pagewidth = fwidth * Canvas->Font->PixelsPerInch;
    pageheight = fheight * Canvas->Font->PixelsPerInch;
    imagerect = TRect(0, 0, pagewidth, pageheight);

    image->Width = pagewidth;
    image->Height = pageheight;

    image->Canvas->Brush->Color = clWhite;
    image->Canvas->FillRect(imagerect);
    image->Canvas->Pen->Color = clRed;
    image->Canvas->MoveTo(0, 0);
    image->Canvas->LineTo(pagewidth, pageheight);
}
3
  • A minimal reproducible example would be nice.. Commented Nov 10 at 14:00
  • @JesperJuhl, this is as minimally reproducible as I could make it. Commented Nov 10 at 14:43
  • 2
    "this is as minimally reproducible as I could make it" - obviously not since there's no main() function. You could obviously reduce this down to something that we could compile and that would just be main() calling a few functions (if you couldn't manage to inline them) - there shouldn't be anything else. But, at the very least, we should be able to compile what you posted. Commented Nov 10 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.