crop - JPG cropping in batch to generate square thumbnails -


i looking light weight batch tool cropping image files. cropping done around center since aspect ration can 3:4 or 4:3 means taller images, crop happen @ top , @ bottom generate square image. wider images, crop happen @ left , right generate square image.

anyone has used such tool? using .net 4.0 , c#

i not looking imagemagick or nconvert.

this fist creates in-memory bitmap square sized square fits in original. scales down thumbsize.

string imagefolder = @"c:\users\russ\originals"; string thumbfolder = @"c:\users\russ\squares"; int thumbsize = 100;  foreach (string file in system.io.directory.getfiles(imagefolder, "*.jpg")) {     using (image original  = bitmap.fromfile(file))     {         size size = new size(             math.min(original.width, original.height),             math.min(original.width, original.height)         );         int translatex = (size.width - original.width) / 2;         int translatey = (size.height - original.height) / 2;          using (bitmap square = new bitmap(size.width, size.height))         {             using (graphics g = graphics.fromimage(square))             {                  g.drawimage(original, translatex, translatey, original.width, original.height);             }              using (bitmap thumb = new bitmap(thumbsize, thumbsize))             {                 using (graphics g2 = graphics.fromimage(thumb))                 {                     g2.drawimage(square, 0, 0, thumbsize, thumbsize);                 }                 string thumbfile = path.combine(thumbfolder, path.getfilename(file));                 thumb.save(thumbfile, imageformat.jpeg);             }          }     } } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -