Встала задача – создать thumbnails для полноформатных изображений. В сети уйма информации, не знал за что хвататься, решил пробовать по очереди. В сущности, попробовал 2 способа более менее подробно, а остальные по верхам. Остановился на стандартном StretchDraw из Delphi, из за того, что GDI+ почему-то в дополнительном потоке рисовала белые прямоугольники. Я не стал это глубоко раскапывать, так как нужно было решать задачу.
В результате получим кучу уменьшенный фотографий практически из любых форматов. Я тестил с Jpg, PNG, Tiff, с другими думаю тоже будет работать, так как для загрузки используется twicimage.
Коды приведенные ниже иллюстрируют уменьшение большого числа изображений. Как это работает?
Выбираем директорию
Запускается отдельный поток, в котором в цикле уменьшается большое число изображений
Всё складывается в папочку \thumbnaiPics
Я использовал формат twicimage, который позволяет загружать большое число форматов, ну и в программе уже в ручную ставил фильтр на сами форматы.
Как уменьшить одно изображение с помощью библиотеки GDI+?
1 2 3 |
... uses GDIPOBJ ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//-------------Diminish one pic GDI+--------------- procedure TDiminishPicsThread.LoadAAImageFromFile(FileName: String; W,H: Integer; Pic: TBitmap); var ImageTemp: TGPImage; graphicsGDIPlus: TGPGraphics; begin if FileExists(FileName) then begin Pic.Width:=W; Pic.Height:=H; graphicsGDIPlus:=TGPGraphics.Create (Pic.Canvas.Handle); ImageTemp:=TGPImage.Create(FileName ); // Trouble here graphicsGDIPlus.DrawImage(ImageTemp , 0,0,W,H); ImageTemp.Free; graphicsGDIPlus.Free; end; end; |
В Filename отправляем путь до файла, который надо уменьшить, в Pic получаем уменьшенное изображение, которое можно как-то сохранить, например вот так…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
procedure TDiminishPicsThread.GDIplusDiminishPic(FilePath:string); var thumbnailPicsDir: string; bmp, bmpDiminished: TBItmap; twic:TWICImage; maxWidth:integer; maxHeight:integer; currentWidth:integer; currentHeight:integer; koeff:Extended; NewWidth: integer; NewHeight: integer; CurrentFileName: string; FileExtension: string; NewFileName: string; begin //Checks... if not TFile.Exists(FilePath) then exit; //Creating dir for Thumbs if not Existed thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\thumbnailPics'; if not TDirectory.Exists( thumbnailPicsDir) then TDirectory.CreateDirectory(thumbnailPicsDir); //---- //Getting FileExtension FileExtension:=ExtractFileExt(FilePath); //Setting MaxSize On the Exit... maxWidth:=50; maxHeight:=50; twic:=TWICImage.Create; bmp:= Tbitmap.Create; bmpDiminished:= Tbitmap.Create; try twic.LoadFromFile(FilePath); bmp.Assign(twic); currentWidth:=bmp.Width; currentHeight:=bmp.Height; //Calculating koeff if (currentWidth>maxWidth) or (currentHeight>maxHeight) then koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else koeff:=1; // New width and height NewWidth:=Trunc(koeff*currentWidth); NewHeight:=Trunc(koeff*currentHeight); CurrentFileName:=ExtractFileName(FilePath); NewFileName:=ChangeFileext( CurrentFileName, '_thumb.'+FileExtension ); // <<NewExtension LoadAAImageFromFile(FilePath,NewWidth,NewHeight,bmpDiminished); bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName); finally FreeAndNil(bmp); FreeAndNil(bmpDiminished); FreeAndNil(twic); // ShowMessage('success'); end; end; |
Если вызвать этот код для 1 файла, то всё прекрасно отработает. Но запустить его в потоке, скажем так…
Как уменьшить много картинок в отдельном потоке с GDI+?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
procedure TDiminishPicsThread.Execute; var iTick:integer; i: Integer; begin Synchronize( procedure begin FThreadStarter.ProgressBar.Max:=High(PicsInDir); end ); iTick:=GetTickCount; for i := 0 to High(PicsInDir) do begin // Anonymously updating progressBar Synchronize( procedure begin FThreadStarter.ProgressBar.Position:= FThreadStarter.ProgressBar.Position+1; end ); // DiminishPic(PicsInDir[i]); ( GDIplusDiminishPic(FilesInDir[i])); // <<Works unstable Randomly // Anonymously updating caption Synchronize( procedure begin ThreadStarter.Caption:= 'Npics / total = '+i.ToString+' / ' +length(FPicsArray).ToString+ ' Time(ms)='+IntToStr(GetTickCount-iTick); end ); end; fEx := ExceptObject as Exception; Synchronize( QueryError ); end; |
Код запуска дополнительного потока из основного
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
procedure TfTestDiminshPics.bMultiDiminishPicsClick(Sender: TObject); var FilesInDir,FilesInDirFiltered:TStringDynArray; i: Integer; Ext: string; DiminishPicsThread:TDiminishPicsThread; AllowedExtensions:string; begin AllowedExtensions:='.bmp,.jpg,.jpeg,.png,.tiff'; //Choosing only folders FileOpenDialog.Options := [fdoPickFolders]; // Getting All files from dir if FileOpenDialog.Execute then FilesInDir:= TDirectory.GetFiles(FileOpenDialog.FileName); //Filtering only pics... in FilesInDirFiltered SetLength(FilesInDirFiltered,0); // nulling... for i := 0 to High(FilesInDir) do begin Ext:=AnsiLowerCase(ExtractFileExt(FilesInDir[i])); if Pos(Ext,AllowedExtensions)>0 then begin SetLength(FilesInDirFiltered,Length(FilesInDirFiltered)+1); FilesInDirFiltered[ High(FilesInDirFiltered) ]:=FilesInDir[i]; end; end; //--------------Starting additional thread if Length(FilesInDir)>0 then begin ProgressBar.Position:=0; //Creating additional thread DiminishPicsThread:=TDiminishPicsThread.Create(true); DiminishPicsThread.FreeOnTerminate:=true; DiminishPicsThread.Priority:=tpHigher; //Transferring params to additional thread DiminishPicsThread.ThreadStarter:=Self; DiminishPicsThread.PicsInDir:=FilesInDirFiltered; DiminishPicsThread.MaxWidth:=50; DiminishPicsThread.MaxHeight:=50; DiminishPicsThread.Start; end; end; |
то в моем случае, этот код стал давать белые прямоугольники вместо картинок… Немного поэкспериментировав, я пришел к тому, что стандартная StretchDraw функция из Delphi вполне выполняет мою задачу.
Как уменьшить много картинок в отдельном потоке при помощи StretchDraw?
Код запуска потока (точно такой же)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
procedure TfTestDiminshPics.bMultiDiminishPicsClick(Sender: TObject); var FilesInDir,FilesInDirFiltered:TStringDynArray; i: Integer; Ext: string; DiminishPicsThread:TDiminishPicsThread; AllowedExtensions:string; begin AllowedExtensions:='.bmp,.jpg,.jpeg,.png,.tiff'; //Choosing only folders FileOpenDialog.Options := [fdoPickFolders]; // Getting All files from dir if FileOpenDialog.Execute then FilesInDir:= TDirectory.GetFiles(FileOpenDialog.FileName); //Filtering only pics... in FilesInDirFiltered SetLength(FilesInDirFiltered,0); // nulling... for i := 0 to High(FilesInDir) do begin Ext:=AnsiLowerCase(ExtractFileExt(FilesInDir[i])); if Pos(Ext,AllowedExtensions)>0 then begin SetLength(FilesInDirFiltered,Length(FilesInDirFiltered)+1); FilesInDirFiltered[ High(FilesInDirFiltered) ]:=FilesInDir[i]; end; end; //--------------Starting additional thread if Length(FilesInDir)>0 then begin ProgressBar.Position:=0; //Creating additional thread DiminishPicsThread:=TDiminishPicsThread.Create(true); DiminishPicsThread.FreeOnTerminate:=true; DiminishPicsThread.Priority:=tpHigher; //Transferring params to additional thread DiminishPicsThread.ThreadStarter:=Self; DiminishPicsThread.PicsInDir:=FilesInDirFiltered; DiminishPicsThread.MaxWidth:=50; DiminishPicsThread.MaxHeight:=50; DiminishPicsThread.Start; end; end; |
Метод уменьшения одной картинки при помощи StretchDraw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
//-------------Diminish one pic Stretch--------------- procedure TDiminishPicsThread.DiminishPic(FilePath:string); var thumbnailPicsDir: string; bmp, bmpDiminished: TBItmap; twic:TWICImage; maxWidth:integer; maxHeight:integer; currentWidth:integer; currentHeight:integer; koeff:Extended; NewWidth: integer; NewHeight: integer; CurrentFileName: string; FileExtension: string; NewFileName: string; Picture:TPicture; ms: TMemoryStream; size:int64; begin //Checks... if not TFile.Exists(FilePath) then exit; //Creating dir for Thumbs if not Existed thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\thumbnailPics'; if not TDirectory.Exists( thumbnailPicsDir) then TDirectory.CreateDirectory(thumbnailPicsDir); //---- //Getting FileExtension FileExtension:=ExtractFileExt(FilePath); //Setting MaxSize On the Exit... maxWidth:=FMaxWidth; maxHeight:=FMaxHeight; twic:=TWICImage.Create; bmp:= Tbitmap.Create; //bmp.PixelFormat:=pf32bit; bmp.AlphaFormat:=afDefined; bmpDiminished:= Tbitmap.Create; //bmpDiminished.PixelFormat:=pf32bit; bmpDiminished.AlphaFormat:=afDefined; try twic.LoadFromFile(FilePath); bmp.Assign(twic); currentWidth:=bmp.Width; currentHeight:=bmp.Height; //Calculating koeff if (currentWidth>maxWidth) or (currentHeight>maxHeight) then koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else koeff:=1; // New width and height NewWidth:=Trunc(koeff*currentWidth); NewHeight:=Trunc(koeff*currentHeight); CurrentFileName:=ExtractFileName(FilePath); NewFileName:=ChangeFileext( CurrentFileName, '_thumb'+FileExtension ); // <<NewExtension bmpDiminished.Width:=NewWidth; bmpDiminished.Height:=NewHeight; bmpDiminished.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), bmp); bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName); finally FreeAndNil(twic); FreeAndNil(bmp); FreeAndNil(bmpDiminished); // ShowMessage('success'); end; end; |
Модуль потока – полностью (на всякий случай)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
unit uDiminishPicsThread; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,GDIPOBJ,Math, System.IOUtils,System.Types, Vcl.ComCtrls,uTestDiminishPics; type TDiminishPicsThread = class(TThread) private FThreadStarter:TfTestDiminshPics; fEx : Exception; FPicsArray:TStringDynArray; FMaxWidth:integer; FMaxHeight:integer; procedure QueryError; procedure GDIplusDiminishPic(FilePath: string); procedure LoadAAImageFromFile(FileName: String; W, H: Integer; Pic: TBitmap); procedure LoadFromFile(FilePath: string; var twic:twicimage); procedure DiminishPic(FilePath: string); public property PicsInDir:TStringDynArray read FPicsArray write FPicsArray; property ThreadStarter:TfTestDiminshPics read FThreadStarter write FThreadStarter; property MaxWidth:integer read FMaxWidth write FMaxWidth; property MaxHeight:integer read FMaxHeight write FMaxHeight; protected procedure Execute; override; end; implementation { TDiminishPicsThread } procedure TDiminishPicsThread.Execute; var iTick:integer; i: Integer; begin FThreadStarter.ProgressBar.Max:=High(PicsInDir); iTick:=GetTickCount; for i := 0 to High(PicsInDir) do begin // Anonymously updating progressBar Synchronize( procedure begin FThreadStarter.ProgressBar.Position:= FThreadStarter.ProgressBar.Position+1; end ); DiminishPic(PicsInDir[i]); // ( GDIplusDiminishPic(FilesInDir[i])); // <<Works unstable Randomly // Anonymously updating caption Synchronize( procedure begin ThreadStarter.Caption:= 'Npics / total = '+i.ToString+' / ' +length(FPicsArray).ToString+ ' Time(ms)='+IntToStr(GetTickCount-iTick); end ); end; fEx := ExceptObject as Exception; Synchronize( QueryError ); end; procedure TDiminishPicsThread.LoadFromFile(FilePath:string; var twic:twicimage); begin twic.LoadFromFile(FilePath); end; procedure TDiminishPicsThread.QueryError; begin Application.OnException(Self,fex); end; procedure TDiminishPicsThread.GDIplusDiminishPic(FilePath:string); var thumbnailPicsDir: string; bmp, bmpDiminished: TBItmap; twic:TWICImage; maxWidth:integer; maxHeight:integer; currentWidth:integer; currentHeight:integer; koeff:Extended; NewWidth: integer; NewHeight: integer; CurrentFileName: string; FileExtension: string; NewFileName: string; begin //Checks... if not TFile.Exists(FilePath) then exit; //Creating dir for Thumbs if not Existed thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\thumbnailPics'; if not TDirectory.Exists( thumbnailPicsDir) then TDirectory.CreateDirectory(thumbnailPicsDir); //---- //Getting FileExtension FileExtension:=ExtractFileExt(FilePath); //Setting MaxSize On the Exit... maxWidth:=50; maxHeight:=50; twic:=TWICImage.Create; bmp:= Tbitmap.Create; bmpDiminished:= Tbitmap.Create; try twic.LoadFromFile(FilePath); bmp.Assign(twic); currentWidth:=bmp.Width; currentHeight:=bmp.Height; //Calculating koeff if (currentWidth>maxWidth) or (currentHeight>maxHeight) then koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else koeff:=1; // New width and height NewWidth:=Trunc(koeff*currentWidth); NewHeight:=Trunc(koeff*currentHeight); CurrentFileName:=ExtractFileName(FilePath); NewFileName:=ChangeFileext( CurrentFileName, '_thumb.'+FileExtension ); // <<NewExtension LoadAAImageFromFile(FilePath,NewWidth,NewHeight,bmpDiminished); bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName); finally FreeAndNil(bmp); FreeAndNil(bmpDiminished); FreeAndNil(twic); // ShowMessage('success'); end; end; //-------------Diminish one pic GDI+--------------- procedure TDiminishPicsThread.LoadAAImageFromFile(FileName: String; W,H: Integer; Pic: TBitmap); var ImageTemp: TGPImage; graphicsGDIPlus: TGPGraphics; begin if FileExists(FileName) then begin Pic.Width:=W; Pic.Height:=H; graphicsGDIPlus:=TGPGraphics.Create (Pic.Canvas.Handle); ImageTemp:=TGPImage.Create(FileName ); // Trouble here graphicsGDIPlus.DrawImage(ImageTemp , 0,0,W,H); ImageTemp.Free; graphicsGDIPlus.Free; end; end; //-------------Diminish one pic Stretch--------------- procedure TDiminishPicsThread.DiminishPic(FilePath:string); var thumbnailPicsDir: string; bmp, bmpDiminished: TBItmap; twic:TWICImage; maxWidth:integer; maxHeight:integer; currentWidth:integer; currentHeight:integer; koeff:Extended; NewWidth: integer; NewHeight: integer; CurrentFileName: string; FileExtension: string; NewFileName: string; Picture:TPicture; ms: TMemoryStream; size:int64; begin //Checks... if not TFile.Exists(FilePath) then exit; //Creating dir for Thumbs if not Existed thumbnailPicsDir:=ExtractFileDir(Application.ExeName)+'\thumbnailPics'; if not TDirectory.Exists( thumbnailPicsDir) then TDirectory.CreateDirectory(thumbnailPicsDir); //---- //Getting FileExtension FileExtension:=ExtractFileExt(FilePath); //Setting MaxSize On the Exit... maxWidth:=FMaxWidth; maxHeight:=FMaxHeight; twic:=TWICImage.Create; bmp:= Tbitmap.Create; //bmp.PixelFormat:=pf32bit; bmp.AlphaFormat:=afDefined; bmpDiminished:= Tbitmap.Create; //bmpDiminished.PixelFormat:=pf32bit; bmpDiminished.AlphaFormat:=afDefined; try twic.LoadFromFile(FilePath); bmp.Assign(twic); currentWidth:=bmp.Width; currentHeight:=bmp.Height; //Calculating koeff if (currentWidth>maxWidth) or (currentHeight>maxHeight) then koeff:=Min(maxWidth/currentWidth,maxHeight/currentHeight) else koeff:=1; // New width and height NewWidth:=Trunc(koeff*currentWidth); NewHeight:=Trunc(koeff*currentHeight); CurrentFileName:=ExtractFileName(FilePath); NewFileName:=ChangeFileext( CurrentFileName, '_thumb'+FileExtension ); // <<NewExtension bmpDiminished.Width:=NewWidth; bmpDiminished.Height:=NewHeight; bmpDiminished.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), bmp); bmpDiminished.SaveToFile(thumbnailPicsDir+'\'+NewFileName); finally FreeAndNil(twic); FreeAndNil(bmp); FreeAndNil(bmpDiminished); // FreeAndNil(twic); // ShowMessage('success'); end; end; end. |
Исходники