Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Nov 11, 2019 7:44 am

Hello!
Is it possible to insert a picture in excel from a stream? The file does not exist in memory and I would rather not store it in memory since it is a server on which I do not have write permission.
What options are there that is not a physical file?

Thank you!

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Mon Nov 11, 2019 8:24 am

Hi,

Thanks for your inquiry.
Yes, there are multiple overloaded functions in sheet.Pictures.Add which could insert a picture from a stream. For example:
Code: Select all
sheet.Pictures.Add(int topRow, int leftColumn, int bottomRow, int rightColumn, Stream stream);

If there is any question, welcome to write back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Nov 11, 2019 10:04 am

Thank you very much for your help.

Really I need replace a picture:
The picture exists already in the excel file.

byte[] imageBytes --> I had a value read from database
foreach (ExcelPicture pic in _listPictures)
{
if (pic.AlternativeTextTitle != null && pic.AlternativeTextTitle.Length > 0 && pic.AlternativeTextTitle = "MyPicture")
{
if (imageBytes != null)
{
float w = pic.Width;
float h = pic.Height;

----------->I need load de picture from imageBytes
----------->pic.Picture .............

------> Resize de picture to the original size
pic.Width = w;
pic.Height = h;
}
}

}

If it possible to do that?

Thank you very much.

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Tue Nov 12, 2019 2:05 am

Hi,

Thanks for your response.
Yes, please refer to following code snippet:
Code: Select all
            //the image data from your database
            Image Replace = Image.FromStream(stream);
            ExcelPicture picture1 = sheet.Pictures[0];
            //replace the image
            picture1.Picture = Replace;


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Nov 12, 2019 3:44 pm

Hello!
The problem is that if i close the stream before save excel file, them one error occurs and this is a problem when is a distributed application.
If not, the solution work fine.

Thank you very much!

LauraNM
 
Posts: 77
Joined: Tue Feb 19, 2019 1:23 pm

Wed Nov 13, 2019 1:44 am

Hi,

Thanks for your feedback.
I have noticed the error when saving file after closing stream, and posted it to our Dev team.
If there is any update, we will let you know. Sorry for the inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Nov 18, 2019 9:21 am

Hi,

Thanks for waiting.
After further investigation, we found the error is caused by System.Drawing.Image. You could check it via following code:
Code: Select all
            FileStream stream = File.OpenRead(@"E:\image\flower5.jpg");
            Image Replace = Image.FromStream(stream);
            stream.Close();
            //This will throw the GDI+ error.
            Replace.Save("new.jpg");

So you cannot close image stream before saving it. But you could close the stream after saving file.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS