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

Free Support Forum - aspose.com

Create image stream and add an image stream to excel worksheet in .NET

Hi, this is the other question following by my previous post.
We would like to generate an image from one sheet and insert it to other sheet with good performance.
Currently, we saved an image to local drive and insert back to the worksheet.
Code example below.
static void Main(string[] args)
{
// new Workbook(_sourceFile.FullName);
var _workbook = new Workbook(“Source\SourceFile.xlsx”);

        Worksheet sheet = _workbook.Worksheets["Disclosure"];

        Aspose.Cells.Rendering.ImageOrPrintOptions options = new ImageOrPrintOptions();
        options.HorizontalResolution = 400;
        options.VerticalResolution = 400;
        options.ImageType = Aspose.Cells.Drawing.ImageType.Jpeg;
        options.IsImageFitToPage = true;
        options.IsCellAutoFit = true;
        options.PrintingPage = PrintingPageType.IgnoreBlank;
        options.OnePagePerSheet = true;


        var targetFile = "outputConvertWorksheetToImageByPage.Jpeg";

        SheetRender sr = new SheetRender(sheet, options);
        for (int j = 0; j < sr.PageCount; j++)
        {
            sr.ToImage(j, targetFile);
        }

        _workbook.Worksheets["Target"].Pictures.Add(0, 0, "outputConvertWorksheetToImageByPage.Jpeg");
        _workbook.Save("TargetFile.xlsx");
    }

Is there any way ImageOrPrintOptions() and generate an image stream rather than physical image? Hence we can add image stream to new sheet?
something like that…
int indexPicture = worksheet.Pictures.Add(0, 0, stream);

@catalparue,

Please see the updated code segment to accomplish your task for your reference:
e.g.
Sample code:

.........
 options.IsCellAutoFit = true;
 options.PrintingPage = PrintingPageType.IgnoreBlank;
 options.OnePagePerSheet = true;


MemoryStream stream = new MemoryStream();
SheetRender sr = new SheetRender(sheet, options);
for (int j = 0; j < sr.PageCount; j++)
{

     sr.ToImage(j, stream);

}

_workbook.Worksheets["Target"].Pictures.Add(0, 0, stream);
......

Hope, this helps a bit.

thanks! it works good! in this case, what is the image format? PNG?

@catalparue,

Good to know that it works for your needs.

It would be in “Jpeg” as you have specified image type in your code (see the following line of code), you may change to other format though.