Free Support Forum - aspose.com

Large size of PNG files created using Aspose.Cells

We are using Aspose.Cells to create PNG snapshots of hundreds of Excel worksheets, for loading and displaying on end-users’ iPads. However the size of the PNG files being created is so large that we are running out of physical space. If, however, we take a PrintScreen of the same Worksheets, paste them into MSPaint, scale them to the same size (800 pixels wide) and save them from there, they are significantly smaller in size (about a quarter of the size).


Can you tell me if there is any way of reducing the file size of the PNG files being created?

I have included below some sample code, which shows what I am currently trying to do. Any Excel file will do, but I also included a sample “test.xlsx” as an attachment, which matches this code, plus two PNG versions, one created using the code and the other using MSPaint (notice the size difference):

=====================================

Workbook book = new Workbook(“test.xlsx”);
Worksheet sheet = book.Worksheets[0];

int width = 800;

//Define ImageOrPrintOptions
Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Bmp;

//Set only one page would be rendered for the image
imgOptions.OnePagePerSheet = true;
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;

PageSetup ps = sheet.PageSetup;
ps.PrintArea = “A1:J31”;

//Create the SheetRender object based on the sheet with its
//ImageOrPrintOptions attributes
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
//Render the sheet to a Bitmap
Bitmap bitmap = sr.ToImage(0);

//Convert the Bitmap to a BitmapSource
IntPtr hBitmap = bitmap.GetHbitmap();
try
{
BitmapSource image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());

//Scale the BitmapSource
TransformedBitmap scaledImage = new TransformedBitmap();
scaledImage.BeginInit();
scaledImage.Source = image;
scaledImage.Transform = new ScaleTransform(width / image.Width, width / image.Width);
scaledImage.EndInit();

//Format the BitmapSource
BitmapPalette ourPalette = BitmapPalettes.WebPalette;
PixelFormat ourFormat = PixelFormats.Bgr24;
FormatConvertedBitmap formattedImage = new FormatConvertedBitmap();
formattedImage.BeginInit();
formattedImage.Source = scaledImage;
formattedImage.DestinationPalette = ourPalette;
formattedImage.DestinationFormat = ourFormat;
formattedImage.EndInit();

// Create the output PNG file
FileStream stream = new FileStream(“test.png”, FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.Off;
encoder.Frames.Add(BitmapFrame.Create(formattedImage));
encoder.Save(stream);
}
finally
{
DeleteObject(hBitmap);
}
===========================================

Thanks,

Nigel


Hi,

Thanks for your posting and using Aspose.Cells.

Please download and try the latest version: Aspose.Cells
for .NET v7.4.2.5
It works fine.

It generates 47kb png file. The one you provided was 100kb. Please try the following code. We have also attached the output file for your reference.

C#



string filePath = @“F:\Shak-Data-RW\Downloads\test.xlsx”;


Workbook book = new Workbook(filePath);

Worksheet sheet = book.Worksheets[0];


int width = 800;


//Define ImageOrPrintOptions

Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();

imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Bmp;


//Set only one page would be rendered for the image

imgOptions.OnePagePerSheet = true;

imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;


PageSetup ps = sheet.PageSetup;

ps.PrintArea = “A1:J31”;


//Create the SheetRender object based on the sheet with its

//ImageOrPrintOptions attributes

Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);

//Render the sheet to a Bitmap

Bitmap bitmap = sr.ToImage(0);


//Convert the Bitmap to a BitmapSource


bitmap.Save(filePath + “.out.png”, ImageFormat.Png);

Thanks for that - it does indeed make a smaller file now.


One question though - we need to scale our capture to fit better on the iPad, so we want an image 800 pixels wide. Is there a way to get Aspose.Cells to deal with this at source, or do I have to use .NET image manipulating tools? Any guidance or code would be appreciated…

Thanks,

Nigel

Hi,

Thanks for your posting and using Aspose.Cells.

We are afraid, you cannot set image width and height in pixels using Aspose,Cells APIs. You will have to use .NET image manipulating tools for this purpose.

Okay - I understand.


On a related note then, my client still has a problem with the quality of the images being generated by Aspose (even if I don’t scale them afterwards). One thing I have noticed is that if I change the fonts being used on the Excel Template, I can get markedly different results.

For example, I note that Calibri gives poor results, but Arial gives better ones.

So… is there a reson for this? Perhaps you have a list of Fonts that Aspose.Cells supports?

Thanks,

Nigel

Hi,

Thanks for your posting and using Aspose.Cells.

Arial font is a default font. If there is no font installed, Aspose.Cells will use Arial font. Probably, this is the reason, Arial gives better results. To get correct results, fonts must be installed on user machine. There is no special list of fonts that Aspose.Cells support.