ERROR COMES IN AUTOFITCOLUMNS METHOD

Nov 23, 2016 at 10:06 AM
I am generating data from memory stream to excel files. But suddenly error comes in autofitcolumns method. with " A GENERIC ERROR OCCURRED IN GDI+ ".

using (var excelpackage = new ExcelPackage(stream ?? new MemoryStream()))
        {
            tracer.Trace("entered Helper Class..");
            excelpackage.Workbook.Properties.Author = "xxxx";
            excelpackage.Workbook.Properties.Title = "xxxx";
            excelpackage.Workbook.Properties.Comments = "This is an Exported Excel with CRM Data";
            // set some extended property values
            excelpackage.Workbook.Properties.Company = "xxx";

            excelpackage.Workbook.Worksheets.Add("Report");
            var worksheet = excelpackage.Workbook.Worksheets[1];
            tracer.Trace("Loading the data from Data Table");
            worksheet.Cells[1, 1].LoadFromDataTable(dt, true);
            tracer.Trace("Data loaded");

           worksheet.Cells.AutoFitColumns(tracer);  //IN THIS METHOD ERROR COMES AT         
                                                                                      PARTICULAR LINE.

*************** AutoFitColumns() METHOD **************


public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
{




var nf = styles.Fonts[styles.CellXfs[0].FontId];
        var fs = FontStyle.Regular;
        if (nf.Bold) fs |= FontStyle.Bold;
        if (nf.UnderLine) fs |= FontStyle.Underline;
        if (nf.Italic) fs |= FontStyle.Italic;
        if (nf.Strike) fs |= FontStyle.Strikeout;

          __var nfont = new Font(nf.Name, nf.Size, fs);__ // HERE COMES ERROR

        using (var b = new Bitmap(1, 1)
cAN YOU PLEASE SUGGEST WAY HOW TO SOLVE THIS PROBLEM.