当前位置 : 主页 > 编程语言 > delphi >

delphi – 在TgraphicControl中将jpg设置为位图

来源:互联网 收集:自由互联 发布时间:2021-06-23
尝试将背景图像添加到TGraphicControl. TCard(TGraphicControl) Private BitMap1:TBitMap; {Used to store a card image}Public procedure SetBitmap(image: TBitmap); …… procedure TCard.SetBitmap(image: TBitmap); begin bitmap1 := Tbitma
尝试将背景图像添加到TGraphicControl.

TCard(TGraphicControl)

Private
  BitMap1:TBitMap;       {Used to store a card image}
Public
  procedure SetBitmap(image: TBitmap);

……

procedure TCard.SetBitmap(image: TBitmap);
 begin
   bitmap1 := Tbitmap.create();
   bitmap1.Assign(image);
   canvas.draw(0,0,bitmap1);
 end;

在表单1按钮单击,我想将图像添加到tcard组件

procedure TForm1.Button1Click(Sender: TObject);
var
  image : Tbitmap;
  jpg: TJpegImage;
begin
  image := TBitmap.create();
  jpg := Tjpegimage.Create();
  jpg.LoadFromFile(dir+'\pics\backcard.jpg');
  image.Assign(jpg);
  card1.setbitmap(image);
  card1.Repaint;
  image.Destroy;
  jpg.Destroy;
 end;

当我跑步时,没有任何反应.如何将此图像添加到TCard组件的背景中?

下面是为TGraphicControl后代设置背景图像的示例,以及使用它的示例(它使用表单的OnCreate中的TBitmap,以及Button1Click中的TJpegImage来演示两者).它只需要一个新的空白VCL表单应用程序,其上有一个TButton可以启动它.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Jpeg, StdCtrls;

type
  TCard = class(TGraphicControl)
  private
    FBackGround: TBitmap;
    procedure SetBackground(Value: TBitmap); overload;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Paint; override;
  published
    property BackGround: TBitmap read FBackGround write SetBackground;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FCard: TCard;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TCard }

constructor TCard.Create(AOwner: TComponent);
begin
  inherited;
  FBackGround := TBitmap.Create;
end;

destructor TCard.Destroy;
begin
  FBackground.Free;
  inherited;
end;

procedure TCard.Paint;
begin
  inherited;
  Self.Canvas.StretchDraw(Self.ClientRect, FBackGround);
end;

procedure TCard.SetBackground(Value: TBitmap);
begin
  FBackGround.Assign(Value);
  //Self.SetBounds(Left, Top, FBackGround.Width, FBackGround.Height);
  Invalidate;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Image: TJPEGImage;
  Bmp: TBitmap;
begin
  Image := TJPEGImage.Create;
  Bmp := TBitmap.Create;
  try
    Image.LoadFromFile(PathToSomeJPEGFile);
    Bmp.Assign(Image);
    FCard.BackGround := Bmp;
  finally
    Bmp.Free;
    Image.Free;
  end;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  Bmp: TBitmap;
begin
  FCard := TCard.Create(Self);
  FCard.Parent := Self;
  Bmp := TBitmap.Create;
  try
  // Load a standard image from the backgrounds folder (D2007).
    Bmp.LoadFromFile('C:\Program Files (x86)\Common Files\CodeGear Shared\Images\BackGrnd\GREENBAR.BMP');
    FCard.BackGround := Bmp;
  finally
    Bmp.Free;
  end;
end;


end.
网友评论