Page 1 of 1

Setting the size of a TChart when saved/copied to clipboard

Posted: Thu Jan 21, 2016 11:47 am
by 10548358
We have a number of charts in our application which we allow the user to generate a report in Word from. We do this by calling CopyToClipboardBitmap on the TChart and then pasting it into the Word document at the appropriate location.

However, we're having issues that the resolution of the bitmaps are based on their currently rendered size on screen. So people on low resolution desktops, or who happen to not be running the app maximized end up with quite low resolution images in the generated Word document.

We'd like to be able to set the resolution before doing the CopyToClipboardBitmap. Is there anyway to do this?

Note, we've tried using CopyToClipboardMetafile (with both true and false as the argument) and that seems to have similar issues in that the size of the image is based on the rendered size on screen. This results in the text for the axis, labels, etc. being overly large when made larger within the document. So we're back to wanting to be able to specify the render size that the CopyToClipboardBitmap / CopyToClipboardMetafile / SaveToBitmapFile / SaveToMetafile / SaveToMetafileEnh is based upon.

Code: Select all

procedure copyToClipboard(const aChart: TChart);
var
	guid: TGUID;
    str: String;
begin
	SysUtils.CreateGUID(guid);
	str := StringUtils.ToStr(guid);

	aChart.CopyToClipboardBitmap();
    aChart.SaveToBitmapFile('C:\temp\' + str + '.bmp'); // DEBUG
//	aChart.CopyToClipboardMetafile(false);
    aChart.SaveToMetafile('C:\temp\' + str + '.wmf'); // DEBUG
//	aChart.CopyToClipboardMetafile(true);
    aChart.SaveToMetafileEnh('C:\temp\' + str + '.emf'); // DEBUG
end;

Re: Setting the size of a TChart when saved/copied to clipboard

Posted: Thu Jan 21, 2016 2:50 pm
by yeray
Hello,

You can set a TRect as parameter for the CopyToClipboardBitmap method. Ie:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var tmpRect: TRect;
    tmpWidth, tmpHeight: Integer;
begin
  tmpRect:=Chart1.GetRectangle;
  tmpWidth:=tmpRect.Right-tmpRect.Left;
  tmpHeight:=tmpRect.Bottom-tmpRect.Top;
  tmpRect.Right:=tmpRect.Right+tmpWidth;
  tmpRect.Bottom:=tmpRect.Bottom+tmpHeight;
  Chart1.CopyToClipboardBitmap(tmpRect);
end;