I started this all with the tiffmanager on codeproject:
http://www.codeproject.com/csharp/TiffManager.asp I tried to splitt a
multipage tiff that was generated by combining singlepage tiff's to a group 4
encoded multipage tiff. When I try to extract the single page tiff's I get
the error:
Unhandled Exception: System.Runtime. InteropServices .ExternalExcept ion: A
generic error occurred in GDI+.
at System.Drawing. Image.Save(Stri ng filename, ImageCodecInfo encoder,
EncoderParamete rs encoderParams)
at TiffManager.Spl itTiffImage(Str ing outPutDirectory , EncoderValue format)
at ConsoleApplicat ion.Module1.Mai n(String[] CmdArgs)
I then tried the tiffsplitter in
http://www.codeproject.com/vb/net/faxeditor.asp. This even throws an error
when I try to load the multipage tiff that came with the source.
I then tried the multi page tiff example by bob powell:
http://www.bobpowell.net/addframes.htm
This gives me bufferoverflow when I try to load my group 4 encoded tiff.
If needed I can post the multipage tiff in mime format here.
Code from the TIffManager.cs
using System;
using System.IO;
using System.Drawing;
using System.Drawing. Imaging;
using System.Collecti ons;
/// <summary>
/// Summary description for TiffManager.
/// </summary>
public class TiffManager : IDisposable
{
private string _ImageFileName;
private int _PageNumber;
private Image image;
private string _TempWorkingDir ;
public TiffManager(str ing imageFileName)
{
this._ImageFile Name=imageFileN ame;
image=Image.Fro mFile(_ImageFil eName);
GetPageNumber() ;
}
public TiffManager()
{
}
/// <summary>
/// Read the image file for the page number.
/// </summary>
private void GetPageNumber()
{
Guid objGuid=image.F rameDimensionsL ist[0];
FrameDimension objDimension=ne w FrameDimension( objGuid);
//Gets the total number of frames in the .tiff file
_PageNumber=ima ge.GetFrameCoun t(objDimension) ;
return;
}
/// <summary>
/// Read the image base string,
/// Assert(GetFileN ameStartString( @"c:\test\abc.t if"),"abc")
/// </summary>
/// <param name="strFullNa me"></param>
/// <returns></returns>
private string GetFileNameStar tString(string strFullName)
{
int posDot=_ImageFi leName.LastInde xOf(".");
int posSlash=_Image FileName.LastIn dexOf(@"\");
return _ImageFileName. Substring(posSl ash+1,posDot-posSlash-1);
}
/// <summary>
/// This function will output the image to a TIFF file with specific
compression format
/// </summary>
/// <param name="outPutDir ectory">The splited images' directory</param>
/// <param name="format">T he codec for compressing</param>
/// <returns>splite d file name array list</returns>
public ArrayList SplitTiffImage( string outPutDirectory ,EncoderValue format)
{
string
fileStartString =outPutDirector y+"\\"+GetFileN ameStartString( _ImageFileName) ;
ArrayList splitedFileName s=new ArrayList();
try
{
Guid objGuid=image.F rameDimensionsL ist[0];
FrameDimension objDimension=ne w FrameDimension( objGuid);
//Saves every frame as a separate file.
Encoder enc=Encoder.Com pression;
int curFrame=0;
for (int i=0;i<_PageNumb er;i++)
{
image.SelectAct iveFrame(objDim ension,curFrame );
EncoderParamete rs ep=new EncoderParamete rs(1);
ep.Param[0]=new EncoderParamete r(enc,(long)for mat);
ImageCodecInfo info=GetEncoder Info("image/tiff");
//Save the master bitmap
string fileName=string .Format("{0}{1} .TIF",fileStart String,i.ToStri ng());
image.Save(file Name,info,ep);
splitedFileName s.Add(fileName) ;
curFrame++;
}
}
catch (Exception)
{
throw;
}
return splitedFileName s;
}
/// <summary>
/// This function will join the TIFF file with a specific compression format
/// </summary>
/// <param name="imageFile s">string array with source image files</param>
/// <param name="outFile"> target TIFF file to be produced</param>
/// <param name="compressE ncoder">compres sion codec enum</param>
public void JoinTiffImages( string[] imageFiles,stri ng outFile,Encoder Value
compressEncoder )
{
try
{
//If only one page in the collection, copy it directly to the target file.
if (imageFiles.Len gth==1)
{
File.Copy(image Files[0],outFile,true);
return;
}
//use the save encoder
Encoder enc=Encoder.Sav eFlag;
EncoderParamete rs ep=new EncoderParamete rs(2);
ep.Param[0]=new EncoderParamete r(enc,(long)Enc oderValue.Multi Frame);
ep.Param[1] = new
EncoderParamete r(Encoder.Compr ession,(long)co mpressEncoder);
Bitmap pages=null;
int frame=0;
ImageCodecInfo info=GetEncoder Info("image/tiff");
foreach(string strImageFile in imageFiles)
{
if(frame==0)
{
pages=(Bitmap)I mage.FromFile(s trImageFile);
//save the first frame
pages.Save(outF ile,info,ep);
}
else
{
//save the intermediate frames
ep.Param[0]=new
EncoderParamete r(enc,(long)Enc oderValue.Frame DimensionPage);
Bitmap bm=(Bitmap)Imag e.FromFile(strI mageFile);
pages.SaveAdd(b m,ep);
}
if(frame==image Files.Length-1)
{
//flush and close.
ep.Param[0]=new EncoderParamete r(enc,(long)Enc oderValue.Flush );
pages.SaveAdd(e p);
}
frame++;
}
}
catch (Exception)
{
throw;
}
return;
}
/// <summary>
/// This function will join the TIFF file with a specific compression format
/// </summary>
/// <param name="imageFile s">array list with source image files</param>
/// <param name="outFile"> target TIFF file to be produced</param>
/// <param name="compressE ncoder">compres sion codec enum</param>
public void JoinTiffImages( ArrayList imageFiles,stri ng
outFile,Encoder Value compressEncoder )
{
try
{
//If only one page in the collection, copy it directly to the target file.
if (imageFiles.Cou nt==1)
{
File.Copy((stri ng)imageFiles[0],outFile,true);
return;
}
//use the save encoder
Encoder enc=Encoder.Sav eFlag;
EncoderParamete rs ep=new EncoderParamete rs(2);
ep.Param[0]=new EncoderParamete r(enc,(long)Enc oderValue.Multi Frame);
ep.Param[1] = new
EncoderParamete r(Encoder.Compr ession,(long)co mpressEncoder);
Bitmap pages=null;
int frame=0;
ImageCodecInfo info=GetEncoder Info("image/tiff");
foreach(string strImageFile in imageFiles)
{
if(frame==0)
{
pages=(Bitmap)I mage.FromFile(s trImageFile);
//save the first frame
pages.Save(outF ile,info,ep);
}
else
{
//save the intermediate frames
ep.Param[0]=new
EncoderParamete r(enc,(long)Enc oderValue.Frame DimensionPage);
Bitmap bm=(Bitmap)Imag e.FromFile(strI mageFile);
pages.SaveAdd(b m,ep);
bm.Dispose();
}
if(frame==image Files.Count-1)
{
//flush and close.
ep.Param[0]=new EncoderParamete r(enc,(long)Enc oderValue.Flush );
pages.SaveAdd(e p);
}
frame++;
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLi ne(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="pageNumbe r">page number to be removed</param>
/// <param name="compressE ncoder">compres s encoder after operation</param>
/// <param name="strFileNa me">filename to be outputed</param>
/// <returns></</returns>
public void RemoveAPage(int pageNumber,Enco derValue compressEncoder ,string
strFileName)
{
try
{
//Split the image files to single pages.
ArrayList arrSplited=Spli tTiffImage(this ._TempWorkingDi r,compressEncod er);
//Remove the specific page from the collection
string
strPageRemove=s tring.Format("{ 0}\\{1}{2}.TIF" ,_TempWorkingDi r,GetFileNameSt artString(this. _ImageFileName) ,pageNumber);
arrSplited.Remo ve(strPageRemov e);
JoinTiffImages( arrSplited,strF ileName,compres sEncoder);
}
catch(Exception )
{
throw;
}
return;
}
/// <summary>
/// Getting the supported codec info.
/// </summary>
/// <param name="mimeType" >description of mime type</param>
/// <returns>imag e codec info</returns>
private ImageCodecInfo GetEncoderInfo( string mimeType)
{
ImageCodecInfo[] encoders=ImageC odecInfo.GetIma geEncoders();
for (int j=0;j<encoders. Length;j++)
{
if (encoders[j].MimeType==mime Type)
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="pageNumbe r">page number to be extracted</param>
/// <returns>imag e object</returns>
public Image GetSpecificPage (int pageNumber)
{
MemoryStream ms=null;
Image retImage=null;
try
{
ms=new MemoryStream();
Guid objGuid=image.F rameDimensionsL ist[0];
FrameDimension objDimension=ne w FrameDimension( objGuid);
image.SelectAct iveFrame(objDim ension,pageNumb er);
image.Save(ms,I mageFormat.Bmp) ;
retImage=Image. FromStream(ms);
return retImage;
}
catch (Exception)
{
ms.Close();
retImage.Dispos e();
throw;
}
}
/// <summary>
/// Convert the existing TIFF to a different codec format
/// </summary>
/// <param name="compressE ncoder"></param>
/// <returns></returns>
public void ConvertTiffForm at(string strNewImageFile Name,EncoderVal ue
compressEncoder )
{
//Split the image files to single pages.
ArrayList arrSplited=Spli tTiffImage(this ._TempWorkingDi r,compressEncod er);
JoinTiffImages( arrSplited,strN ewImageFileName ,compressEncode r);
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.Suppr essFinalize(thi s);
}
#endregion
}
I use a vb.net console app to run it as commandline app:
Imports System
Imports System.Drawing
Module Module1
Sub Main(ByVal CmdArgs() As String)
If CmdArgs.Length = 1 Then
Dim tiffmngr As New TiffManager(Cmd Args(0))
Dim Filenames As ArrayList
tiffmngr.TempWo rkingDir = "c:\output\temp "
Filenames = tiffmngr.SplitT iffImage("c:\ou tput\temp", _
System.Drawing. Imaging.Encoder Value.Compressi onLZW)
Else
Console.WriteLi ne("TiffSplit <tif filename>")
End If
End Sub
End Module
"Kevin Yu [MSFT]" wrote:
Hi Philip,
Have you tried using another TIF file, because it might be the TIF file
error. If this is not the case, could you please post the whole class
definition here, so that I can try to repro it on my machine?
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."