Hi,
I am using aspose Cells licensed version and when I send over a chart from Excel . Its not coming up correctly when I use the following code.
public static string ConvertToBase64EncodedStringByKeepingSameImageType(Picture picture, IAsposeLicenseManager licenseManager, Exhibit exhibit)
{
licenseManager.CheckAndLogCellsLicensed();
using var stream = new MemoryStream(picture.Data);
using var image = Image.FromStream(stream);
using var outStream = new MemoryStream();
string imageContent;
string format;
string exhibitContent;
var imageType = (picture.ImageType is ImageType.Emf or ImageType.Wmf) ? ImageType.Png : picture.ImageType;
var options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
HorizontalResolution = 1200,
VerticalResolution = 1200,
Quality = 100,
OnlyArea = true
};
picture.ToImage(outStream, options);
imageContent = Convert.ToBase64String(outStream.ToArray());
format = string.Format(ImageConstants.EncodedImagePrefixWithType, imageType).ToLower();
exhibitContent = string.Concat(format, imageContent);
return exhibitContent;
}
@usman.khan
It seems you are experiencing issues with the rendering of charts when using Aspose.Cells. To ensure that charts are converted correctly to images, you can follow these guidelines:
-
Use the Correct Method: Make sure you are using the ToImage
method of the Chart
class. This method has several overloads that allow you to specify various parameters, including image format and resolution.
-
Image Options: When converting the chart to an image, you can customize the rendering by using the ImageOrPrintOptions
class. This allows you to set properties such as:
ImageType
: Specify the desired image format (e.g., PNG, JPEG).
HorizontalResolution
and VerticalResolution
: Set the resolution for the output image.
Quality
: Adjust the quality of the image.
-
Supported Chart Types: Be aware that not all chart types may be supported for rendering. Check the documentation to ensure that the chart type you are using is compatible with the conversion process.
Here is a sample code snippet that demonstrates how to convert a chart to an image using Aspose.Cells:
// Assuming 'workbook' is your Workbook object and 'chartIndex' is the index of your chart
Chart chart = workbook.Worksheets[0].Charts[chartIndex];
ImageOrPrintOptions options = new ImageOrPrintOptions
{
ImageType = ImageType.Png,
HorizontalResolution = 1200,
VerticalResolution = 1200,
Quality = 100
};
using (MemoryStream stream = new MemoryStream())
{
chart.ToImage(stream, options);
string imageContent = Convert.ToBase64String(stream.ToArray());
// Now you can use imageContent as needed
}
By following these steps and using the provided code, you should be able to resolve the issues with chart rendering in Aspose.Cells. If you continue to face problems, please check the specific chart type you are using and ensure it is supported for image conversion.
For more detailed information, you can refer to the official documentation on chart to image conversion here.
Sources:
[1]: Chart to Image Conversion in .NET | Aspose.Cells .NET Excel Processing API
[2]: Chart to Image - Aspose Documentation
I have tried all these things. Some of the charts are coming up correctly but I have this particular chart which when converted to Image and sent over is not coming up correctly.
Could you please help.
This is the original chart in Excel
image.png (18.9 KB)
And it becomes this after conversion using Aspose Cells.
image.png (77.8 KB)
Please help
@usman.khan,
Could you please zip and attach your template Excel file (containing the chart) here. We will evaluate and test using your Excel file for rendering the chart to image with Aspose.Cells for .NET API and update you soon.