How to get good quality image during chart to image or PDF conversion in C#, VB.NET?
This article explains how to get the best quality image in Chart to Image or PDF conversion using WinForms XlsIO.
A chart can be converted to an image or PDF with two different qualities of images, and the ScalingMode property of the IChartToImageConverter interface helps to achieve this in XlsIO.
Conversion with `Normal` scaling mode results in an image with normal image quality, whereas conversion with `Best` scaling mode results in an image with the best image quality and larger image size.
Code snippet to set image quality in Chart to Image or PDF conversion
// Set the image quality application.ChartToImageConverter.ScalingMode = ScalingMode.Best;
ScalingMode.Best consumes more memory compared to ScalingMode.Normal.
The following C#/VB complete code snippet shows how to set image quality in Chart to Image or PDF conversion using XlsIO.
C#
using Syncfusion.ExcelChartToImageConverter;
using Syncfusion.XlsIO;
using System.Drawing;
using System.IO;namespace ImageQuality
{
class Program
{
static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Instantiate the Excel application object
IApplication application = excelEngine.Excel;// Set default version
application.DefaultVersion = ExcelVersion.Excel2013;// Initializing the chart to image converter object
application.ChartToImageConverter = new ChartToImageConverter();// Set the image quality
application.ChartToImageConverter.ScalingMode = ScalingMode.Normal;// Open existing workbook with data entered
Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
Stream fileStream = assembly.GetManifestResourceStream("ImageQuality.Sample.xlsx");
IWorkbook workbook = application.Workbooks.Open(fileStream);// The first worksheet object in the worksheets collection is accessed
IWorksheet worksheet = workbook.Worksheets[0];// The first chart object in the charts collection is accessed
IChart chart = worksheet.Charts[0];// Creating the memory stream for chart image
MemoryStream stream = new MemoryStream();// Saving the chart as image
chart.SaveAsImage(stream);// Getting the image file from stream
Image image = Image.FromStream(stream);// Saving image stream to file
image.Save("Output.png");
}
}
}
}
VB
Imports System.Drawing Imports System.IO Imports Syncfusion.ExcelChartToImageConverter Imports Syncfusion.XlsIO Namespace ImageQuality Class Program Public Shared Sub Main(ByVal args As String()) Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the excel application object Dim application As IApplication = excelEngine.Excel 'Set default version application.DefaultVersion = ExcelVersion.Excel2013 'Initializing the chart to image converter object application.ChartToImageConverter = New ChartToImageConverter 'Set the image quality application.ChartToImageConverter.ScalingMode = ScalingMode.Best 'Open existing workbook with data entered Dim assembly As Assembly = GetType(Program).GetTypeInfo.Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("ImageQuality.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream) 'The first worksheet object in the worksheets collection is accessed Dim worksheet As IWorksheet = workbook.Worksheets(0) 'The first chart object in the charts collection is accessed Dim chart As IChart = worksheet.Charts(0) 'Creating the memory stream for chart image Dim stream As MemoryStream = New MemoryStream 'Saving the chart as image chart.SaveAsImage(stream) 'Getting the image file from stream Dim image As Image = Image.FromStream(stream) 'Saving image stream to file image.Save("Output.png") End Using End Sub End Class End Namespace
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to get good quality image during chart to image or PDF conversion in C#, VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!