We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Chart.PlotArea.Area.FillFormat.ImageData with memory stream rather than filestream

Hi There,


I looked at http://www.aspose.com/docs/display/cellsnet/Set+Picture+as+Background+Fill+in+the+Chart and can get something implemented if I save the image I have to disk and then use the filestream to open it again. However, I don’t really want to save the image to disk and would rather use something like a MemoryStream.

See the below code snippet and attached workbook:

Workbook wb = new Workbook(“picture_as_fill.xlsx”);

Worksheet mainWS = wb.Worksheets[0];
Chart myChart = mainWS.Charts[0];

/// Setup picture saving options
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
options.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
options.Quality = 100;
options.VerticalResolution = 300;
options.HorizontalResolution = 300;
options.PrintingPage = PrintingPageType.IgnoreBlank;
options.IsImageFitToPage = false;

/// Get the image from the excel file
Bitmap penguinImage = mainWS.Pictures[0].ToImage(options);

/// Save image into a memorystream
MemoryStream memStream = new MemoryStream();
penguinImage.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] data = new byte[memStream.Length];
memStream.Read(data, 0, data.Length);
memStream.Close();
myChart.PlotArea.Area.FillFormat.ImageData = data;

The final line throws an exception of “Parameter is not valid”.

Is there something I need to tweak in order to add the ImageData for the fill from a MemoryStream?

Using Aspose.Cells .NET 7.5.0.5

I should also add that doing something like


mainWS.Pictures.Add(1, 18, memStream);

works with a memorystream, so its odd that Fill will not.

Hi,


Please change your code a bit for your needs, it works fine.

Sample code:

Workbook wb = new Workbook(“picture_as_fill.xlsx”);

Worksheet mainWS = wb.Worksheets[0];
Chart myChart = mainWS.Charts[0];
myChart.PlotArea.Area.FillFormat.ImageData = mainWS.Pictures[0].Data;

//…


Thank you.



Hi,


Also, you may try as following, it will work fine:

Sample code:

Workbook wb = new Workbook(“picture_as_fill.xlsx”);

Worksheet mainWS = wb.Worksheets[0];
Chart myChart = mainWS.Charts[0];

// Setup picture saving options
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
options.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
options.Quality = 100;
options.VerticalResolution = 300;
options.HorizontalResolution = 300;
options.PrintingPage = PrintingPageType.IgnoreBlank;
options.IsImageFitToPage = false;

/// Get the image from the excel file
Bitmap penguinImage = mainWS.Pictures[0].ToImage(options);

/// Save image into a memorystream
MemoryStream memStream = new MemoryStream();
penguinImage.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] data = memStream.ToArray();

myChart.PlotArea.Area.FillFormat.ImageData = data;




Both work like a charm, thank you.

Hi,


Good to know that your issue is sorted out now.

Feel free to contact us any time if you need further help or have some other query or issue, we will be happy to assist you soon.

Thank you.