Board index » delphi » Retrieving TCanvas contents into a TBitmap

Retrieving TCanvas contents into a TBitmap

There doesn't seem to be functionality in the VCL to accomplish this, so
I'm wondering if anyone has a clue as to how to take whatever is drawn on a
Tcanvas and load it into a TBitmap.  I'm thinking there must be a way using
the device context of the canvas and GDI functions but I'm having no luck
putting it all together.

Thanks for any help.

Matt

 

Re:Retrieving TCanvas contents into a TBitmap


Quote
Matt Olson wrote:

> There doesn't seem to be functionality in the VCL to accomplish this, so
> I'm wondering if anyone has a clue as to how to take whatever is drawn on a
> Tcanvas and load it into a TBitmap.  I'm thinking there must be a way using
> the device context of the canvas and GDI functions but I'm having no luck
> putting it all together.

> Thanks for any help.

> Matt

Are you talking like a TImage.Canvas here?  It doesn't seem so and I'm
assuming that you're talking like a Form.Canvas, maybe?  If so, how
about just placing a TImage on the form and copying the Form's canvas
directly to the Image's canvas and then using the Image.Picture.Bitmap?

It would help to have a little more information.

Derek

Re:Retrieving TCanvas contents into a TBitmap


Quote
Matt Olson wrote:

> There doesn't seem to be functionality in the VCL to accomplish this,
> so
> I'm wondering if anyone has a clue as to how to take whatever is drawn
> on a
> Tcanvas and load it into a TBitmap.  I'm thinking there must be a way
> using
> the device context of the canvas and GDI functions but I'm having no
> luck
> putting it all together.

> Thanks for any help.

> Matt

How about Bitmap.Canvas.CopyRect(DestRect, SourceCanvas, SourceRect)

Andrew

Re:Retrieving TCanvas contents into a TBitmap


Your exact solution came to me last night as I looked this over some more.
I'm actually trying to access the canvas of a DBChart control in D3 for
reporting.  I know the DBChart has print functions but they are limited as
far as formatting the entire page, adding text, etc.  Thanks for your
response.

Derek Benner <dben...@pacbell.net> wrote in article
<3426F404.2F04E...@pacbell.net>...

Quote
> Matt Olson wrote:

> > There doesn't seem to be functionality in the VCL to accomplish this,
so
> > I'm wondering if anyone has a clue as to how to take whatever is drawn
on a
> > Tcanvas and load it into a TBitmap.  I'm thinking there must be a way
using
> > the device context of the canvas and GDI functions but I'm having no
luck
> > putting it all together.

> > Thanks for any help.

> > Matt

> Are you talking like a TImage.Canvas here?  It doesn't seem so and I'm
> assuming that you're talking like a Form.Canvas, maybe?  If so, how
> about just placing a TImage on the form and copying the Form's canvas
> directly to the Image's canvas and then using the Image.Picture.Bitmap?

> It would help to have a little more information.

> Derek

Re:Retrieving TCanvas contents into a TBitmap


Hello, Matt!

Quote
> There doesn't seem to be functionality in the VCL to accomplish this, so
> I'm wondering if anyone has a clue as to how to take whatever is drawn on a
> Tcanvas and load it into a TBitmap.

I have done it, to save to a bmp file one graph image painted directly
to the canvas of a component. Try with this function or something like
it (its in spanish language, but I think you will understand it):

Procedure TGrafica.GuardarImagen(NombreFichero:String);
Var Mapa:TBitmap;
begin
   Screen.Cursor:=crHourGlass;
   Repaint;
   Mapa:=TBitmap.Create;
   Mapa.Width:=Width;
   Mapa.Height:=Width;
   Mapa.Canvas.CopyRect(ClientRect,Canvas,ClientRect);
   Mapa.SaveToFile(NombreFichero);
   Mapa.Destroy;
   Screen.Cursor:=crDefault;
end;

Good luck

        sa...@udc.es

Other Threads