莆仙生活网
当前位置: 莆仙生活网 > 知识库 >

timage

时间:2024-11-14 06:23:55 编辑:莆仙君

delphi中Timage控件动态加载图片

你可以把加载图片部分使用类似:try Image1.Picture.LoadFromFile(预测的图片路径);except on e: EFOpenError do begin //如果找不到图片。则在这里执行没有图片的提示 end else begin //如果图片因为其他的原因加载错误则在这里处理 end;end;当然,这样做的话,可能调试的时候依然会弹出图片不存在的提示(直接运行时则没有)。如果你想要避开提示的话。也可以在加载图片之前使用FileExists函数来确认文件是否存在,如下:if FileExists(预测的图片路径) thenbegin //如果文件存在则加载图片 Image1.Picture.LoadFromFile(预测的图片路径);endelsebegin //如果找不到图片。则在这里执行没有图片的提示end;


delphi 动态创建的image如何释放

如果动态创建了单个 TImage ,那么可以直接使用 free 来释放,如下所示:procedure TForm1.btn2Click(Sender: TObject);var img:TImage;begin img := TImage.Create(Self); img.Parent := Form1; img.Picture.LoadFromFile('e:\1112.bmp'); img.Left := 176; img.Top := 240; img.Width := 300; img.Height := 300; img.Stretch := True; img.Free;end;如果是动态创建了多个 Image,可以通过遍历 Controls 属性,并检查是否是 TImage,然后动态释放:procedure TForm1.btn1Click(Sender: TObject);var i: Integer;begin for i := ControlCount-1 downto 0 do begin if Controls[i] is TImage then Controls[i].Free; end;end;


delphi控件Image加载图片问题

Delphi的TImage控件在加载图片的时候,采用的是同步加载的方式。也就是主线程会一直处于阻塞状态,直到TImage.Picture.LoadFromFile加载完毕为止。也就是说,如果你的代码运行到了LoadFromFile之后就代表图片已经加载完成了。当然,因为是同步加载,所以主线程在加载图片时,完全没有机会重绘窗口。所以加载大图片时可能会有卡顿。如果你确信你需要加载的图片的体积非常大(图片至少要大于5MB)以上,需要异步地加载图片。你可以创建一个新线程,并在这个线程里创建一个TPicture,然后使用TPicture.LoadFromFile(Stream)来加载图片。然后再这个新线程运行完成后,再经由TImage.Picture := 你在线程中创建的Picture; 的方式来将加载完成的图片对象。示例代码如下:const CM_LoadComplete = WM_USER + 20;type TLoadThread = class(TThread) protected AnycPic: TPicture; procedure Execute; override; public destructor Destroy; override; end; TfrmMain = class(TForm) Image1: TImage; Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FThread: TLoadThread; procedure CMLoadComplete(var Message: TMessage); message CM_LoadComplete; public { Public declarations } end;var frmMain: TfrmMain;implementationuses jpeg, pngimage;{$R *.dfm}{ TfrmMain }procedure TfrmMain.Button1Click(Sender: TObject);begin //开始用线程异步加载图像 if not Assigned(FThread) then begin FThread := TLoadThread.Create(True); FThread.Resume; end else ShowMessage('图像正在加载中...');end;procedure TfrmMain.CMLoadComplete(var Message: TMessage);begin //TPicture异步加载完成,将其设置为Image1的Picture Image1.Picture := FThread.AnycPic; //终止线程 FThread.WaitFor; FreeAndNil(FThread);end;procedure TfrmMain.FormDestroy(Sender: TObject);begin if Assigned(FThread) then begin FThread.WaitFor; FThread.Free; end;end;{ TLoadThread }destructor TLoadThread.Destroy;begin AnycPic.Free; inherited;end;procedure TLoadThread.Execute;begin AnycPic := TPicture.Create; AnycPic.LoadFromFile('D:\test1.jpg'); //如果图像是jpeg则先进行解码工作 if AnycPic.Graphic is TJPEGImage then (AnycPic.Graphic as TJPEGImage).DIBNeeded; //将图像提交至主线程,不要使用Synchronize函数,可能会导致主线程锁死 PostMessage(frmMain.Handle,CM_LoadComplete,0,0);end;


delphi中Image加载图片的问题

