[12074] | 1 | /*******************************************************************************
|
---|
| 2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
| 3 | *
|
---|
| 4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
| 5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
| 6 | *
|
---|
| 7 | * Copyright (C) 2011 Jan Källman
|
---|
| 8 | *
|
---|
| 9 | * This library is free software; you can redistribute it and/or
|
---|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 11 | * License as published by the Free Software Foundation; either
|
---|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 13 |
|
---|
| 14 | * This library is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 17 | * See the GNU Lesser General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
| 20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
| 21 | *
|
---|
| 22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
| 23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
| 24 | *
|
---|
| 25 | * Code change notes:
|
---|
| 26 | *
|
---|
| 27 | * Author Change Date
|
---|
| 28 | *******************************************************************************
|
---|
| 29 | * Jan Källman Added 10-SEP-2009
|
---|
| 30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
| 31 | *******************************************************************************/
|
---|
| 32 | using System;
|
---|
| 33 | using System.Collections.Generic;
|
---|
| 34 | using System.Linq;
|
---|
| 35 | using System.Text;
|
---|
| 36 | using System.Xml;
|
---|
| 37 | using System.Drawing;
|
---|
| 38 | using System.IO;
|
---|
| 39 | using OfficeOpenXml.Drawing;
|
---|
| 40 | using OfficeOpenXml.Packaging;
|
---|
| 41 | using OfficeOpenXml.Utils;
|
---|
| 42 | namespace OfficeOpenXml
|
---|
| 43 | {
|
---|
| 44 | /// <summary>
|
---|
| 45 | /// An image that fills the background of the worksheet.
|
---|
| 46 | /// </summary>
|
---|
| 47 | public class ExcelBackgroundImage : XmlHelper
|
---|
| 48 | {
|
---|
| 49 | ExcelWorksheet _workSheet;
|
---|
| 50 | /// <summary>
|
---|
| 51 | ///
|
---|
| 52 | /// </summary>
|
---|
| 53 | /// <param name="nsm"></param>
|
---|
| 54 | /// <param name="topNode">The topnode of the worksheet</param>
|
---|
| 55 | /// <param name="workSheet">Worksheet reference</param>
|
---|
| 56 | internal ExcelBackgroundImage(XmlNamespaceManager nsm, XmlNode topNode, ExcelWorksheet workSheet) :
|
---|
| 57 | base(nsm, topNode)
|
---|
| 58 | {
|
---|
| 59 | _workSheet = workSheet;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | const string BACKGROUNDPIC_PATH = "d:picture/@r:id";
|
---|
| 63 | /// <summary>
|
---|
| 64 | /// The background image of the worksheet.
|
---|
| 65 | /// The image will be saved internally as a jpg.
|
---|
| 66 | /// </summary>
|
---|
| 67 | public Image Image
|
---|
| 68 | {
|
---|
| 69 | get
|
---|
| 70 | {
|
---|
| 71 | string relID = GetXmlNodeString(BACKGROUNDPIC_PATH);
|
---|
| 72 | if (!string.IsNullOrEmpty(relID))
|
---|
| 73 | {
|
---|
| 74 | var rel = _workSheet.Part.GetRelationship(relID);
|
---|
| 75 | var imagePart = _workSheet.Part.Package.GetPart(UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri));
|
---|
| 76 | return Image.FromStream(imagePart.GetStream());
|
---|
| 77 | }
|
---|
| 78 | return null;
|
---|
| 79 | }
|
---|
| 80 | set
|
---|
| 81 | {
|
---|
| 82 | DeletePrevImage();
|
---|
| 83 | if (value == null)
|
---|
| 84 | {
|
---|
| 85 | DeleteAllNode(BACKGROUNDPIC_PATH);
|
---|
| 86 | }
|
---|
| 87 | else
|
---|
| 88 | {
|
---|
| 89 | ImageConverter ic = new ImageConverter();
|
---|
| 90 | byte[] img = (byte[])ic.ConvertTo(value, typeof(byte[]));
|
---|
| 91 | var ii = _workSheet.Workbook._package.AddImage(img);
|
---|
| 92 | var rel = _workSheet.Part.CreateRelationship(ii.Uri, Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
|
---|
| 93 | SetXmlNodeString(BACKGROUNDPIC_PATH, rel.Id);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Set the picture from an image file.
|
---|
| 99 | /// The image file will be saved as a blob, so make sure Excel supports the image format.
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <param name="PictureFile">The image file.</param>
|
---|
| 102 | public void SetFromFile(FileInfo PictureFile)
|
---|
| 103 | {
|
---|
| 104 | DeletePrevImage();
|
---|
| 105 |
|
---|
| 106 | Image img;
|
---|
| 107 | try
|
---|
| 108 | {
|
---|
| 109 | img = Image.FromFile(PictureFile.FullName);
|
---|
| 110 | }
|
---|
| 111 | catch (Exception ex)
|
---|
| 112 | {
|
---|
| 113 | throw (new InvalidDataException("File is not a supported image-file or is corrupt", ex));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | ImageConverter ic = new ImageConverter();
|
---|
| 117 | string contentType = ExcelPicture.GetContentType(PictureFile.Extension);
|
---|
| 118 | var imageURI = XmlHelper.GetNewUri(_workSheet._package.Package, "/xl/media/" + PictureFile.Name.Substring(0, PictureFile.Name.Length - PictureFile.Extension.Length) + "{0}" + PictureFile.Extension);
|
---|
| 119 |
|
---|
| 120 | byte[] fileBytes = (byte[])ic.ConvertTo(img, typeof(byte[]));
|
---|
| 121 | var ii = _workSheet.Workbook._package.AddImage(fileBytes, imageURI, contentType);
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | if (_workSheet.Part.Package.PartExists(imageURI) && ii.RefCount==1) //The file exists with another content, overwrite it.
|
---|
| 125 | {
|
---|
| 126 | //Remove the part if it exists
|
---|
| 127 | _workSheet.Part.Package.DeletePart(imageURI);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | var imagePart = _workSheet.Part.Package.CreatePart(imageURI, contentType, CompressionLevel.None);
|
---|
| 131 | //Save the picture to package.
|
---|
| 132 |
|
---|
| 133 | var strm = imagePart.GetStream(FileMode.Create, FileAccess.Write);
|
---|
| 134 | strm.Write(fileBytes, 0, fileBytes.Length);
|
---|
| 135 |
|
---|
| 136 | var rel = _workSheet.Part.CreateRelationship(imageURI, Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
|
---|
| 137 | SetXmlNodeString(BACKGROUNDPIC_PATH, rel.Id);
|
---|
| 138 | }
|
---|
| 139 | private void DeletePrevImage()
|
---|
| 140 | {
|
---|
| 141 | var relID = GetXmlNodeString(BACKGROUNDPIC_PATH);
|
---|
| 142 | if (relID != "")
|
---|
| 143 | {
|
---|
| 144 | var ic = new ImageConverter();
|
---|
| 145 | byte[] img = (byte[])ic.ConvertTo(Image, typeof(byte[]));
|
---|
| 146 | var ii = _workSheet.Workbook._package.GetImageInfo(img);
|
---|
| 147 |
|
---|
| 148 | //Delete the relation
|
---|
| 149 | _workSheet.Part.DeleteRelationship(relID);
|
---|
| 150 |
|
---|
| 151 | //Delete the image if there are no other references.
|
---|
| 152 | if (ii != null && ii.RefCount == 1)
|
---|
| 153 | {
|
---|
| 154 | if (_workSheet.Part.Package.PartExists(ii.Uri))
|
---|
| 155 | {
|
---|
| 156 | _workSheet.Part.Package.DeletePart(ii.Uri);
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|