Convert chart to image in ASP.net 2.0

The convert chart to image sample code:

Learn how to use Aspose.Cells for .NET to convert a chart to an image format, such as JPEG or PNG. Our guide will demonstrate how to export a chart from Microsoft Excel and save it as a standalone image for further use and manipulation.

doesn’t work for ASP.net 2.0 due to

bitmap.Save("d:\\Chart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

Any suggestions?

Thanks

Charlie

I just found that this line returns null.

Bitmap bitmap = chart.ToImage();

Hi,

Which version do you use? Please try this fix.

If you still get null ,please post your template file or codes. We will check it soon.

Warren,

It is still not working by using the new dll. All the code are from the sample code.

protected void btnChart2Image_Click(object sender, EventArgs e)

{

try

{

Aspose.Cells.License license = new Aspose.Cells.License();

license.SetLicense("Aspose.Cells.lic");

//Instantiating an Workbook object

Workbook workbook = new Workbook();

//Adding a new worksheet to the Excel object

int sheetIndex = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = workbook.Worksheets[sheetIndex];

//Adding a sample value to "A1" cell

worksheet.Cells["A1"].PutValue(50);

//Adding a sample value to "A2" cell

worksheet.Cells["A2"].PutValue(100);

//Adding a sample value to "A3" cell

worksheet.Cells["A3"].PutValue(150);

//Adding a sample value to "B1" cell

worksheet.Cells["B1"].PutValue(4);

//Adding a sample value to "B2" cell

worksheet.Cells["B2"].PutValue(20);

//Adding a sample value to "B3" cell

worksheet.Cells["B3"].PutValue(50);

//Adding a chart to the worksheet

int chartIndex = worksheet.Charts.Add(ChartType.Pyramid, 5, 0, 15, 5);

//Accessing the instance of the newly added chart

Chart chart = worksheet.Charts[chartIndex];

//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"

chart.NSeries.Add("A1:B3", true);

//Converting chart to image.

Bitmap bitmap = chart.ToImage(); // return null

//Saving the chart image.

//bitmap.Save("c:\\Chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

//Saving the Excel file

workbook.Save("C:\\SampleChart.xls");

}

catch (Exception err)

{

lblError.Text = "btnBasicPivotTable_Click -- " + err.Message;

lblError.Visible = true;

btnFind.Visible = false;

}

}

Warren,


the following code works for version 4.4.05 but doesn't work for 4.4.0.26 now. I got the following message.


The process cannot access the file 'c:\inetpub\wwwroot\SPG\eng\Template\Evaluation.xls' because it is being used by another process.


for
templateWorkbook.Save(tempFile, FileFormatType.Default);

private void CourseEvalReport_Template()
{
DataSet dsEvaluation;

try
{
dsEvaluation = new DataSet();

dsEvaluation = spgCon.GetCourseEvalDataSet("TRAINING_PKG", "GetCourseEvaluation", "courseevaluation", "cur_courseevaluation",
"p_course", strCourseno);

int iRow = dsEvaluation.Tables[0].Rows.Count; // # of data rows
int iCol = dsEvaluation.Tables[0].Columns.Count; // # of data columns

if (iRow <= 0)
{
lblError.Text = "No feedback for this course.";
lblError.Visible = true;
}
else
{
//Creating a Workbook object
Workbook templateWorkbook = new Workbook();
Workbook workbook = new Workbook();

string path = MapPath("..");
string tempFile = path + @"\Template\Evaluation.xls";

templateWorkbook.Open(tempFile);

// copy it first
workbook.Copy(templateWorkbook);

// close the template
templateWorkbook.Save(tempFile, FileFormatType.Default);

// start working on the new workwook
// create Title for summary
Cells cells;
Cell cell;

Worksheet summarySheet = workbook.Worksheets[0]; // overall rating sheet
cells = summarySheet.Cells;
cell = cells["A2"];
cell.PutValue(strCourseTitle);
cell.Style.Font.Size = 14;
cell.Style.Font.Color = Color.Blue;
cell.Style.Font.IsItalic = true;
summarySheet.AutoFitRow(1);

Worksheet commentSheet = workbook.Worksheets[1]; // comments sheet
cells = commentSheet.Cells;
cell = cells["A2"];
cell.PutValue(strCourseTitle);
cell.Style.Font.Size = 14;
cell.Style.Font.Color = Color.Blue;
cell.Style.Font.IsItalic = true;
commentSheet.AutoFitRow(1);

Worksheet rawSheet = workbook.Worksheets[2]; // Raw Data sheet
cells = rawSheet.Cells;
cells.ImportDataTable(dsEvaluation.Tables[0], false, 1, 0, iRow, iCol); //Import data without heading and start at row 1(set it to false)

// set all row height for raw data sheet
for (int i = 0; i < iRow + 3; i++)
cells.SetRowHeight(i, 20);

workbook.Save(strCourseTitle + ".xls", FileFormatType.Default, SaveType.OpenInBrowser, this.Response);
}
}
catch (Exception err)
{
lblError.Text = "CreateExcelReport-- " + err.Message;
lblError.Visible = true;
}
}


}


Any status for this issue?

Thanks

Charlie

Hi,

Could you try the attached version.

Thank you.

Version 4.4.0.28 is still not working for

Bitmap bitmap = chart.ToImage();

bitmap return null.


Do you have sample code which works for ASP.net 2.0 for this version?

Thanks
Charlie

Hi Charlie,

Sorry! We do not support to convert Cone/Cylinder/Pyramid charts to image now .We will look into it soon.

I have used the bar type and version 4.4.0.28. It is working now.

thanks for your help.

Charlie