1、创建rc文件。可以用任意文本编辑器来写。文件格式为:"资源名 资源类型 文件名"。
对于资源类型,如果是exe文件,应该是,如果是二进制文件,则是RCDATA。
这里创建一个文件float.rc:
代码
ATP BITMAP "E:\Software\1.jpg"
这里顺便记得“
AVI 无声动画
EXEFILE 可执行文件
BITMAP 位图文件
CURSOR 光标文件
ICON 图标文件
WAVE 声音文件”

2、将这个rc文件转换成res文件。
执行brcc32 float.rc,生成float.res

3、将这个res文件包含至工程文件中。
代码
{$R float.res}

4、提取RES中的1.jpg。
代码
procedure TFormMain.loadtpClick(Sender: TObject);
var
t : TResourceStream;
begin
if FileExists('1.jpg') then
image1.bitmap.loadfromfile('1.jpg');
else
begin
try
t := TResourceStream.Create(HInstance,'Atp','bitmap'); //
t.SaveToFile('1.jpg');
finally
t.free;
end;
Wi image1.bitmap.loadfromfile('1.jpg');
end;
end;


Delphi7 image控件图片问题

// 重写一下TImage 把OnMouseEnter和OnMouseLeave事件放出来。// 然后在OnMouseEnter及OnMouseLeave事件中换图片。// 下面是TMYImage,已经把OnMouseEnter和OnMouseLeave事件放出来了。type TMYImage = class(TGraphicControl) private FOnMouseEnter: TNotifyEvent; FOnMouseLeave: TNotifyEvent; FPicture: TPicture; FOnProgress: TProgressEvent; FStretch: Boolean; FCenter: Boolean; FIncrementalDisplay: Boolean; FTransparent: Boolean; FDrawing: Boolean; FProportional: Boolean; function GetCanvas: TCanvas; procedure PictureChanged(Sender: TObject); procedure SetCenter(Value: Boolean); procedure SetPicture(Value: TPicture); procedure SetStretch(Value: Boolean); procedure SetTransparent(Value: Boolean); procedure SetProportional(Value: Boolean); protected function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override; function DestRect: TRect; function DoPaletteChange: Boolean; function GetPalette: HPALETTE; override; procedure Paint; override; procedure Progress(Sender: TObject; Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic; procedure MouseEnter; virtual; procedure MouseLeave; virtual; procedure CM_MouseEnter(var msg: TMessage); message CM_MOUSEENTER; procedure CM_MouseLeave(var msg: TMessage); message CM_MOUSELEAVE; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Canvas: TCanvas read GetCanvas; published property Align; property Anchors; property AutoSize; property Center: Boolean read FCenter write SetCenter default False; property Constraints; property DragCursor; property DragKind; property DragMode; property Enabled; property IncrementalDisplay: Boolean read FIncrementalDisplay write FIncrementalDisplay default False; property ParentShowHint; property Picture: TPicture read FPicture write SetPicture; property PopupMenu; property Proportional: Boolean read FProportional write SetProportional default false; property ShowHint; property Stretch: Boolean read FStretch write SetStretch default False; property Transparent: Boolean read FTransparent write SetTransparent default False; property Visible; property OnClick; property OnContextPopup; property OnDblClick; property OnDragDrop; property OnDragOver; property OnEndDock; property OnEndDrag; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnProgress: TProgressEvent read FOnProgress write FOnProgress; property OnStartDock; property OnStartDrag; property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter; property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave; end;constructor TMYImage.Create(AOwner: TComponent);begin inherited Create(AOwner); ControlStyle := ControlStyle + [csReplicatable]; FPicture := TPicture.Create; FPicture.OnChange := PictureChanged; FPicture.OnProgress := Progress; Height := 105; Width := 105;end;destructor TMYImage.Destroy;begin FPicture.Free; inherited Destroy;end;function TMYImage.GetPalette: HPALETTE;begin Result := 0; if FPicture.Graphic nil thenResult := FPicture.Graphic.Palette;end;function TMYImage.DestRect: TRect;var w, h, cw, ch: Integer; xyaspect: Double;begin w := Picture.Width; h := Picture.Height; cw := ClientWidth; ch := ClientHeight; if Stretch or (Proportional and ((w > cw) or (h > ch))) then beginif Proportional and (w > 0) and (h > 0) thenbegin xyaspect := w / h; if w > h then begin w := cw; h := Trunc(cw / xyaspect); if h > ch then // woops, too big begin h := ch; w := Trunc(ch * xyaspect); end; end else begin h := ch; w := Trunc(ch * xyaspect); if w > cw then // woops, too big begin w := cw; h := Trunc(cw / xyaspect); end; end; end else begin w := cw; h := ch; end; end; with Result do begin Left := 0; Top := 0; Right := w; Bottom := h; end; if Center thenOffsetRect(Result, (cw - w) div 2, (ch - h) div 2);end;procedure TMYImage.Paint;var Save: Boolean;begin if csDesigning in ComponentState thenwith inherited Canvas dobegin Pen.Style := psDash; Brush.Style := bsClear; Rectangle(0, 0, Width, Height);end; Save := FDrawing; FDrawing := True; trywith inherited Canvas do StretchDraw(DestRect, Picture.Graphic); finallyFDrawing := Save; end;end;function TMYImage.DoPaletteChange: Boolean;var ParentForm: TCustomForm; Tmp: TGraphic;begin Result := False; Tmp := Picture.Graphic; if Visible and (not (csLoading in ComponentState)) and (Tmp nil) and(Tmp.PaletteModified) then beginif (Tmp.Palette = 0) then Tmp.PaletteModified := Falseelsebegin ParentForm := GetParentForm(Self); if Assigned(ParentForm) and ParentForm.Active and Parentform.HandleAllocated then beginif FDrawing then ParentForm.Perform(wm_QueryNewPalette, 0, 0)else PostMessage(ParentForm.Handle, wm_QueryNewPalette, 0, 0);Result := True;Tmp.PaletteModified := False; end;end; end;end;procedure TMYImage.Progress(Sender: TObject; Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);begin if FIncrementalDisplay and RedrawNow then beginif DoPaletteChange then Updateelse Paint; end; if Assigned(FOnProgress) then FOnProgress(Sender, Stage, PercentDone, RedrawNow, R, Msg);end;function TMYImage.GetCanvas: TCanvas;var Bitmap: TBitmap;begin if Picture.Graphic = nil then beginBitmap := TBitmap.Create;try Bitmap.Width := Width; Bitmap.Height := Height; Picture.Graphic := Bitmap;finally Bitmap.Free;end; end; if Picture.Graphic is TBitmap thenResult := TBitmap(Picture.Graphic).Canvas elseraise EInvalidOperation.Create(SImageCanvasNeedsBitmap);end;procedure TMYImage.SetCenter(Value: Boolean);begin if FCenter Value then beginFCenter := Value;PictureChanged(Self); end;end;procedure TMYImage.SetPicture(Value: TPicture);begin FPicture.Assign(Value);end;procedure TMYImage.SetStretch(Value: Boolean);begin if Value FStretch then beginFStretch := Value;PictureChanged(Self); end;end;procedure TMYImage.SetTransparent(Value: Boolean);begin if Value FTransparent then beginFTransparent := Value;PictureChanged(Self); end;end;procedure TMYImage.SetProportional(Value: Boolean);begin if FProportional Value then begin FProportional:= Value; PictureChanged(Self); end;end;procedure TMYImage.PictureChanged(Sender: TObject);var G: TGraphic; D : TRect;begin if AutoSize and (Picture.Width > 0) and (Picture.Height > 0) thenSetBounds(Left, Top, Picture.Width, Picture.Height); G := Picture.Graphic; if G nil then beginif not ((G is TMetaFile) or (G is TIcon)) then G.Transparent := FTransparent; D := DestRect;if (not G.Transparent) and (D.Left = Width) and (D.Bottom >= Height) then ControlStyle := ControlStyle + [csOpaque]else // picture might not cover entire clientrect ControlStyle := ControlStyle - [csOpaque];if DoPaletteChange and FDrawing then Update; end else ControlStyle := ControlStyle - [csOpaque]; if not FDrawing then Invalidate;end;function TMYImage.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;begin Result := True; if not (csDesigning in ComponentState) or (Picture.Width > 0) and (Picture.Height > 0) then begin if Align in [alNone, alLeft, alRight] then NewWidth := Picture.Width; if Align in [alNone, alTop, alBottom] then NewHeight := Picture.Height; end;end;procedure TMYImage.MouseEnter;begin if Assigned(FOnMouseEnter) then FOnMouseEnter(Self);end;procedure TMYImage.MouseLeave;begin if Assigned(FOnMouseLeave) then FOnMouseLeave(Self);end;procedure TMYImage.CM_MouseEnter(var msg: TMessage);begin Inherited; MouseEnter;end;procedure TMYImage.CM_MouseLeave(var msg: TMessage);begin Inherited; MouseLeave;end;