Wednesday, 28 February 2018 08:29

Convert Excel Sheet to a High-Resolution Image in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

Sometimes, you may want to convert Excel sheet to a high-resolution image, especially when the Excel report contains graphs or pictures. This article will show you how to set image resolution when saving Excel sheet to JPG using Spire.XLS.

Step 1: Create a custom function that you can use to reset the image resolution.

private static Bitmap ResetResolution(Metafile mf, float resolution)
{
    int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
    int height = (int)(mf.Height * resolution / mf.VerticalResolution);
    Bitmap bmp = new Bitmap(width, height);
    bmp.SetResolution(resolution, resolution);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(mf, 0, 0);
    g.Dispose();
    return bmp;
}

Step 2: Create a workbook instance and load the sample Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013);

Step 3: Get the worksheet you want to convert.

Worksheet worksheet = workbook.Worksheets[0];

Step 4: Convert the worksheet to EMF stream.

MemoryStream ms = new MemoryStream();
worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);

Step 5: Create an image from the EMF stream, and call ResetResolution to reset the resolution for the image.

Image image = Image.FromStream(ms);
Bitmap images = ResetResolution(image as Metafile, 300);

Step 6: Save the image in JPG file format.

images.Save("Result.jpg", ImageFormat.Jpeg);

Output:

Convert Excel Sheet to a High-Resolution Image in C#, VB.NET

Full Code:

[C#]
static void Main(string[] args)
{
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013);‬
    Worksheet worksheet = workbook.Worksheets[0];

    using (MemoryStream ms = new MemoryStream())
    {

        worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);
        Image image = Image.FromStream(ms);
        Bitmap images = ResetResolution(image as Metafile, 300);
        images.Save("Result.jpg", ImageFormat.Jpeg);
    }
}
private static Bitmap ResetResolution(Metafile mf, float resolution)
{
    int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
    int height = (int)(mf.Height * resolution / mf.VerticalResolution);
    Bitmap bmp = new Bitmap(width, height);
    bmp.SetResolution(resolution, resolution);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(mf, 0, 0);
    g.Dispose();
    return bmp;
}
[VB.NET]
Shared Sub Main(ByVal args() As String)
    Dim workbook As Workbook = New Workbook()
    workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013)‬
    Dim worksheet As Worksheet = workbook.Worksheets(0)


    Imports(MemoryStream ms = New MemoryStream())
    {
 
        worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn)
        Dim image As Image = Image.FromStream(ms)
        Dim images As Bitmap = ResetResolution(image as Metafile, 300)
        images.Save("Result.jpg", ImageFormat.Jpeg)
    }
End Sub
Private Shared Function ResetResolution(ByVal mf As Metafile, ByVal resolution As single) As Bitmap
    Dim width As Integer = CType((mf.Width * resolution / mf.HorizontalResolution), Integer)
    Dim height As Integer = CType((mf.Height * resolution / mf.VerticalResolution), Integer)
    Dim bmp As Bitmap = New Bitmap(width, height)
    bmp.SetResolution(resolution, resolution)
    Dim g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(mf, 0, 0)
    g.Dispose()
    Return bmp
End Function

Additional Info

  • tutorial_title: