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 Initial Release 2009-10-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using System.IO;
|
---|
37 | using System.IO.Packaging;
|
---|
38 | using System.Drawing;
|
---|
39 | using System.Drawing.Imaging;
|
---|
40 | using System.Diagnostics;
|
---|
41 |
|
---|
42 | namespace OfficeOpenXml.Drawing
|
---|
43 | {
|
---|
44 | /// <summary>
|
---|
45 | /// An image object
|
---|
46 | /// </summary>
|
---|
47 | public sealed class ExcelPicture : ExcelDrawing
|
---|
48 | {
|
---|
49 | #region "Constructors"
|
---|
50 | internal ExcelPicture(ExcelDrawings drawings, XmlNode node) :
|
---|
51 | base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name")
|
---|
52 | {
|
---|
53 | XmlNode picNode = node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip", drawings.NameSpaceManager);
|
---|
54 | if (picNode != null)
|
---|
55 | {
|
---|
56 | RelPic = drawings.Part.GetRelationship(picNode.Attributes["r:embed"].Value);
|
---|
57 | UriPic = PackUriHelper.ResolvePartUri(drawings.UriDrawing, RelPic.TargetUri);
|
---|
58 |
|
---|
59 | Part = drawings.Part.Package.GetPart(UriPic);
|
---|
60 | FileInfo f = new FileInfo(UriPic.OriginalString);
|
---|
61 | ContentType = GetContentType(f.Extension);
|
---|
62 | _image = Image.FromStream(Part.GetStream());
|
---|
63 | ImageConverter ic=new ImageConverter();
|
---|
64 | var iby=(byte[])ic.ConvertTo(_image, typeof(byte[]));
|
---|
65 | var ii = _drawings._package.LoadImage(iby, UriPic, Part);
|
---|
66 | ImageHash = ii.Hash;
|
---|
67 |
|
---|
68 | string relID = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@r:id");
|
---|
69 | if (!string.IsNullOrEmpty(relID))
|
---|
70 | {
|
---|
71 | HypRel = drawings.Part.GetRelationship(relID);
|
---|
72 | if (HypRel.TargetUri.IsAbsoluteUri)
|
---|
73 | {
|
---|
74 | _hyperlink = new ExcelHyperLink(HypRel.TargetUri.AbsoluteUri);
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | _hyperlink = new ExcelHyperLink(HypRel.TargetUri.OriginalString, UriKind.Relative);
|
---|
79 | }
|
---|
80 | ((ExcelHyperLink)_hyperlink).ToolTip = GetXmlNodeString("xdr:pic/xdr:nvPicPr/xdr:cNvPr/a:hlinkClick/@tooltip");
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | internal ExcelPicture(ExcelDrawings drawings, XmlNode node, Image image) :
|
---|
85 | this(drawings, node, image, null)
|
---|
86 | {
|
---|
87 | }
|
---|
88 | internal ExcelPicture(ExcelDrawings drawings, XmlNode node, Image image, Uri hyperlink) :
|
---|
89 | base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name")
|
---|
90 | {
|
---|
91 | XmlElement picNode = node.OwnerDocument.CreateElement("xdr", "pic", ExcelPackage.schemaSheetDrawings);
|
---|
92 | node.InsertAfter(picNode,node.SelectSingleNode("xdr:to",NameSpaceManager));
|
---|
93 | _hyperlink = hyperlink;
|
---|
94 | picNode.InnerXml = PicStartXml();
|
---|
95 |
|
---|
96 | node.InsertAfter(node.OwnerDocument.CreateElement("xdr", "clientData", ExcelPackage.schemaSheetDrawings), picNode);
|
---|
97 |
|
---|
98 | Package package = drawings.Worksheet._package.Package;
|
---|
99 | //Get the picture if it exists or save it if not.
|
---|
100 | _image = image;
|
---|
101 | string relID = SavePicture(image);
|
---|
102 |
|
---|
103 | //Create relationship
|
---|
104 | node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relID;
|
---|
105 |
|
---|
106 | SetPosDefaults(image);
|
---|
107 | package.Flush();
|
---|
108 | }
|
---|
109 | internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile) :
|
---|
110 | this(drawings,node,imageFile,null)
|
---|
111 | {
|
---|
112 | }
|
---|
113 | internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile, Uri hyperlink) :
|
---|
114 | base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name")
|
---|
115 | {
|
---|
116 | XmlElement picNode = node.OwnerDocument.CreateElement("xdr", "pic", ExcelPackage.schemaSheetDrawings);
|
---|
117 | node.InsertAfter(picNode, node.SelectSingleNode("xdr:to", NameSpaceManager));
|
---|
118 | _hyperlink = hyperlink;
|
---|
119 | picNode.InnerXml = PicStartXml();
|
---|
120 |
|
---|
121 | node.InsertAfter(node.OwnerDocument.CreateElement("xdr", "clientData", ExcelPackage.schemaSheetDrawings), picNode);
|
---|
122 |
|
---|
123 | //Changed to stream 2/4-13 (issue 14834). Thnx SClause
|
---|
124 | var package = drawings.Worksheet._package.Package;
|
---|
125 | ContentType = GetContentType(imageFile.Extension);
|
---|
126 | var imagestream = new FileStream(imageFile.FullName, FileMode.Open, FileAccess.Read);
|
---|
127 | _image = Image.FromStream(imagestream);
|
---|
128 | ImageConverter ic = new ImageConverter();
|
---|
129 | var img = (byte[])ic.ConvertTo(_image, typeof(byte[]));
|
---|
130 | imagestream.Close();
|
---|
131 |
|
---|
132 | UriPic = GetNewUri(package, "/xl/media/{0}" + imageFile.Name);
|
---|
133 | var ii = _drawings._package.AddImage(img, UriPic, ContentType);
|
---|
134 | string relID;
|
---|
135 | if(!drawings._hashes.ContainsKey(ii.Hash))
|
---|
136 | {
|
---|
137 | Part = ii.Part;
|
---|
138 | RelPic = drawings.Part.CreateRelationship(PackUriHelper.GetRelativeUri(drawings.UriDrawing, ii.Uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
|
---|
139 | relID = RelPic.Id;
|
---|
140 | _drawings._hashes.Add(ii.Hash, relID);
|
---|
141 | AddNewPicture(img, relID);
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | relID = drawings._hashes[ii.Hash];
|
---|
146 | var rel = _drawings.Part.GetRelationship(relID);
|
---|
147 | UriPic = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
|
---|
148 | }
|
---|
149 | SetPosDefaults(Image);
|
---|
150 | //Create relationship
|
---|
151 | node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relID;
|
---|
152 | package.Flush();
|
---|
153 | }
|
---|
154 |
|
---|
155 | internal static string GetContentType(string extension)
|
---|
156 | {
|
---|
157 | switch (extension.ToLower())
|
---|
158 | {
|
---|
159 | case ".bmp":
|
---|
160 | return "image/bmp";
|
---|
161 | case ".jpg":
|
---|
162 | case ".jpeg":
|
---|
163 | return "image/jpeg";
|
---|
164 | case ".gif":
|
---|
165 | return "image/gif";
|
---|
166 | case ".png":
|
---|
167 | return "image/png";
|
---|
168 | case ".cgm":
|
---|
169 | return "image/cgm";
|
---|
170 | case ".emf":
|
---|
171 | return "image/x-emf";
|
---|
172 | case ".eps":
|
---|
173 | return "image/x-eps";
|
---|
174 | case ".pcx":
|
---|
175 | return "image/x-pcx";
|
---|
176 | case ".tga":
|
---|
177 | return "image/x-tga";
|
---|
178 | case ".tif":
|
---|
179 | case ".tiff":
|
---|
180 | return "image/x-tiff";
|
---|
181 | case ".wmf":
|
---|
182 | return "image/x-wmf";
|
---|
183 | default:
|
---|
184 | return "image/jpeg";
|
---|
185 |
|
---|
186 | }
|
---|
187 | }
|
---|
188 | //Add a new image to the compare collection
|
---|
189 | private void AddNewPicture(byte[] img, string relID)
|
---|
190 | {
|
---|
191 | var newPic = new ExcelDrawings.ImageCompare();
|
---|
192 | newPic.image = img;
|
---|
193 | newPic.relID = relID;
|
---|
194 | //_drawings._pics.Add(newPic);
|
---|
195 | }
|
---|
196 | #endregion
|
---|
197 | private string SavePicture(Image image)
|
---|
198 | {
|
---|
199 | ImageConverter ic = new ImageConverter();
|
---|
200 | byte[] img = (byte[])ic.ConvertTo(image, typeof(byte[]));
|
---|
201 | var ii = _drawings._package.AddImage(img);
|
---|
202 |
|
---|
203 | if (_drawings._hashes.ContainsKey(ii.Hash))
|
---|
204 | {
|
---|
205 | var relID = _drawings._hashes[ii.Hash];
|
---|
206 | var rel = _drawings.Part.GetRelationship(relID);
|
---|
207 | UriPic = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
|
---|
208 | return relID;
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | UriPic = ii.Uri;
|
---|
213 | }
|
---|
214 |
|
---|
215 | //Set the Image and save it to the package.
|
---|
216 | RelPic = _drawings.Part.CreateRelationship(PackUriHelper.GetRelativeUri(_drawings.UriDrawing, UriPic), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
|
---|
217 |
|
---|
218 | //AddNewPicture(img, picRelation.Id);
|
---|
219 | _drawings._hashes.Add(ii.Hash, RelPic.Id);
|
---|
220 | ImageHash = ii.Hash;
|
---|
221 |
|
---|
222 | return RelPic.Id;
|
---|
223 | }
|
---|
224 | private void SetPosDefaults(Image image)
|
---|
225 | {
|
---|
226 | EditAs = eEditAs.OneCell;
|
---|
227 | SetPixelWidth(image.Width, image.HorizontalResolution);
|
---|
228 | SetPixelHeight(image.Height, image.VerticalResolution);
|
---|
229 | }
|
---|
230 |
|
---|
231 | private string PicStartXml()
|
---|
232 | {
|
---|
233 | StringBuilder xml = new StringBuilder();
|
---|
234 |
|
---|
235 | xml.Append("<xdr:nvPicPr>");
|
---|
236 |
|
---|
237 | if (_hyperlink == null)
|
---|
238 | {
|
---|
239 | xml.AppendFormat("<xdr:cNvPr id=\"{0}\" descr=\"\" />", _id);
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | HypRel = _drawings.Part.CreateRelationship(_hyperlink, TargetMode.External, ExcelPackage.schemaHyperlink);
|
---|
244 | xml.AppendFormat("<xdr:cNvPr id=\"{0}\" descr=\"\">", _id);
|
---|
245 | if (HypRel != null)
|
---|
246 | {
|
---|
247 | if (_hyperlink is ExcelHyperLink)
|
---|
248 | {
|
---|
249 | xml.AppendFormat("<a:hlinkClick xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"{0}\" tooltip=\"{1}\"/>",
|
---|
250 | HypRel.Id, ((ExcelHyperLink)_hyperlink).ToolTip);
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | xml.AppendFormat("<a:hlinkClick xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"{0}\" />",
|
---|
255 | HypRel.Id);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | xml.Append("</xdr:cNvPr>");
|
---|
259 | }
|
---|
260 |
|
---|
261 | xml.Append("<xdr:cNvPicPr><a:picLocks noChangeAspect=\"1\" /></xdr:cNvPicPr></xdr:nvPicPr><xdr:blipFill><a:blip xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:embed=\"\" cstate=\"print\" /><a:stretch><a:fillRect /> </a:stretch> </xdr:blipFill> <xdr:spPr> <a:xfrm> <a:off x=\"0\" y=\"0\" /> <a:ext cx=\"0\" cy=\"0\" /> </a:xfrm> <a:prstGeom prst=\"rect\"> <a:avLst /> </a:prstGeom> </xdr:spPr>");
|
---|
262 |
|
---|
263 | return xml.ToString();
|
---|
264 | }
|
---|
265 |
|
---|
266 | internal string ImageHash { get; set; }
|
---|
267 | Image _image = null;
|
---|
268 | /// <summary>
|
---|
269 | /// The Image
|
---|
270 | /// </summary>
|
---|
271 | public Image Image
|
---|
272 | {
|
---|
273 | get
|
---|
274 | {
|
---|
275 | return _image;
|
---|
276 | }
|
---|
277 | set
|
---|
278 | {
|
---|
279 | if (value != null)
|
---|
280 | {
|
---|
281 | _image = value;
|
---|
282 | try
|
---|
283 | {
|
---|
284 | string relID = SavePicture(value);
|
---|
285 |
|
---|
286 | //Create relationship
|
---|
287 | TopNode.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relID;
|
---|
288 | //_image.Save(Part.GetStream(FileMode.Create, FileAccess.Write), _imageFormat); //Always JPEG here at this point.
|
---|
289 | }
|
---|
290 | catch(Exception ex)
|
---|
291 | {
|
---|
292 | throw(new Exception("Can't save image - " + ex.Message, ex));
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | ImageFormat _imageFormat=ImageFormat.Jpeg;
|
---|
298 | /// <summary>
|
---|
299 | /// Image format
|
---|
300 | /// If the picture is created from an Image this type is always Jpeg
|
---|
301 | /// </summary>
|
---|
302 | public ImageFormat ImageFormat
|
---|
303 | {
|
---|
304 | get
|
---|
305 | {
|
---|
306 | return _imageFormat;
|
---|
307 | }
|
---|
308 | internal set
|
---|
309 | {
|
---|
310 | _imageFormat = value;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | internal string ContentType
|
---|
314 | {
|
---|
315 | get;
|
---|
316 | set;
|
---|
317 | }
|
---|
318 | /// <summary>
|
---|
319 | /// Set the size of the image in percent from the orginal size
|
---|
320 | /// Note that resizing columns / rows after using this function will effect the size of the picture
|
---|
321 | /// </summary>
|
---|
322 | /// <param name="Percent">Percent</param>
|
---|
323 | public override void SetSize(int Percent)
|
---|
324 | {
|
---|
325 | if(Image == null)
|
---|
326 | {
|
---|
327 | base.SetSize(Percent);
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | int width = Image.Width;
|
---|
332 | int height = Image.Height;
|
---|
333 |
|
---|
334 | width = (int)(width * ((decimal)Percent / 100));
|
---|
335 | height = (int)(height * ((decimal)Percent / 100));
|
---|
336 |
|
---|
337 | SetPixelWidth(width, Image.HorizontalResolution);
|
---|
338 | SetPixelHeight(height, Image.VerticalResolution);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | internal Uri UriPic { get; set; }
|
---|
342 | internal PackageRelationship RelPic {get; set;}
|
---|
343 | internal PackageRelationship HypRel { get; set; }
|
---|
344 | internal PackagePart Part;
|
---|
345 |
|
---|
346 | internal new string Id
|
---|
347 | {
|
---|
348 | get { return Name; }
|
---|
349 | }
|
---|
350 | ExcelDrawingFill _fill = null;
|
---|
351 | /// <summary>
|
---|
352 | /// Fill
|
---|
353 | /// </summary>
|
---|
354 | public ExcelDrawingFill Fill
|
---|
355 | {
|
---|
356 | get
|
---|
357 | {
|
---|
358 | if (_fill == null)
|
---|
359 | {
|
---|
360 | _fill = new ExcelDrawingFill(NameSpaceManager, TopNode, "xdr:pic/xdr:spPr");
|
---|
361 | }
|
---|
362 | return _fill;
|
---|
363 | }
|
---|
364 | }
|
---|
365 | ExcelDrawingBorder _border = null;
|
---|
366 | /// <summary>
|
---|
367 | /// Border
|
---|
368 | /// </summary>
|
---|
369 | public ExcelDrawingBorder Border
|
---|
370 | {
|
---|
371 | get
|
---|
372 | {
|
---|
373 | if (_border == null)
|
---|
374 | {
|
---|
375 | _border = new ExcelDrawingBorder(NameSpaceManager, TopNode, "xdr:pic/xdr:spPr/a:ln");
|
---|
376 | }
|
---|
377 | return _border;
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | private Uri _hyperlink = null;
|
---|
382 | /// <summary>
|
---|
383 | /// Hyperlink
|
---|
384 | /// </summary>
|
---|
385 | public Uri Hyperlink
|
---|
386 | {
|
---|
387 | get
|
---|
388 | {
|
---|
389 | return _hyperlink;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | internal override void DeleteMe()
|
---|
393 | {
|
---|
394 | _drawings._package.RemoveImage(ImageHash);
|
---|
395 | base.DeleteMe();
|
---|
396 | }
|
---|
397 | }
|
---|
398 | } |
---|