2

我只在我的 Windows Server Datacenter 2007 SP 2 服务器上收到异常,而不是在我的本地 Windows 7 PC 上。

我已将有问题的调用包装在 a 中try-catch-finally,以便程序的其余部分可以正常执行。

如何解决异常并在服务器上正确生成 TIFF?

例外:

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

抛出异常的代码行:

tiffImage.Save(fileName, info, encoderparams);

相关代码:

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
4

1 回答 1

1

经过一番挣扎,第222行的最后一个参数由更改4L24L,并且成功了!感谢我的PM!

encoderparams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 24L);

似乎旧版本的 Windows 不支持某些 TIFF 编解码器。旧代码也在 Windows Server 2012 上进行了测试,并且可以正常工作。

于 2014-02-13T15:52:44.967 回答