1
Vote

Excel: Office has detected a problem with this file. Editing it may harm your computer. Click for more details

description

If I replace the Silverlight ImageTranslator.TranslateImageToBytes with a WinForms implementation as below, Excel opens with a warning message, stating that there is a problem with the file.
 
 
Quote:
Excel: Office has detected a problem with this file. Editing it may harm your computer. Click for more details
 
 
 
This is my method:
 
    static void SaveFileWithImage()
    {
        // open file dailog for selecting export file
        //System.Windows.Forms.SaveFileDialog sDialog = new System.Windows.Forms.SaveFileDialog();
        //sDialog.Filter = "Excel Files(*.xls)|*.xls";
 
        //if (sDialog.ShowDialog() == true)
        {
            // create a workbook object
            Workbook workbook = new Workbook();
            //Create a worksheet object 
            Worksheet worksheet1 = new Worksheet("SheetWithImage");
 
            // create a spreadsheet picture object
            Lite.ExcelLibrary.SpreadSheet.Picture pic = new Lite.ExcelLibrary.SpreadSheet.Picture();
            //set its image property from silverlight image control
            // image formats 0xF01E=png,0xF01D=jpeg
            //ImageTranslator.TranslateImageToBytes translate an image control to byte array
            // that will be used by excel picture object to plot picture in excel file.
            //////////////////////////pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(ImageTranslator.TranslateImageToBytes(this.imgExport), 0xF01E);
 
            System.Drawing.Image xxx = new System.Drawing.Bitmap(@"C:\temp\images\example.jpg");
            pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(imageToByteArray(xxx), 0xF01E);
 
 
 
            //set picture size
            pic.TopLeftCorner = new CellAnchor(1, 1, 10, 10);
            pic.BottomRightCorner = new CellAnchor(8, 5, 10, 10);
            // add picture to spreadsheet
            worksheet1.AddPicture(pic);
            /// add worksheet to workbook
            workbook.Worksheets.Add(worksheet1);
            // get the stream of selected file
            //System.IO.Stream sFile = sDialog.OpenFile();
            System.IO.Stream sFile = new System.IO.FileStream("WithImage.xls", System.IO.FileMode.Create);

            // Save Excel file 
            workbook.Save(sFile);
        }
    }
 
 
    public static byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

comments

SebCor wrote Jul 6, 2011 at 12:46 PM

Isn't it because of the value of the ImageFormat ?
In your exemple, you are using 0xF01E (png) for a jpg image.

In my opinion, using EscherRecordType.MsofbtBlipBitmapJPEG is more readable.

wrote Feb 14, 2013 at 7:25 PM