-1

I have subclassed ExtCtrls.TPaintBox with several help functions and overridden the Paint method. I can add a TPaintBox to a form which then acts as my custom paintbox object and draws the desired output.

Now I want to draw (write) the contents of my paintbox to a file, but with different dimensions; e.g. inside the UI of my application the paintbox has size 150x600 (width x height), but when drawing to the file, I need it to be larger.

I want to be able to reuse my drawing code (= TPaintBox.Paint) and virtually draw it to an object and then save that object to a file.

I'm already able to export it, but resizing on export makes the image look like you would enlarge it with paint.

user729103
  • 631
  • 2
  • 10
  • 24
  • 2
    Can you show your `TPaintBox.Paint()` method? If you are only drawing vector objects then you will need to re-draw them at higher resolution for a larger output. If you are using bitmap data then you need to create or retain high-resolution data from whatever original source. You cannot otherwise enlarge bitmap data *and* increase resolution - you need a high resolution source to begin with. The only other alternative might be some clever post-processing to anti-alias the larger image. – J... Jun 10 '13 at 14:49
  • Like J said, you should be going from big to small not the other way around. – Peter Jun 10 '13 at 15:28
  • I am indeed drawing vector objects (Rectangle, Pie) and that is exactly what I need: output in a higher resolution than currently shown on screen. – user729103 Jun 11 '13 at 06:14

1 Answers1

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;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • how does this help him achieve that? He needs the graphics to scale with the Canvas. – Peter Jun 11 '13 at 06:20
  • @Peter No amount of code can make that happen by magic. The drawing code has to be able to draw to fit the size of the target canvas. The asker has to do the bit. Use Canvas.ClipRect to find out what size to draw. So no hard coded coordinates. All coords scaled according to ClipRect. – David Heffernan Jun 11 '13 at 06:24
  • I'm well aware that magic doesn't exists thanks for that. I just wanted you to explain to me why you think that this answer answers his question. – Peter Jun 11 '13 at 06:33
  • @Peter I added an extra intro paragraph. Do you understand now? – David Heffernan Jun 11 '13 at 06:38