问题描述
你好techie,
我正在开发一个用于管理扫描文档的.NET Windows应用程序。
在我的项目中,我正在转换使用 System.Drawing.Imaging.Save
(或) SaveAdd
方法将扫描的图像转换为tiff文件。
当我将10-15张图像转换为单个tiff文件时,工作正常。如果我将更多图像转换为tiff文件,我的代码会触发这些异常参数无效或内存不足。
我已经完成了最后一周的所有调试,有时我的代码运行完美,但大多数时候它抛出错误。
我已经通过codeproject.com遵循以下参考代码:
[ ]
将图像转换为tiff的代码:
Hi techie,
I am developing a .NET windows application, for managing scanning documents.
In my project I am converting the scanned images into tiff file by using System.Drawing.Imaging.Save
(or) SaveAdd
methods.
It is working fine, when I am converting 10-15 images into single tiff file. If I convert more images into tiff file, my code is triggering these exception "Parameter is not valid" or "Out of memory".
I have totally tried all the debugging last one week, sometimes my code is running perfect, but most of the times its throwing the error.
I have followed the below reference code by codeproject.com:
Save images into a multi-page TIFF file or add images to an existing TIFF file[^]
Below the code for convert images into tiff:
public bool saveMultipage(Image[] bmp, string location, string type)
{
if (bmp != null)
{
try
{
ImageCodecInfo codecInfo = getCodecForstring(type);
for (int i = 0; i < bmp.Length; i++)
{
if (bmp[i] == null)
break;
bmp[i] = (Image)ConvertToBitonal((Bitmap)bmp[i]);
}
if (bmp.Length == 1)
{
EncoderParameters iparams = new EncoderParameters(1);
Encoder iparam = Encoder.Compression;
EncoderParameter iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionCCITT4));
iparams.Param[0] = iparamPara;
bmp[0].Save(location, codecInfo, iparams);
}
else if (bmp.Length > 1)
{
Encoder saveEncoder;
Encoder compressionEncoder;
EncoderParameter SaveEncodeParam;
EncoderParameter CompressionEncodeParam;
EncoderParameters EncoderParams = new EncoderParameters(2);
saveEncoder = Encoder.SaveFlag;
compressionEncoder = Encoder.Compression;
// Save the first page (frame).
SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
EncoderParams.Param[0] = CompressionEncodeParam;
EncoderParams.Param[1] = SaveEncodeParam;
File.Delete(location);
bmp[0].Save(location, codecInfo, EncoderParams);
for (int i = 1; i < bmp.Length; i++)
{
if (bmp[i] == null)
break;
SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
EncoderParams.Param[0] = CompressionEncodeParam;
EncoderParams.Param[1] = SaveEncodeParam;
bmp[0].SaveAdd(bmp[i], EncoderParams);
}
SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush);
EncoderParams.Param[0] = SaveEncodeParam;
bmp[0].SaveAdd(EncoderParams);
}
return true;
}
catch (System.Exception ee)
{
throw new Exception(ee.Message + " Error in saving as multipage ");
}
}
else
return false;
}
请告诉我一些关于这个问题的信息,这对我有帮助。
先谢谢开发人员。
Kindly suggest me something regarding this issue, it will be helpful for me.
Thanks in advance developers.
推荐答案
这篇关于使用System.Drawing.Image.Save将多个图像保存到Tiff文件时,参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!