读书人

C# 分割tif文件有关问题

发布时间: 2012-12-19 14:13:14 作者: rapoo

C# 分割tif文件问题
Image img = Image.FromFile(fileName);
Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
FrameDimension dimension = new FrameDimension(guid);
int totalPage = img.GetFrameCount(dimension);

MessageBox.Show("共" + totalPage + "页");
for (int i = 0; i < totalPage; i++)
{

img.SelectActiveFrame(dimension, i);
int num = i + 1;
img.Save(@"d:\test2\jpeg" + num + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

} 拆分tif文件时,如果tif文件里有压缩jpg的图片,就导致拆分出异常,这个应该怎么解决啊?
[最优解释]


try
{
Bitmap img = new BitMap("c:/1.TIF")//写上你的地址,因为我担心Image会报错
Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
FrameDimension dimension = new FrameDimension(guid);
int totalPage = img.GetFrameCount(dimension);

MessageBox.Show("共" + totalPage + "页");
for (int i = 0; i < totalPage; i++)
{

img.SelectActiveFrame(dimension, i);
int num = i + 1;
img.Save(@"d:\test2\jpeg" + num + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);

}
}
catch
{
}

}

[其他解释]
引用:
如果tif文件里有jpeg文件,程序走到
img.SelectActiveFrame(dimension, i);
的时候就会报错。提示说有未处理的ExternaException GDI+ 中发生一般性错误

不知道你是不是用的win7操作系统,可能没有取得管理员权限所致。XP情况下是可以的
[其他解释]
记得结贴额 。。。。。。。。。。。。。。。。。。。。。。。。。。。。


