将图像加载到TImageList并阅读它们? [英] Loading images to TImageList and Reading them?
问题描述
我正在尝试通过将.jpg转换为bmp然后将其保存到imagelist1来将jpg加载到图像列表中。
I am trying to load jpg into an imagelist by converting the .jpg to a bmp and then saving it to imagelist1.
从上到下的代码剪辑。
Selectdir作品和fileexists的作品。这是用来加载在一个文件夹中的所有图像。所有的图像命名如so 0.jpg / 1.jpg ect ..
From top to bottom of the code snip. The Selectdir works and fileexists parts work. This is used to load in all the Images in a folder.All images are named like so 0.jpg / 1.jpg ect..
然后我加载jpg一个tpicture。设置bmp宽度/高度,并加载与jpg相同的图像的bmp,然后将bmp添加到imagelist。当它完成它应该显示第一个图像0.jpg
I then load the jpg to a tpicture. Set the bmp width /height and load the bmp with same image as jpg , i then add the bmp to the imagelist. And when its done it should show the first image 0.jpg
两个问题,首先如果我这样做,它只会显示一个小区域(左上) bmp
但它是正确的图像。我认为这是因为选择作物。我不明白如何在运行时选择中心?
Two issues, first if i did it like so it would only show a small area (top left) of the bmp but it was the correct image. I assume this is due to the option crop. which i cant seem to figure out how to make it select center during runtime?
其次,如果我把
Imagelist1.width := currentimage.width;
Imagelist1.height := currentimage.height;
然后它显示最后一个图像。像 Imagelist1.GetBitmap()
没有工作?
所以我假设任何一个修复将是伟大的!
cheers
squills
Then it shows last image. like Imagelist1.GetBitmap()
did not work?
so i assume a fix for either one would be great!
cheers
squills
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
You're adding a lot of unnecessary overhead by using the TImage
every time.
尝试这样的东西(未经测试,因为我没有一个文件夹已满以这种方式命名的图像 - 它编译,尽管< g>)。当然,您需要将 Jpeg
添加到您的实现中使用
子句。 >
Try something like this (untested, because I don't have a folder full of images named this way - it compiles, though <g>). You'll need to add Jpeg
to your implementation uses
clause if it's not already there, of course.
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;
这篇关于将图像加载到TImageList并阅读它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!