gpt4 book ai didi

delphi - 将图像加载到 TImageList 并读取它们?

转载 作者:行者123 更新时间:2023-12-03 15:01:56 29 4
gpt4 key购买 nike

我试图通过将 .jpg 转换为 bmp 然后将其保存到 imagelist1 来将 jpg 加载到图像列表中。

从上到下的代码片段。Selectdir 有效,fileexists 部分有效。这用于加载文件夹中的所有图像。所有图像都以 0.jpg/1.jpg 等命名。

然后我将 jpg 加载到一张图片中。设置 bmp 宽度/高度并加载与 jpg 相同的图像的 bmp,然后将 bmp 添加到图像列表。完成后,它应该显示第一张图片 0.jpg

有两个问题,首先如果我这样做的话它只会显示 bmp 的一小部分区域(左上)但这是正确的图像。我认为这是由于选择裁剪。我似乎无法弄清楚如何让它在运行时选择中心?

第二,如果我放

Imagelist1.width := currentimage.width;
Imagelist1.height := currentimage.height;

然后它显示最后一张图片。像 Imagelist1.GetBitmap() 不起作用?所以我认为修复其中任何一个都会很棒!干杯海葱

procedure TForm1.Load1Click(Sender: TObject);
var
openDialog : TOpenDialog;
dir :string;
MyPicture :TPicture;
currentimage :Tbitmap;
image : integer;
clTrans : TColor;
begin
Image := 0 ;
//lets user select a dir
SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP);
myPicture :=Tpicture.Create;
currentimage := TBitmap.Create;
//keeps adding images as long as the file path exsist.
//thus comic pages should be renumbed to 0-XX
while FileExists(Dir+'\'+inttostr(image)+'.jpg') do
begin
try
MyPicture.LoadFromFile(Dir+'\'+inttostr(image)+'.jpg'); //load image to jpg holder
currentimage.Width := mypicture.Width; //set width same as jpg
currentimage.Height:= mypicture.Height; //set height same as jpg
currentimage.Canvas.Draw(0, 0, myPicture.Graphic); //draw jpg on bmp
clTrans:=currentimage.TransparentColor; //unknown if needed?
//Imagelist1.Width := currentimage.Width;
//imagelist1.Height := currentimage.Height;
Imagelist1.Addmasked(Currentimage,clTrans); //add to imagelist
finally
image := image +1; //add one so it adds next page
end;
end;
ImageList1.GetBitmap(0,zImage1.Bitmap);
mypicture.Free;
currentimage.Free;
end;

最佳答案

每次都使用 TImage 会增加很多不必要的开销。

尝试这样的事情(未经测试,因为我没有一个充满这样命名的图像的文件夹 - 它可以编译,尽管 )。当然,如果尚不存在,您需要将 Jpeg 添加到您的实现 uses 子句中。

procedure TForm2.Button1Click(Sender: TObject);
var
DirName: string;
begin
DirName := 'D:\Images';
if SelectDirectory('Select Image Path',
'D:\TempFiles',
DirName,
[sdNewUI],
Self) then
LoadImages(DirName);
end;

procedure TForm2.LoadImages(const Dir: string);
var
i: Integer;
CurFileName: string;
JpgIn: TJPEGImage;
BmpOut: TBitmap;
begin
i := 1;
while True do
begin
CurFileName := Format('%s%d.jpg',
[IncludeTrailingPathDelimiter(Dir), i]);
if not FileExists(CurFileName) then
Break;
JpgIn := TJPEGImage.Create;
try
JpgIn.LoadFromFile(CurFileName);

// If you haven't initialized your ImageList width and height, it
// defaults to 16 x 16; we can set it here, if all the images are
// the same dimensions.
if (ImageList1.Count = 0) then
ImageList1.SetSize(JpgIn.Width, JpgIn.Height);

BmpOut := TBitmap.Create;
try
BmpOut.Assign(JpgIn);
ImageList1.Add(BmpOut, nil);
finally
BmpOut.Free;
end;
finally
JpgIn.Free;
end;
Inc(i);
end;
if ImageList1.Count > 0 then
begin
BmpOut := TBitmap.Create;
try
ImageList1.GetBitmap(0, BmpOut);
Image1.Picture.Assign(BmpOut);
finally
BmpOut.Free;
end;
end;
end;

关于delphi - 将图像加载到 TImageList 并读取它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988310/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com