-1

我已经ExtCtrls.TPaintBox用几个帮助函数进行了子类化并覆盖了该Paint方法。我可以将 a 添加TPaintBox到表单中,然后将其用作我的自定义画框对象并绘制所需的输出。

现在我想将我的画框的内容绘制(写入)到一个文件中,但尺寸不同;例如,在我的应用程序的 UI 中,paintbox 的大小为 150x600(宽 x 高),但是当绘制到文件时,我需要它更大。

我希望能够重用我的绘图代码(= TPaintBox.Paint)并将其虚拟绘制到an object然后将该对象保存到文件中。

我已经能够导出它,但是在导出时调整大小会使图像看起来像你会用油漆放大它。

4

1 回答 1

1

Your paint box OnPaint event handler is probably dedicated to painting to the size of the paint box. You need to generalize the painting code to be able to draw to a general canvas whose size is only known at runtime. That way you can draw to the low resolution paint box and the high resolution file with the same painting code.

Extract the code inside your OnPaint event handler into a separate method that looks like this:

procedure TForm1.DoPaintBoxPaint(Canvas: TCanvas);
begin
  // All painting code goes here. Use Canvas.ClipRect to infer size of canvas.
end;

Then call this method from your OnPaint handler. Pass PaintBox1.Canvas as the parameter to the method.

In outline that looks like this:

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  DoPaintBoxPaint(PaintBox1.Canvas);
end;

Finally you can call the method from the method that saves the image to file. In that case I assume you have a temporary bitmap on which to draw the image before saving. Pass the canvas of that bitmap. A sketch of that code would be:

Bitmap := TBitmap.Create;
try
  Bitmap.SetSize(Width, Height);
  DoPaintBoxPaint(Bitmap.Canvas);
  Bitmap.SaveToFile(...);
finally
  Bitmap.Free;
end;
于 2013-06-10T14:51:06.860 回答