I only receive an exception on my Windows Server Datacenter 2007 SP 2 server, not on my local Windows 7 PC.
I have wrapped the problematic call in a try-catch-finally
, so that the rest of the program may execute normally.
How do I resolve the exception and generate the TIFF correctly on the server?
Exception:
System.Runtime.InteropServices.ExternalException:
A generic error occurred in GDI+. at System.Drawing.Image.Save(String
filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at
TSEmails.TaskScheduleSlippage.CreateTIFFImageReport(DataTable
dataOfManyProjects, String fileName) in
D:\TSEmail\TSEmails\TaskScheduleSlippage.cs:line 236 at
TSEmails.TaskScheduleSlippage.GenerateOrganizationalEmail(DataTable
dataOfManyProjects, DataTable emailSettings) in
D:\TSEmail\TSEmails\TaskScheduleSlippage.cs:line 92
Line of code throwing the exception:
tiffImage.Save(fileName, info, encoderparams);
Relevant code:
private static void CreateTIFFImageReport(DataTable dataOfManyProjects, string fileName)
{//Line:210
///The following code was originally taken from http://www.yoursearchbuddy.com/create-tiff-image-multiple-images
///on Thursday May 9, 2013
//Selecting the image encoder
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
ImageCodecInfo info = null;
info = (from ie in ImageCodecInfo.GetImageEncoders()
where ie.MimeType == "image/tiff"
select ie).FirstOrDefault();
EncoderParameters encoderparams = new EncoderParameters(2);
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
encoderparams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 4L);
//Adding graphs of each project to TIFF image
Bitmap tiffImage = null;
Image img = null;
foreach (DataRow row in dataOfManyProjects.DefaultView.ToTable(true, "Project Code").Rows)
{
string projectCode = row["Project Code"].ToString();
img = Image.FromFile("C:\\LMS\\Logs\\" + masterTitleWS + "ReportOf" + projectCode.Replace(" ", string.Empty) + ".jpg", false);
if (row.Table.Rows.IndexOf(row) == 0)
{
//Saving the bitmap
tiffImage = new Bitmap(250, 250, PixelFormat.Format8bppIndexed);//This line was put which SEEMED to have solved the problem, according to a developer,but there is no prove that it ran correctly, and is still giving an exception
tiffImage = (Bitmap)img;
tiffImage.Save(fileName, info, encoderparams);
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
}
else
{
//Adding another image
tiffImage.SaveAdd(img, encoderparams);
}
//img.Dispose();
}
//close file
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
tiffImage.SaveAdd(encoderparams);
tiffImage.Dispose();
}//Line:250
After a lot of struggle, the last parameter of line 222 was changed from 4L
to 24L
, and it worked! Thanks to my PM!
encoderparams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 24L);
It seems that older versions of windows do not support some TIFF codecs. The older code was tested on Windows Server 2012 too, and it works.