Where is wrong ?

I try to make a component .
I load a bmp to PicFrom . All is OK .
But when I try to delete the bmp from PicFrom I still have TBitmap in PicFrom How can I fix that ?
This is my code . Compile and try that .

unit GNSlide;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TGNSlide = class(TGraphicControl)
  private
    { Private declarations }
    FPicFrom:TPicture;
    FPicTo:TPicture;
    picT:TBitmap;
    procedure PictureFrom(Pic:TPicture);
    procedure PictureTo(Pic:TPicture);
    procedure changePicFrom(Sender : TObject);
    procedure changesize(Sender : TObject);
  protected
    { Protected declarations }
    function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Paint; override;
  published
    { Published declarations }
    property PicFrom : TPicture read FPicFrom write PictureFrom;
    property PicTo : TPicture read FPicTo write PictureTo;
    property Autosize ;
  end;

procedure Register;

implementation

constructor TGNSlide.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  FPicFrom := TPicture.Create;
  FPicTo := TPicture.Create;

  Height:=105;
  Width:=105;

  FPicFrom.OnChange := changePicFrom;
  Canvas.OnChanging:= changesize;
end;

function TGNSlide.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
begin
  Result := True ;
  if not (csDesigning in ComponentState) or (PicFrom.Width > 0) and
    (PicFrom.Height > 0) then
  begin
    if Align in [alNone, alLeft, alRight] then
      NewWidth := PicFrom.Width;
    if Align in [alNone, alTop, alBottom] then
      NewHeight := PicFrom.Height;
  end;

end;


procedure TGNSlide.Paint;
begin
  Canvas.Draw(0,0,picT);
end;

procedure TGNSlide.PictureFrom(Pic:TPicture);
begin
 FPicFrom.Assign(Pic);
end;

procedure TGNSlide.PictureTo(Pic:TPicture);
begin
 FPicTo.Assign(Pic);
end;

procedure TGNSlide.changePicFrom(Sender : TObject);
begin
 Width:=FPicFrom.Width;
 Height:=FPicFrom.Height;

  picT := TBitmap.Create;
  picT.Canvas.Brush.Color := clBlack;
  picT.Width := Width;
  picT.Height := Height;

 if AutoSize and (PicFrom.Width > 0) and (PicFrom.Height > 0) then
 SetBounds(Left, Top, PicFrom.Width, PicFrom.Height);
 picT.Assign(FPicFrom.Bitmap);

end;

procedure TGNSlide.changesize(Sender:TObject);
begin
 if Width>PicFrom.Width then Width:=PicFrom.Width;
 if Height>PicFrom.Height then Height:=PicFrom.Height;
end;

destructor TGNSlide.Destroy;
begin
  FPicFrom.Free;
  FPicTo.Free;
  picT.Free;
  inherited Destroy;
end;

procedure Register;
begin
  RegisterComponents('Nick', [TGNSlide]);
end;

end.
LVL 9
ginsonicAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PeterLarsenConnect With a Mentor Commented:
Try this :

TGNSlide = class(TGraphicControl)
private
{ Private declarations }
FPicFrom:TPicture;
FPicTo:TPicture;
picT: tpicture; // change to Tpicture

....

procedure TGNSlide.Paint;
begin
 Canvas.Draw(0,0,PicFrom.Graphic); //use graphic
end;

...

procedure TGNSlide.changePicFrom(Sender : TObject);
begin
Width:=FPicFrom.Width;
Height:=FPicFrom.Height;

//Move this to the components constructor - otherwise you //create a new instance every time the picture is changed.
// picT := TPicture.Create;
// picT.Canvas.Brush.Color := clBlack;
// picT.Width := Width;
// picT.Height := Height;

if AutoSize and (PicFrom.Width > 0) and (PicFrom.Height > 0) then
SetBounds(Left, Top, PicFrom.Width, PicFrom.Height);
picT.Assign(FPicFrom);
end;

...

good luck
0
 
PeterLarsenCommented:
I dont see where and how you delete the bmp.
Is it in designtime you have the problem ??
0
 
ginsonicAuthor Commented:
In design time , yes .
0
 
ginsonicAuthor Commented:
I select PicFrom from ObjectInspector , delete TBitmap , but when I press enter object inspector return TBitmap and not None .
In this time , the component don't display the initial bitmap ( the bitmap is erased from it , but object inspector report that the bitmap is still there ( in PicFrom ) .
0
 
ginsonicAuthor Commented:
Thx , man !
0
All Courses

From novice to tech pro — start learning today.