[其他解释]
img.Save(@"d:\test2\jpeg" + num + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
这一句改成img.Save(@"d:\test2\jpeg" + num + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
还有就是加个try catch


try
{
Image img = Image.FromFile(fileName);
Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
FrameDimension dimension = new FrameDimension(guid);
int totalPage = img.GetFrameCount(dimension);

MessageBox.Show("共" + totalPage + "页");
for (int i = 0; i < totalPage; i++)
{

img.SelectActiveFrame(dimension, i);
int num = i + 1;
img.Save(@"d:\test2\jpeg" + num + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);

}
}
catch
{
}

}

[其他解释]
加了try catch 在分割的时候就会把tif文件中压缩的jpg文件跳过去不分割。比如tif文件有100页,10页是jpeg文件,加了try catch 就会把tif文件分割成90份,jpeg类型的被直接跳过了。
[其他解释]
如果tif文件里有jpeg文件,程序走到
img.SelectActiveFrame(dimension, i);
的时候就会报错。提示说有未处理的ExternaException GDI+ 中发生一般性错误
[其他解释]
该回复于2012-11-27 16:20:37被管理员删除
[其他解释]
路径是没问题的。我用的也是XP系统。
我觉得可能是因为这个tif文件合并的jpg应该有什么特殊的地方。因为我自己合并的tif文件(含jpg格式图像)用上面的拆分方法拆分一点问题没有。只有拆分这个tif文件一直通过不了。
我用TiffToy.exe可以拆分它。拆分时如果把jpg格式文件拆成tif格式,C#程序识别不了,如果拆成jpg格式,C#程序可以处理。
[其他解释]
有人知道TiffToy.exe的实现原理么?
[其他解释]
想要实现:TIFF文件拆分:将多页TIFF拆分成每页一个文件。JPEG或OJPEG压缩的TIFF可无损转换成JPG。 等大师~~~~~~~~~~~~~~~~~
[其他解释]

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

namespace RSExplorer
{
/// <summary>
/// Summary description for TiffManager.
/// </summary>


public class TiffManager : IDisposable
{
private string _ImageFileName;
private int _PageNumber;
private Image image;
private string _TempWorkingDir;

public TiffManager(string imageFileName)
{
this._ImageFileName=imageFileName;
image=Image.FromFile(_ImageFileName);
GetPageNumber();
}

public TiffManager(){
}

/// <summary>
/// Read the image file for the page number.
/// </summary>
private void GetPageNumber(){
Guid objGuid=image.FrameDimensionsList[0];
FrameDimension objDimension=new FrameDimension(objGuid);

//Gets the total number of frames in the .tiff file
_PageNumber=image.GetFrameCount(objDimension);

return;
}

/// <summary>
/// Read the image base string,
/// Assert(GetFileNameStartString(@ "c:/test/abc.tif "), "abc ")
/// </summary>
/// <param name= "strFullName "> </param>
/// <returns> </returns>
private string GetFileNameStartString(string strFullName){
int posDot=_ImageFileName.LastIndexOf( ". ");
int posSlash=_ImageFileName.LastIndexOf(@ "/ ");
return _ImageFileName.Substring(posSlash+1,posDot-posSlash-1);
}


/// <summary>
/// This function will join the TIFF file with a specific compression format
/// </summary>
/// <param name= "imageFiles "> array list with source image files </param>
/// <param name= "outFile "> target TIFF file to be produced </param>
/// <param name= "compressEncoder "> compression codec enum </param>
public void JoinTiffImages(ArrayList imageFiles,string outFile,EncoderValue compressEncoder)
{
try
{
//If only one page in the collection, copy it directly to the target file.


if (imageFiles.Count==1){
File.Copy((string)imageFiles[0],outFile,true);
return;
}

//use the save encoder
Encoder enc=Encoder.SaveFlag;

EncoderParameters ep=new EncoderParameters(2);
ep.Param[0]=new EncoderParameter(enc,(long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression,(long)compressEncoder);

Bitmap pages=null;
int frame=0;
ImageCodecInfo info=GetEncoderInfo( "image/tiff ");


foreach(string strImageFile in imageFiles)
{
if(frame==0)
{
pages=(Bitmap)Image.FromFile(strImageFile);

//save the first frame
pages.Save(outFile,info,ep);
}
else
{
//save the intermediate frames
ep.Param[0]=new EncoderParameter(enc,(long)EncoderValue.FrameDimensionPage);

Bitmap bm=(Bitmap)Image.FromFile(strImageFile);
pages.SaveAdd(bm,ep);
bm.Dispose();
}

if(frame==imageFiles.Count-1)
{
//flush and close.
ep.Param[0]=new EncoderParameter(enc,(long)EncoderValue.Flush);
pages.SaveAdd(ep);
}

frame++;
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex.Message);
#endif
throw;
}

return;
}

/// <summary>
/// Remove a specific page within the image object and save the result to an output image file.
/// </summary>
/// <param name= "pageNumber "> page number to be removed </param>
/// <param name= "compressEncoder "> compress encoder after operation </param>
/// <param name= "strFileName "> filename to be outputed </param>
/// <returns> </ </returns>
public void RemoveAPage(int pageNumber,EncoderValue compressEncoder,string strFileName){
try
{
//Split the image files to single pages.
ArrayList arrSplited=SplitTiffImage(this._TempWorkingDir,compressEncoder);

//Remove the specific page from the collection


string strPageRemove=string.Format( "{0}//{1}{2}.TIF ",_TempWorkingDir,GetFileNameStartString(this._ImageFileName),pageNumber);
arrSplited.Remove(strPageRemove);

JoinTiffImages(arrSplited,strFileName,compressEncoder);
}
catch(Exception)
{
throw;
}

return;
}

/// <summary>
/// Getting the supported codec info.
/// </summary>
/// <param name= "mimeType "> description of mime type </param>
/// <returns> image codec info </returns>
private ImageCodecInfo GetEncoderInfo(string mimeType){
ImageCodecInfo[] encoders=ImageCodecInfo.GetImageEncoders();
for (int j=0;j <encoders.Length;j++){
if (encoders[j].MimeType==mimeType)
return encoders[j];
}

throw new Exception( mimeType + " mime type not found in ImageCodecInfo " );
}

/// <summary>
/// Return the memory steam of a specific page
/// </summary>
/// <param name= "pageNumber "> page number to be extracted </param>
/// <returns> image object </returns>
public Image GetSpecificPage(int pageNumber)
{
MemoryStream ms=null;
Image retImage=null;
try
{
ms=new MemoryStream();
Guid objGuid=image.FrameDimensionsList[0];
FrameDimension objDimension=new FrameDimension(objGuid);

image.SelectActiveFrame(objDimension,pageNumber);
image.Save(ms,ImageFormat.Bmp);

retImage=Image.FromStream(ms);

return retImage;
}
catch (Exception)
{
ms.Close();
retImage.Dispose();
throw;
}
}

/// <summary>
/// Convert the existing TIFF to a different codec format
/// </summary>
/// <param name= "compressEncoder "> </param>
/// <returns> </returns>
public void ConvertTiffFormat(string strNewImageFileName,EncoderValue compressEncoder)


{
//Split the image files to single pages.
ArrayList arrSplited=SplitTiffImage(this._TempWorkingDir,compressEncoder);
JoinTiffImages(arrSplited,strNewImageFileName,compressEncoder);

return;
}

/// <summary>
/// Image file to operate
/// </summary>
public string ImageFileName
{
get
{
return _ImageFileName;
}
set{
_ImageFileName=value;
}
}

/// <summary>
/// Buffering directory
/// </summary>
public string TempWorkingDir
{
get
{
return _TempWorkingDir;
}
set{
_TempWorkingDir=value;
}
}

/// <summary>
/// Image page number
/// </summary>
public int PageNumber
{
get
{
return _PageNumber;
}
}


#region IDisposable Members

public void Dispose()
{
image.Dispose();
System.GC.SuppressFinalize(this);
}

#endregion
}
}


[其他解释]
程序分离tif文件时,如果tif文件里有jpg格式,当走到image.SelectActiveFrame时就出错误。
[其他解释]
VB 和 VC 都不识别jpeg压缩的tif文件时怎么回事?
[其他解释]
围观一下专家!!
[其他解释]
引用:
围观一下专家!!

C#和JAVA 对jpeg压缩的tif文件支持不是很好,这个问题用VC做了个dll解决了

读书人网 >C#

热点推荐