Beispiel #1
0
        internal ExcelPicture(ExcelDrawings drawings, XmlNode node, ExcelGroupShape shape = null) :
            base(drawings, node, "xdr:pic", "xdr:nvPicPr/xdr:cNvPr", shape)
        {
            XmlNode picNode = node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip", drawings.NameSpaceManager);

            if (picNode != null)
            {
                IPictureContainer container = this;
                container.RelPic = drawings.Part.GetRelationship(picNode.Attributes["r:embed"].Value);
                container.UriPic = UriHelper.ResolvePartUri(drawings.UriDrawing, container.RelPic.TargetUri);

                Part = drawings.Part.Package.GetPart(container.UriPic);
                FileInfo f = new FileInfo(container.UriPic.OriginalString);
                ContentType = PictureStore.GetContentType(f.Extension);
                _image      = Image.FromStream(Part.GetStream());

#if (Core)
                byte[] iby = ImageCompat.GetImageAsByteArray(_image);
#else
                ImageConverter ic  = new ImageConverter();
                var            iby = (byte[])ic.ConvertTo(_image, typeof(byte[]));
#endif
                var ii = _drawings._package.PictureStore.LoadImage(iby, container.UriPic, Part);
                container.ImageHash = ii.Hash;
            }
        }
Beispiel #2
0
        private void SaveImageToPackage(ePictureType type, byte[] img)
        {
            var package = _drawings.Worksheet._package.ZipPackage;

            ContentType = PictureStore.GetContentType(type.ToString());
            IPictureContainer container = this;

            container.UriPic = GetNewUri(package, "/xl/media/image{0}." + type.ToString());
            var    store = _drawings._package.PictureStore;
            var    ii    = store.AddImage(img, container.UriPic, ContentType);
            string relId;

            if (!_drawings._hashes.ContainsKey(ii.Hash))
            {
                Part             = ii.Part;
                container.RelPic = _drawings.Part.CreateRelationship(UriHelper.GetRelativeUri(_drawings.UriDrawing, ii.Uri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
                relId            = container.RelPic.Id;
                _drawings._hashes.Add(ii.Hash, new HashInfo(relId));
                AddNewPicture(img, relId);
            }
            else
            {
                relId = _drawings._hashes[ii.Hash].RelId;
                var rel = _drawings.Part.GetRelationship(relId);
                container.UriPic = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
            }
            container.ImageHash = ii.Hash;
            _image = Image.FromStream(new MemoryStream(img));
            SetPosDefaults(_image);

            //Create relationship
            TopNode.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relId;
            package.Flush();
        }
Beispiel #3
0
        internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile, Uri hyperlink) :
            base(drawings, node, "xdr:pic", "xdr:nvPicPr/xdr:cNvPr")
        {
            CreatePicNode(node);

            //Changed to stream 2/4-13 (issue 14834). Thnx SClause
            var package = drawings.Worksheet._package.Package;

            ContentType = PictureStore.GetContentType(imageFile.Extension);
            var imagestream = new FileStream(imageFile.FullName, FileMode.Open, FileAccess.Read);

            _image    = Image.FromStream(imagestream);
            Hyperlink = hyperlink;

#if (Core)
            var img = ImageCompat.GetImageAsByteArray(_image);
#else
            ImageConverter ic  = new ImageConverter();
            var            img = (byte[])ic.ConvertTo(_image, typeof(byte[]));
#endif
            IPictureContainer container = this;
            imagestream.Close();
            container.UriPic = GetNewUri(package, "/xl/media/{0}" + imageFile.Name);
            var    store = _drawings._package.PictureStore;
            var    ii    = store.AddImage(img, container.UriPic, ContentType);
            string relId;
            if (!_drawings._hashes.ContainsKey(ii.Hash))
            {
                Part             = ii.Part;
                container.RelPic = drawings.Part.CreateRelationship(UriHelper.GetRelativeUri(drawings.UriDrawing, ii.Uri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image");
                relId            = container.RelPic.Id;
                _drawings._hashes.Add(ii.Hash, new HashInfo(relId));
                AddNewPicture(img, relId);
            }
            else
            {
                relId = _drawings._hashes[ii.Hash].RelId;
                var rel = _drawings.Part.GetRelationship(relId);
                container.UriPic = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
            }
            container.ImageHash = ii.Hash;
            _height             = Image.Height;
            _width = Image.Width;
            SetPosDefaults(Image);
            //Create relationship
            node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relId;
            package.Flush();
        }
Beispiel #4
0
        internal ExcelPicture(ExcelDrawings drawings, XmlNode node, ExcelGroupShape shape = null) :
            base(drawings, node, "xdr:pic", "xdr:nvPicPr/xdr:cNvPr", shape)
        {
            XmlNode picNode = node.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip", drawings.NameSpaceManager);

            if (picNode != null)
            {
                IPictureContainer container = this;
                container.RelPic = drawings.Part.GetRelationship(picNode.Attributes["embed", ExcelPackage.schemaRelationships].Value);
                container.UriPic = UriHelper.ResolvePartUri(drawings.UriDrawing, container.RelPic.TargetUri);

                Part = drawings.Part.Package.GetPart(container.UriPic);
                var extension = Path.GetExtension(container.UriPic.OriginalString);
                ContentType = PictureStore.GetContentType(extension);

#if (Core)
                try
                {
                    _image = Image.FromStream(Part.GetStream());
                }
                catch
                {
                    if (extension.ToLower() == ".emf" || extension.ToLower() == ".wmf") //Not supported in linux environments, so we ignore them and set image to null.
                    {
                        _image = null;
                        return;
                    }
                    else
                    {
                        throw;
                    }
                }
                byte[] iby = ImageCompat.GetImageAsByteArray(_image);
#else
                _image = Image.FromStream(Part.GetStream());
                ImageConverter ic  = new ImageConverter();
                var            iby = (byte[])ic.ConvertTo(_image, typeof(byte[]));
#endif
                var ii = _drawings._package.PictureStore.LoadImage(iby, container.UriPic, Part);
                container.ImageHash = ii.Hash;
            }
        }
Beispiel #5
0
        internal static Image GetPicture(string relId, IPictureContainer container, out string contentType)
        {
            ZipPackagePart part;

            //if (container.Drawing is ExcelChart chart)
            //{
            //    container.RelPic = chart.Part.GetRelationship(relId);
            //    container.UriPic = UriHelper.ResolvePartUri(chart.UriChart, container.RelPic.TargetUri);
            //    part = chart.Part.Package.GetPart(container.UriPic);
            //}
            //else
            //{
            container.RelPic = container.RelationDocument.RelatedPart.GetRelationship(relId);
            container.UriPic = UriHelper.ResolvePartUri(container.RelationDocument.RelatedUri, container.RelPic.TargetUri);
            part             = container.RelationDocument.RelatedPart.Package.GetPart(container.UriPic);
            //}

            FileInfo f = new FileInfo(container.UriPic.OriginalString);

            contentType = PictureStore.GetContentType(f.Extension);
            return(Image.FromStream(part.GetStream()));
        }