我面临一个挑战,我需要在 tiff 文件的最后一页上做一些工作。所以我写了下面的示例代码,它确实有效,只是运行缓慢。代码最终会执行很多,我需要加快速度。
我玩过多线程的想法,但我认为结果并不好,因为最终所有数据都必须写入同一个文件,这似乎是耗时的部分。
我希望有一个答案可以告诉我如何仅在最后一页上工作,或者我还没有考虑过的事情。
private void rotateLastPage() {
string inputfile = "C:\\input.tif";
string tmpfile = inputfile + ".tmp";
Bitmap bmap = (Bitmap)Image.FromFile(inputfile);
int max = bmap.GetFrameCount(FrameDimension.Page);
Image[] images = new Image[max];
int i;
for (i = 0; i < max; i++)
{
bmap.SelectActiveFrame(FrameDimension.Page, i);
images[i] = (Image)bmap.Clone();
}
bmap.Dispose();
Bitmap pages = null;
i = 0;
foreach(Image image in images)
{
try
{
if (i == max - 1)
{
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
EncoderParameters encoderParameters = new EncoderParameters(2);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
Int16 c = BitConverter.ToInt16(image.PropertyItems[Array.IndexOf(image.PropertyIdList, 0x103)].Value, 0);
if (c != 4 && c != 5)
{
throw new ArgumentException("Only CCIT4 and LZW compressed images are allowed.");
}
else if (c == 4)
{
encoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
}
else if (c == 5)
{
encoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
}
if (i == 0)
{
pages = (Bitmap)image;
ImageCodecInfo encoder = getTiffEncoder();
pages.Save(tmpfile, encoder, encoderParameters);
}
else
{
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(image, encoderParameters);
}
if (i == max - 1)
{
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
pages.SaveAdd(encoderParameters);
}
i++;
}
catch (Exception e)
{
Console.WriteLine(String.Format("Unable to rotate page {0} in file {1} due to {2}", (i + 1).ToString(), inputfile, e.Message));
break;
}
}
foreach (Image image in images) {
image.Dispose();
}
pages.Dispose();
// eventually overwrite input file with tmp file
}
private static ImageCodecInfo getTiffEncoder()
{
ImageCodecInfo encoder = null;
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < encoders.Length; i++)
{
if (encoders[i].MimeType == "image/tiff")
{
encoder = encoders[i];
}
}
if (encoder == null)
{
throw new NotSupportedException("Unable to find a tiff encoder.");
}
return encoder;
}