2012-06-11 15 views
5

Estoy tratando de cargar jpg en una lista de imágenes convirtiendo .jpg a un bmp y luego guardándolo en imagelist1.Cargando imágenes en TImageList y leyéndolas?

De arriba a abajo del recorte de código. El trabajo y el archivo Selectdirexisten piezas de trabajo. Esto se usa para cargar todas las imágenes en una carpeta.Todas las imágenes se nombran como 0.jpg/1.jpg ect ..

A continuación, carga el jpg en una imagen. Establezca el ancho/alto de bmp y cargue el bmp con la misma imagen que jpg, luego agrego el bmp a la lista de imágenes. Y cuando esté hecho debería mostrar la primera imagen 0.jpg

Dos problemas, primero si lo hice así, solo mostraría una pequeña área (arriba a la izquierda) de la bmp pero era la imagen correcta. Supongo que esto se debe a la opción de recorte. que no puedo entender cómo hacer que seleccione centro durante el tiempo de ejecución?

En segundo lugar, si pongo

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

A continuación se muestra la última imagen. como Imagelist1.GetBitmap() no funcionó? así que supongo que una solución para cualquiera sería genial. aplausos 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; 

Respuesta

2

va a añadir un montón de gastos innecesarios mediante el uso de la TImage cada vez.

Pruebe algo como esto (no probado, porque no tengo una carpeta llena de imágenes llamadas de esta manera - compila, aunque <g>). Tendrá que agregar Jpeg a su cláusula de implementación uses si aún no está allí, por supuesto.

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

esto funciona, hasta cierto punto, una vez que se ha terminado de cargar, no muestra ninguna imagen. también tuve que cambiar image1 a ZImage1.bitmap.assign (bmpout); Como ZImage1 es una clase de TGraphicControl. no estoy seguro si esto importa ... –

+0

parece que nunca entra en el ciclo while. Siempre se rompe –

+0

No hay forma (a excepción de una excepción que esté ignorando) de que no pueda entrar en el bucle 'while' si se llama a la función; 'while True' siempre es' True'. La única forma de que el código no se ejecute es si 'SelectDirectory' devuelve falso y la función no se llama como resultado. ¿Qué le muestra el depurador si establece un punto de interrupción en esa línea? –

Cuestiones relacionadas