我有一个关于将不同的fie /图像类型转换为TIFF格式的小问题。我们使用Atalasoft,一个第三方软件,用于处理我们的扫描和查看文档和图像。使用Atalasoft&C将PDF/JPEG转换为TIFF#
我面临的问题是我在TiffDocument()方法中得到一个参数异常。它将文件流传入该方法。参数异常声明文件不是TIFF格式。当我传递PDF或JPEG时,这是可以理解的。
我尝试过试图转换这些,但无济于事无数次尝试。任何时候我尝试将jpeg转换为tiff,都会出现问题,因为图像是AtalaImage而不是System.Drawing.Image。
对于JPEG转换,我从注释部分here劫持了这段代码。
public static Image ConvertToJpeg(string fileName)
{
Image retVal = null;
using (FileStream fs = File.OpenRead(fileName))
{
retVal = ConvertToJpeg(fs);
fs.Close();
}
return retVal;
}
/// <summary>
/// Converts the specified image into a JPEG format
/// </summary>
/// <param name="imgStream">The stream of the image to convert</param>
/// <returns>An Image with JPEG data if successful; otherwise null</returns>
public static Image ConvertToJpeg(Stream imgStream)
{
Image retVal = null;
Stream retStream = new MemoryStream();
using (Image img = Image.FromStream(imgStream, true, true))
{
img.Save(retStream, ImageFormat.Jpeg);
retStream.Flush();
}
retVal = Image.FromStream(retStream, true, true);
return retVal;
}
}
}
此外,Atalasoft确实有关于小的指令,PDF转换为TIFF,而是一个ArgumentException在保存方法抛出(错误消息:TIFF编解码器错误错误写入TIFF流)。下面的代码是相同的链接代码:
TiffEncoder noAppend = new TiffEncoder(TiffCompression.Default, true);
PdfDecoder pdf = new PdfDecoder();
for(int i=0; i< numPages; i++)
{
AtalaImage img = pdf.Read(inStream, i, null);
noAppend.Save(outStream, img, null);
img.Dispose();
outStream.Seek(0, SeekOrigin.Begin);
}
编辑:在上面的代码工作,它的PDF转换成功为TIFF。
此外,我需要知道这个文件是什么格式,所以它可以发送到适当的方法进行转换。我曾尝试使用this question的代码,但无济于事。
下面是魔术发生的一个片段。有调用此功能,但该功能的图像设置为给WebImageViewer.Image到可变并调用以下方法:
private void Foo(AtalaImage image)
{
/*I have tried converting here, before the file stream, and after the
filestream. */
var url = wiv.ImageUrl;
var path = Page.MapPath(url);
var frame = wiv.CurrentPage - 1;
Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
var doc = new TiffDocument(fs); //This is where the error occurs.
var page = new TiffPage(image);
doc.Pages[frame] = page;
doc.Save(path + "_tmp");
fs.Close();
File.Delete(path);
File.Move(path + "_tmp", path);
wtv.OpenUrl(url);
wtv.SelectedIndex = frame;
wiv.OpenUrl(url, frame);
}
任何帮助或思维过程,将不胜感激。