I got ghost image when load an image into imageviewer by click item in listview

  

Question: How can I remove afterimage?

Detail:
I’m using delphi xe10. I’m making simple imageview app.
Computer specification: I7 6700HQ, Ram16G. Windows10, 64bit.

I set Opendialog, ImageViewer, ListView, Button on the form. Did not change default name cause it’s for test.

Below is procedure of this program.
1. click button for setting directory.
2. Then I can see filenames in the directory at Listview
3. click item in listview.
4. Can see image in ImageViewer

It is working well. But afterimage is remained like a mosaic. So, I have tried

image1.free;
image1.canvas.free;
image1.bitmap.free;

finally, add below code. But didn’t work.

if Assigned(ImageViewer1.Bitmap) then
begin
ImageViewer1.Bitmap.Clear(0);
end;

Anything I need to try something more?

Here is my complete code.

unit app4VIew;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.ListView, FMX.Layouts,
FMX.ExtCtrls, FMX.Objects;

type
TForm1 = class(TForm)
ListView1: TListView;
SpeedButton1: TSpeedButton;
OpenDialog1: TOpenDialog;
ImageViewer1: TImageViewer;
procedure SpeedButton1Click(Sender: TObject);
procedure ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
chosenDirectory: string;
SR: TsearchRec;
Litem: TListViewItem;

implementation

{$R *.fmx}

{ In the form There are Opendialog, ImageViewer, ListView. *}

//when click item in listview, imagefile appear in
procedure TForm1.ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
var
Image1: TBitmap;

begin
//if already imageviewer1.bitmap assigned then free it for initializing imageviewer
//Question1: I suppose it make easy memory load or some conflict. Just feeling. Am I right?
if Assigned(ImageViewer1.Bitmap) then
begin
ImageViewer1.Bitmap.Clear(0);
end;

//Question2: There is afterimage look like mosaic. Why? I freed image before load.
try
Image1 := TBitmap.CreateFromFile(chosenDirectory + ” + ListView1.Items
[ListView1.Selected.Index].Text);
Image1.Resize(round(ImageViewer1.Width)-100, round(ImageViewer1.Canvas.Height)-100);
ImageViewer1.Bitmap.Assign(Image1);
finally
Image1.Free;
end;
end;

//List files in selected directory to Listview
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
Res: Integer; // count number of files

begin
if SelectDirectory(‘Select a directory’, ‘C:’, chosenDirectory) then
begin
//find file in the Directry and assign the result to SR
Res := FindFirst(chosenDirectory + ‘*.*’, faAnyFile, SR);
if Res = 0 then
repeat
//and give the result SR to ListView as Itrm as SR.name
Litem := ListView1.Items.Add();
Litem.Text := SR.Name;
until FindNext(SR) <> 0;
FindClose(SR);
end;

end;

end.

Comments are closed.