Пример #1
0
        /// <summary>
        /// Get the chart image
        /// </summary>
        /// <returns></returns>
        private Image GenerateImage()
        {
            Image Retval = null;
            try
            {
                this.moSpreadsheet.GetLock();

                //-- Set the aspect ratio of the image
                this.SetAspectRatio();

                SpreadsheetGear.Shapes.IShapes ExcelShapes = this.moSpreadsheet.ActiveWorksheet.Shapes;
                SpreadsheetGear.Drawing.Image SpreadsheetImage = new SpreadsheetGear.Drawing.Image(ExcelShapes[0]);

                Bitmap ExcelBitmap = SpreadsheetImage.GetBitmap();
                Retval = (Image)ExcelBitmap;
            }
            catch (Exception)
            {
            }
            finally
            {
                this.moSpreadsheet.ReleaseLock();
            }
            return Retval;
        }
Пример #2
0
        /// <summary>
        /// It replaces vertor Map image (.emf), present in xls presentation, with bitmap image like .png, .jpeg.
        /// </summary>
        /// <param name="mapPresentationFilePath"></param>
        public static bool ReplaceVectorMapImageWithBitmap(string mapPresentationFilePath, ImageFormat desiredBitmapFormat, string resultantOutputFilePath)
        {
            bool RetVal = false;
            SpreadsheetGear.IWorkbook workbook = null;
            SpreadsheetGear.IWorksheet worksheet = null;
            SpreadsheetGear.Shapes.IShapes ImagesShapes = null;
            SpreadsheetGear.Shapes.IShape OriginalMapShape = null;
            byte[] MapImageBytes = null;

            try
            {
                if (string.IsNullOrEmpty(mapPresentationFilePath) == false)
                {
                    if (File.Exists(mapPresentationFilePath))
                    {
                        //- Load xls.
                        workbook = SpreadsheetGear.Factory.GetWorkbook(mapPresentationFilePath);
                        worksheet = workbook.Worksheets[0];

                        //- Get all shapes present in WorkSheet 1
                        ImagesShapes = worksheet.Shapes;

                        if (ImagesShapes != null)
                        {
                            //- Load Map Image (Generally 2nd Shape in Sheet 1 represents Map image.)
                            foreach (SpreadsheetGear.Shapes.IShape imgShape in ImagesShapes)
                            {
                                if (imgShape.Type == SpreadsheetGear.Shapes.ShapeType.Picture && imgShape.Name == "Picture 2")
                                {
                                    OriginalMapShape = imgShape;

                                    //- Load Image into memory
                                    SpreadsheetGear.Drawing.Image MapImage = new SpreadsheetGear.Drawing.Image(imgShape);
                                    System.Drawing.Bitmap MapBitmap = MapImage.GetBitmap();
                                    MemoryStream ms = new MemoryStream();
                                    ms.Position = 0;

                                    //- Convert Map to png format
                                    MapBitmap.Save(ms, desiredBitmapFormat);
                                    MapImageBytes = ms.ToArray();
                                    break;
                                }
                            }

                            //- Save and close Workbook.
                            if (MapImageBytes != null)
                            {
                                // overwrite on previous Map shape.
                                worksheet.Shapes.AddPicture(MapImageBytes, OriginalMapShape.Left, OriginalMapShape.Top, OriginalMapShape.Width, OriginalMapShape.Height);

                                //- Delete Previous Image
                                OriginalMapShape.Delete();

                                if (File.Exists(resultantOutputFilePath))
                                {
                                    try
                                    {
                                        File.Delete(resultantOutputFilePath);
                                    }
                                    catch
                                    { }
                                }

                                workbook.SaveAs(resultantOutputFilePath, SpreadsheetGear.FileFormat.XLS97);

                                RetVal = true;
                                workbook.Close();
                                workbook = null;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                if (workbook != null)
                {
                    workbook.Close();
                }
            }

            return RetVal;
        }