分割tiff图像时,在image.save上的GDI +中的Asp.net泛型错误 [英] Asp.net generic error in GDI+ on image.save when splitting tiff image
问题描述
亲爱的,
i在Web应用程序(ASP.NET / C#)中使用以下代码来分割tiff图像:
Dear all,
i use the following code in a web application (ASP.NET/C#) to split a tiff image:
public void SplitTiffImage(string inputFilePath, EncoderValue format)
{
try{
using (FileStream fs = new FileStream(inputFilePath, FileMode.Open,
FileAccess.Read) )
{
using (Image temp_image = Image.FromStream(fs))
{
Guid objGuid = temp_image.FrameDimensionsList[0];
FrameDimension objDimension = new FrameDimension(objGuid);
//Saves every frame as a separate file.
Encoder enc = Encoder.Compression;
int curFrame = 0;
for (int i = 0; i < page_number; i++)
{
temp_image.SelectActiveFrame(objDimension, curFrame);
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)format);
ImageCodecInfo info = GetEncoderInfo("image/tiff");
string fileName = string.Format("{0}_{1}.tif", "some_name",
i.ToString());
temp_image.Save(fileName, info, ep);
curFrame++;
}// for
}// using imgage
}// using filestream
File.Delete(inputFilePath);
}catch(Exception x){
/*do something*/}
}
i这样称呼:
i call it like this:
SplitTiffImage(ImageFullPath , EncoderValue.CompressionNone);
i多次测试代码。有时候它会成功运行,有时会失败:它会保存很多帧然后就会失败
i tested the code several times. sometimes it works successfully, and sometimes it fails : it saves many frames and then it fails at
temp_image.Save(fileName, info, ep)
,GDI +发生一般性错误。它发生在随机帧中,而不是特定的帧。
我尝试过:
i我确定用户有权限..
i测试了很多tiff文件的代码。它成功获取了一个文件,并且下次它失败了同一个文件!
i注释掉了File.Delete语句,以确保没有触及原始文件,但结果没有改变
with a general error occured at GDI+. it happens at random frames, not a particular one.
What I have tried:
i am sure the user has permissions..
i tested the code on many tiff files. it succeeds for a file, and the next time it fails for the same file !
i commented out the File.Delete statement to make sure the original file is not touched, but results didn't change
推荐答案
GDI +发生一般性错误,此错误导致图像文件引用未正确关闭,您正在尝试创建或打开相同的图像。
用于在函数结束时的近距离图像引用。通过调用垃圾收集器。处理图像对象。
general error occured at GDI+ , this error raise due image file references not closed properly and you are trying create or open same image.
there for close image references at end of function. by calling Garbage collector. disposing image object .
以避免GDI +错误,我改为使用Microsoft Windows Imaging Components(WIC)。这是一个很好的页面,包含拆分和合并的代码tiff
使用Microsoft Windows Imaging Components(WIC)加速Tiff文件处理
,这是拆分方法:
to avoid GDI+ error, i used Microsoft Windows Imaging Components (WIC) instead. here is a good page with code to split and merge tiff
Speed up Tiff file processing with Microsoft Windows Imaging Components (WIC)
and here is the splitting method :
public static void SPlitTiffWIC(string fileName)
{
TiffBitmapEncoder newFileEncoder = null;
FileInfo fi = new FileInfo(fileName);
using (Stream documentStream = fi.OpenRead())
{
TiffBitmapDecoder originalFileDecoder =
new TiffBitmapDecoder(documentStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None);
foreach (BitmapFrame frame in originalFileDecoder.Frames)
{
newFileEncoder = new TiffBitmapEncoder();
newFileEncoder.Frames.Add(frame);
using (FileStream stream = File.Create("c:\\tiffs\\"
+ Guid.NewGuid().ToString() + ".tiff"))
{
newFileEncoder.Save(stream);
}
}
}
}
这篇关于分割tiff图像时,在image.save上的GDI +中的Asp.net泛型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!