본문 바로가기
Application/Delphi Lecture

GDI+ 를 이용해서 썸네일 만들기.

by 현이빈이 2008. 8. 13.
반응형

uses GDIPOBJ, GDIPAPI;


const
  ImageBMPClsid        : TGUID = '{557CF400-1A04-11D3-9A73-0000F81EF32E}';
  ImageJPEGClsid       : TGUID = '{557CF401-1A04-11D3-9A73-0000F81EF32E}';
  ImageGIFClsid        : TGUID = '{557CF402-1A04-11D3-9A73-0000F81EF32E}';
  ImageTIFFClsid       : TGUID = '{557CF405-1A04-11D3-9A73-0000F81EF32E}';
  ImagePNGClsid        : TGUID = '{557CF406-1A04-11D3-9A73-0000F81EF32E}';



function TForm1.setMakeThumbnailWithGDI(strFileFullname,
  strFilepath: string; intWidth, intHeight: integer): string;
var
  graphics: TGPGraphics;
  Image : TGPImage; //TGPImage;
  tImage : TGPImage;
  strSaveFilefullname : string;
  intXZoom, intYZoom : real;
  nPosX, nPosY : integer;
begin

  try
    if (strFileFullname <> '') and (fileExists(strFileFullname)) then
    begin
      try
        tImage := TGPImage.create();
        Image  := TGPImage.Create(strFilefullname);

        intXZoom := intWidth / Image.GetWidth();
        intYZoom := intHeight / Image.GetHeight();


        if intXZoom > intYZoom then
        begin
          nPosX := round(image.GetWidth() * intYZoom);
          nPosY := intHeight;
        end
        else
        begin
          nPosX := intWidth;
          nPosY := round(image.GetHeight() * intXZoom);
        end;

        //tImage := image.GetThumbnailImage(intWidth, intHeight, nil, nil);
        tImage := image.GetThumbnailImage(nPosX, nPosY, nil, nil);
        //graphics := TGPGraphics.Create(imgView.Canvas.Handle);
        //graphics.DrawImage(JPEGImage, 0, 0, JPEGImage.GetWidth, JPEGImage.GetHeight);

        strSaveFilefullname := strFilepath + extractFilename(strFileFullname);
        tImage.Save(strSaveFilefullname, ImageJPEGClsid);

         //imgView.Picture.SaveToFile(strSaveFilefullname);
      finally
        Image.free;
        tImage.Free;
        //freeAndNil(graphics);
      end;
    end
    else
    begin
      result := '';
    end;
  except
    strSaveFilefullname := '';
  end;
  Result := strSaveFilefullname;

end;

반응형