This is a question I posted on stack exchange, but have not gotten any answers. I posted it on a Windows Server forum and was referred here. This is occurring on Windows Server 2008 R2 systems.
I have seen many issues involving the GDI+ Generic Error, but I have not seen this particular matter raised before. We are consistently getting the error, on Windows systems other than Windows 8, when attempting to read (System.Drawing.Image.SelectActiveFrame)
a multiple-frame tiff that includes frames in mixed Photometric Interpretation formats. That is to say, the file includes both RGB color and min-is-white formats, with corresponding differences in the Bits/Sample and Samples/Pixel frame parameters. The error
is consistently raising as soon as a frame is encountered with a format that is different from that of the first frame.
// Convert File from .tiff to .PDF
static void ConvertFile(string file, string pdffilename)
{
string localMessage = string.Empty;
try
{
//if it's a PDF Just renamed it and continue
if (file.ToLower().Contains(".pdf"))
{
File.Copy(file, pdffilename);
return;
}
// If file exists return
if (File.Exists(pdffilename)) { return; }
using (var stream = new FileStream(pdffilename, FileMode.Create))
{
localMessage = "01";
var document = new iTextSharp.text.Document();
localMessage = "01";
var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream);
localMessage = "02";
var bm = Bitmap.FromFile(file);
localMessage = "03";
var total = bm.GetFrameCount(FrameDimension.Page);
localMessage = "04";
document.Open();
//iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
localMessage = "05";
iTextSharp.text.Image img = null;
for (var currentframe = 0; currentframe < total; ++currentframe)
{
localMessage = "06=>" + currentframe.ToString();
bm.SelectActiveFrame(FrameDimension.Page, currentframe);
localMessage = "07=>" + currentframe.ToString();
img = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Bmp);
localMessage = "08=>" + currentframe.ToString();
img.ScalePercent(72f / img.DpiX * 100);
localMessage = "09=>" + currentframe.ToString();
img.SetAbsolutePosition(0, 0);
localMessage = "10=>" + currentframe.ToString();
iTextSharp.text.Rectangle pageRect = new iTextSharp.text.Rectangle(0, 0, img.ScaledWidth, img.ScaledHeight);
localMessage = "11=>" + currentframe.ToString();
document.SetPageSize(pageRect);
localMessage = "12=>" + currentframe.ToString();
document.NewPage();
localMessage = "13=>" + currentframe.ToString();
document.Add(img);
}
localMessage = "14";
bm.Dispose();
localMessage = "15";
document.Close();
localMessage = "16";
stream.Close();
}
}
catch (Exception exception)
{
string msg = exception.Message + "\r\n" +"Coversion Error--\r\n" +"\tinput file name: " + file + "\r\n" +"\toutput file name: " + pdffilename + "\r\n" +"\tlocal message" + localMessage + "\r\n";
Console.WriteLine(msg);
SaveError(msg);
throw;
}
}
The "local message" is always "06=>n", where n is the 0-based index of the frame that transitions to a new format.
The error occurs whether the transition is from black and white to color, or color to black and white. The issue does not seem to occur on Windows 8 boxes, but we only have W8 developer PCs, not servers. It is a production process and needs to be deployed
on a server.
Does anyone know why this is happening or how to fix it?