Hi team,
I am getting below error while merging Tiff Image files.
A generic error occurred in GDI+.
Below is my code.
Class file code
public string MultiImageName { get; set; }
public string[] inputTIFFs { get; set; }
public void Create()
{
CreateMTIFF(inputTIFFs, MultiImageName);
}
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
private void CreateMTIFF(string[] stiffs, string mimgname)
{
try
{
ImageCodecInfo codec = GetEncoderInfo("image/tiff");
EncoderParameters imgPrms = new EncoderParameters(3);
Image img = null;
imgPrms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
imgPrms.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
bool firstpage = true;
foreach (var s in stiffs)
{
Image imgAdd;
if (firstpage)
{
img = Image.FromFile(s);
imgPrms.Param[2] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
img.Save(mimgname, codec, imgPrms);
firstpage = false;
}
else
{
img = Image.FromFile(s);
imgPrms.Param[2] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
imgAdd = Image.FromFile(s);
img.SaveAdd(imgAdd, imgPrms); // Getting error at this line for second image
imgAdd.Dispose();
}
}
imgPrms.Param[2] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
img.SaveAdd(imgPrms);
img.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public ImageCodecInfo GetEncoderInfo(string mimeType)
{
foreach (var ice in ImageCodecInfo.GetImageEncoders())
{
if (ice.MimeType.Equals(mimeType))
return ice;
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
Aspx codebhind code
string[] arrimgn;
clsMTiff myc = new clsMTiff();
myc = new clsMTiff();
Random r = new Random();
int genRand = r.Next(10, 50);
myc.MultiImageName = Server.MapPath("~/Image/TestImage_Converted_" + genRand.ToString() + ".tif");
arrimgn = new string[3];
arrimgn[0] = Server.MapPath("~/Image/0001.tif");
arrimgn[1] = Server.MapPath("~/Image/0002.tif");
arrimgn[2] = Server.MapPath("~/Image/0003.tif");
myc.inputTIFFs = arrimgn;
myc.Create();