Convert System.Drawing.Bitmap to Syncfusion.Drawing.Image

In .Net 6 web api, I am setting background image of Syncfusion.XlsIO.IWorksheet.PageSetup.BackgoundImage from utitlity which generated System.Drawing.Bitmap from text data.


In WPF application Worksheept PageSetup Background Image is type of System.Drawing.Image, while in .Net core FileFormat libraries only accepts Syncfusion.Drawing.Image.

I tried to generate stream from Bitmap, and create instace of Syncfusion Drawing image by 

Syncfusion.Drawing.Image.FromStream(stream). But It give me error 

System.ArgumentException: Stream at Syncfusion.Drawing.Image..ctor(Stream stream) at Syncfusion.Drawing.Image.FromStream(Stream stream)

Following code for reference.


var watermarkImage = TextToImageStream(watermarkTex);
for (int workSheetIndex = workbook.Worksheets.Count - 1; workSheetIndex >= 0; workSheetIndex--)
{
Syncfusion.XlsIO.IWorksheet sheet = workbook.Worksheets[workSheetIndex];
sheet.PageSetup.PrintComments = Syncfusion.XlsIO.ExcelPrintLocation.PrintSheetEnd;
sheet.PageSetup.BackgoundImage = Syncfusion.Drawing.Image.FromStream(watermarkImage);
}


private MemoryStream TextToImageStream(String text)
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap bitmap = TextToImage(text);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
}




3 Replies

RS Ramya Sivakumar Syncfusion Team July 25, 2022 02:29 PM UTC

Hi Kamran,


Greetings from Syncfusion.


We are unable to reproduce the issue. The image is inserted properly at our end. We suspect that the issue is caused because of the TexttoImageStream method.


We have attached the sample which was tried at our end. Kindly share a sample illustrating the issue or else modify the following sample and reproduce the issue which would help us to proceed further.


Sample link - https://www.syncfusion.com/downloads/support/directtrac/general/ze/F-176408430227480


Regards,

Ramya.



KA Kamran replied to Ramya Sivakumar July 31, 2022 01:09 PM UTC

Thank youRamya Sivakumar  for your response. I review my code, and found resultant stream was closed from  TextToImageStream() caused by using block. 



RS Ramya Sivakumar Syncfusion Team August 1, 2022 11:04 AM UTC

Hi Kamran,


You’re welcome.


We are glad that the provided suggestion helped you. Kindly let us know if you need any further assistance.


Regards,

Ramya.


Loader.
Up arrow icon