2012-06-11 37 views
5

我想通过将.jpg转换为bmp并将其保存到imagelist1来将jpg加载到图像列表中。将图像加载到TImageList并读取它们?

从上到下的代码片段。 Selectdir工作和文件存在的部分工作。这是用来加载一个文件夹中的所有图像。所有的图像被命名为像0.jpg/1.jpg等。

然后我加载JPG到一个tpicture。设置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; 

回答

2

你使用每次TImage加入了很多不必要的开销。

尝试类似这样的事情(未经测试,因为我没有一个文件夹中装满了以此方式命名的图像 - 它会编译,尽管<g>)。当然,如果它尚未存在,则需要将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; 
+0

这个工作,一旦它完成加载它不显示任何图像。还必须将image1更改为ZImage1.bitmap.assign(bmpout);由于ZImage1是TGraphicControl的一个类。不知道这是否重要.. –

+0

它似乎永远不会进入while循环。总是爆发 –

+0

如果函数被调用,那么它无法进入'while'循环,这是没有办法的(短时间内忽略了一个异常) '而真'总是'真'。代码不执行它的唯一方法是如果'SelectDirectory'返回false并且该函数不会被调用。如果您在该行上设置断点,调试器会显示什么内容? –

相关问题