Add a bmp or jpg figure to a canvas in Delphi

Asked

Viewed 1,153 times

3

It is possible to insert, via programming, a jpg or bmp image on a canvas?
I am using the canvas in a Tbitmap at programming time because my application has no screen. It is called, creates the drawing according to parameters passed and exits.
Below is a part of my code:

var
  bCan: TBitmap;
begin
  bCan := TBitmap.Create;
  bCan.Width := 800;
  bCan.Height := 500;
  bCan.Canvas.Lock;

  // Desenhos diversos com linhas e retângulos

  bCan.Canvas.Unlock;
end;

2 answers

1

It depends on where the image you want to insert in is bCan.

Say it is in a directory, you can use the command bCan.LoadFromFile

Take a look at the variations of the command LoadFrom... of TBitmap

Edit:

Take a look, see if this fits you:

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    btnPosicionarImagem: TButton;
    btnCarregarImagem: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnPosicionarImagemClick(Sender: TObject);
    procedure btnCarregarImagemClick(Sender: TObject);
  private
    Image: TBitmap;
    Canvas: TControlCanvas;

    procedure CarregarImagemBitmap;
    procedure PosicionarPainel;
    procedure CarregarImagemCanvas;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}




procedure TForm2.FormCreate(Sender: TObject);
begin
  Image  := TBitmap.Create;
  Canvas := TControlCanvas.Create;
end;



procedure TForm2.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Canvas);
  FreeAndNil(Image);
end;



procedure TForm2.CarregarImagemBitmap;
begin
  Image.LoadFromFile('C:\executaveis_01_256x256.bmp');
end;



procedure TForm2.PosicionarPainel;
begin
  Panel1.Width  := Image.Width;
  Panel1.Height := Image.Height;

  Panel1.Left := Random(100);
  Panel1.Top  := Random(100);
end;



procedure TForm2.CarregarImagemCanvas;
begin
  Canvas.Control := Panel1;
  BitBlt(Canvas.Handle, 0, 0, Image.Width, Image.Height, Image.Canvas.Handle, 0, 0, SRCCOPY);
end;



procedure TForm2.btnPosicionarImagemClick(Sender: TObject);
begin
  CarregarImagemBitmap;
  PosicionarPainel;
end;



procedure TForm2.btnCarregarImagemClick(Sender: TObject);
begin
  CarregarImagemCanvas;
end;

end.
  • Victor, I’m sorry for the delay... In the tests I did the program plays the image loaded by "Loadfromfile" into Tbitmap, leaving only the image. It does not allow me to position the image within the canvas area in the position I need... Thanks for the help.

  • @Rodrigotognin, take a look at the example I just posted

0


From the comments of the other answer, you want to insert a bitmap in a specific position of a canvas. The question is not how to load the bitmap, correct? If so, just use this command:

Canvas.Draw(x, y, bitmap);
  • This does not answer the question. It would be better if you explained the solution you found.

  • 3

    @Asurakhan ah, you were faster than me.

  • @Acklay is the coffee. It’s working.

  • 1

    @Asurakhan was already written, just click on the send, when appeared... "new comment"... hehe

  • Thanks, it worked out here. In my case it was a Tpicture, so I used img.bitmap.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.