Compressing existing TIFF file in c# -
i have thousands of tiff files 32mb because no compression added them. need run through them , compress each 1 using lzw. make quick compressing single page tiff files, of them.
i generic gdi+ error when running this. however, if use different file name when saving works fine.
directoryinfo d = new directoryinfo(@"d:\data\lzwtest"); fileinfo[] files = d.getfiles("*.tif"); foreach (fileinfo file in files) { bitmap image1 = (bitmap)image.fromfile(file.fullname); int tiffpages = image1.getframecount(system.drawing.imaging.framedimension.page); if (tiffpages == 1) { image1.save(@"d:\data\lzwtest\" + file.name, myimagecodecinfo, myencoderparameters); } }
if change image1.save line following works (just adding "test-" front of file name.
image1.save(@"d:\data\lzwtest\test-" + file.name, myimagecodecinfo, myencoderparameters);
any appreciated!
you need use intermediate store bitmap.
directoryinfo d = new directoryinfo(@"d:\data\lzwtest"); fileinfo[] files = d.getfiles("*.tif"); foreach (fileinfo file in files) { using(memorystream ms = new memorystream(file.readallbytes(file))) { bitmap image1 = (bitmap)image.fromstream(ms); int tiffpages = image1.getframecount(system.drawing.imaging.framedimension.page); if (tiffpages == 1) { image1.save(@"d:\data\lzwtest\" + file.name, myimagecodecinfo, myencoderparameters); } } }
the problem gdi+ locks source of bitmap, whether it's file or stream really. in case, don't care if memorystream locked.
and wish devs of gdi+ hours of sitting on uncomfortable chair "generic gdi+ error". no inner exception , no other explanation.
Comments
Post a Comment