//private string GetPictureRelID(byte[] img) //{ // foreach (var hash in _drawings._hashes) // { // if (checkImg.Comparer(img)) // { // return checkImg.relID; // } // } // return ""; //} private string SavePicture(Image image) { ImageConverter ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(image, typeof(byte[])); var ii = _drawings._package.AddImage(img); if (_drawings._hashes.ContainsKey(ii.Hash)) { var relID = _drawings._hashes[ii.Hash]; var rel = _drawings.Part.GetRelationship(relID); UriPic = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri); return(relID); } else { UriPic = ii.Uri; } //Set the Image and save it to the package. PackageRelationship picRelation = _drawings.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(_drawings.UriDrawing, UriPic), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image", PackagePartForMono.NextRelationshipID); //AddNewPicture(img, picRelation.Id); _drawings._hashes.Add(ii.Hash, picRelation.Id); return(picRelation.Id); }
internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile) : base(drawings, node, "xdr:pic/xdr:nvPicPr/xdr:cNvPr/@name") { XmlElement picNode = node.OwnerDocument.CreateElement("xdr", "pic", ExcelPackage.schemaSheetDrawings); node.InsertAfter(picNode, node.SelectSingleNode("xdr:to", NameSpaceManager)); picNode.InnerXml = PicStartXml(); node.InsertAfter(node.OwnerDocument.CreateElement("xdr", "clientData", ExcelPackage.schemaSheetDrawings), picNode); Package package = drawings.Worksheet._package.Package; ContentType = GetContentType(imageFile.Extension); _image = Image.FromFile(imageFile.FullName); ImageConverter ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(_image, typeof(byte[])); UriPic = GetNewUri(package, "/xl/media/{0}" + imageFile.Name); var ii = _drawings._package.AddImage(img, UriPic, ContentType); //string relID = GetPictureRelID(img); //if (relID == "") string relID; if (!drawings._hashes.ContainsKey(ii.Hash)) { //UriPic = GetNewUri(package, "/xl/media/image{0}" + imageFile.Extension); //Part = package.CreatePart(UriPic, ContentType, CompressionOption.NotCompressed); ////Save the picture to package. //byte[] file = File.ReadAllBytes(imageFile.FullName); //var strm = Part.GetStream(FileMode.Create, FileAccess.Write); //strm.Write(file, 0, file.Length); Part = ii.Part; PackageRelationship picRelation = drawings.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(drawings.UriDrawing, ii.Uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image", PackagePartForMono.NextRelationshipID); relID = picRelation.Id; _drawings._hashes.Add(ii.Hash, relID); AddNewPicture(img, relID); } else { relID = drawings._hashes[ii.Hash]; var rel = _drawings.Part.GetRelationship(relID); UriPic = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri); } SetPosDefaults(Image); //Create relationship node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relID; package.Flush(); }
private XmlElement CreateDrawingXml() { if (DrawingXml.OuterXml == "") { DrawingXml.LoadXml(string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><xdr:wsDr xmlns:xdr=\"{0}\" xmlns:a=\"{1}\" />", ExcelPackage.schemaSheetDrawings, ExcelPackage.schemaDrawings)); _uriDrawing = new Uri(string.Format("/xl/drawings/drawing{0}.xml", Worksheet.SheetID), UriKind.Relative); Package package = Worksheet._package.Package; _part = package.CreatePart(_uriDrawing, "application/vnd.openxmlformats-officedocument.drawing+xml", _package.Compression); StreamWriter streamChart = new StreamWriter(_part.GetStream(FileMode.Create, FileAccess.Write)); DrawingXml.Save(streamChart); streamChart.Close(); package.Flush(); PackageRelationship drawRelation = Worksheet.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(Worksheet.WorksheetUri, _uriDrawing), TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing", PackagePartForMono.NextRelationshipID); XmlElement e = Worksheet.WorksheetXml.CreateElement("drawing", ExcelPackage.schemaMain); e.SetAttribute("id", ExcelPackage.schemaRelationships, drawRelation.Id); Worksheet.WorksheetXml.DocumentElement.AppendChild(e); package.Flush(); } XmlNode colNode = _drawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager); XmlElement drawNode = _drawingsXml.CreateElement("xdr", "twoCellAnchor", ExcelPackage.schemaSheetDrawings); colNode.AppendChild(drawNode); //Add from position Element; XmlElement fromNode = _drawingsXml.CreateElement("xdr", "from", ExcelPackage.schemaSheetDrawings); drawNode.AppendChild(fromNode); fromNode.InnerXml = "<xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff>"; //Add to position Element; XmlElement toNode = _drawingsXml.CreateElement("xdr", "to", ExcelPackage.schemaSheetDrawings); drawNode.AppendChild(toNode); toNode.InnerXml = "<xdr:col>10</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>10</xdr:row><xdr:rowOff>0</xdr:rowOff>"; return(drawNode); }