代码
/// <summary>
/// 将TIF文件转换为GIF文件列表
/// </summary>
/// <param name="fileName">要转换的TIF文件</param>
/// <returns>转换后生成的GIF文件路径</returns>
public static IList<string> ConvertTifToGif(string fileName)
{
IList<string> list = new List<string>();
string dic = AppDomain.CurrentDomain.BaseDirectory + "\\tempgif\\";
if (!Directory.Exists(dic))
{
Directory.CreateDirectory(dic);
}
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentException("转换的TIF文件路径不能为空");
}
FileInfo file = new FileInfo(fileName);
if (!file.Exists)
{
throw new FileNotFoundException("待转换的TIF文件不存在");
}
Image imgObj = Image.FromFile(file.FullName);
//Image imgObj = imgObj2.GetThumbnailImage(imgObj2.Width / 3 * 2, imgObj2.Height / 3 * 2, null, IntPtr.Zero); //Image.FromFile(file.FullName);
Guid objGuid = (Guid)imgObj.FrameDimensionsList.GetValue(0);
FrameDimension objDimension = new FrameDimension(objGuid);
int totalImage = imgObj.GetFrameCount(objDimension);
for (int index = 0; index < totalImage; index++)
{
string gifPath = dic + file.Name.Substring(0, file.Name.LastIndexOf('.')) + "_" + index + ".gif";
if (File.Exists(gifPath))
{
File.Delete(gifPath);
}
//EncoderParameter en = new EncoderParameter();
imgObj.SelectActiveFrame(objDimension, index);
imgObj.Save(gifPath, ImageFormat.Gif);
FileInfo item = new FileInfo(gifPath);
list.Add("tempgif\\" + item.Name);
}
return list;
}
/// 将TIF文件转换为GIF文件列表
/// </summary>
/// <param name="fileName">要转换的TIF文件</param>
/// <returns>转换后生成的GIF文件路径</returns>
public static IList<string> ConvertTifToGif(string fileName)
{
IList<string> list = new List<string>();
string dic = AppDomain.CurrentDomain.BaseDirectory + "\\tempgif\\";
if (!Directory.Exists(dic))
{
Directory.CreateDirectory(dic);
}
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentException("转换的TIF文件路径不能为空");
}
FileInfo file = new FileInfo(fileName);
if (!file.Exists)
{
throw new FileNotFoundException("待转换的TIF文件不存在");
}
Image imgObj = Image.FromFile(file.FullName);
//Image imgObj = imgObj2.GetThumbnailImage(imgObj2.Width / 3 * 2, imgObj2.Height / 3 * 2, null, IntPtr.Zero); //Image.FromFile(file.FullName);
Guid objGuid = (Guid)imgObj.FrameDimensionsList.GetValue(0);
FrameDimension objDimension = new FrameDimension(objGuid);
int totalImage = imgObj.GetFrameCount(objDimension);
for (int index = 0; index < totalImage; index++)
{
string gifPath = dic + file.Name.Substring(0, file.Name.LastIndexOf('.')) + "_" + index + ".gif";
if (File.Exists(gifPath))
{
File.Delete(gifPath);
}
//EncoderParameter en = new EncoderParameter();
imgObj.SelectActiveFrame(objDimension, index);
imgObj.Save(gifPath, ImageFormat.Gif);
FileInfo item = new FileInfo(gifPath);
list.Add("tempgif\\" + item.Name);
}
return list;
}