Beispiel #1
0
        private CropRectangle GetDefaultCrop(XmlElement widthNode, XmlElement heightNode, XmlElement selectedCrop)
        {
            // Work out the correct crop positon if there is no crop provided
            CropRectangle crop = new CropRectangle();

            decimal width  = decimal.Parse(widthNode.InnerText);
            decimal height = decimal.Parse(heightNode.InnerText);

            decimal targetWidth  = decimal.Parse(selectedCrop.SelectSingleNode(@"jll_width", _nsManagerDataset).InnerText);
            decimal targetHeight = decimal.Parse(selectedCrop.SelectSingleNode(@"jll_height", _nsManagerDataset).InnerText);

            crop.width  = targetWidth;
            crop.height = targetHeight;
            // Normalise
            decimal sourceAspectRatio = width / height;
            decimal targetAspectRatio = targetWidth / targetHeight;

            height = 1;
            width  = sourceAspectRatio;

            targetHeight = 1;
            targetWidth  = targetAspectRatio;

            // Centre the crop
            decimal widthPercentage  = targetWidth / width;
            decimal heightPercentage = targetHeight / height;

            crop.top    = ((1 - heightPercentage) / 2) * 100;
            crop.bottom = crop.top;
            crop.left   = ((1 - widthPercentage) / 2) * 100;
            crop.right  = crop.left;
            return(crop);
        }
Beispiel #2
0
        private static CropRectangle GetUserDefinedCropRectangle(string cropParameter, XmlElement cropsNode)
        {
            if (cropsNode == null)
            {
                return(null);
            }

            CropRectangle crop            = null;
            string        pictureCropData = cropsNode.InnerText;

            // Get the aspect ratio using the name from the available crops
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(pictureCropData)))
            {
                // Desieralise the aspect ratios
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dictionary <string, CropRectangle>), new DataContractJsonSerializerSettings {
                    UseSimpleDictionaryFormat = true
                });
                var crops = (Dictionary <string, CropRectangle>)ser.ReadObject(ms);

                // Select the correct aspect ratio
                foreach (string key in crops.Keys)
                {
                    if (key == cropParameter)
                    {
                        crop = crops[key];
                        break;
                    }
                }
            }
            return(crop);
        }
Beispiel #3
0
        /// <summary>
        /// Image Expression consists of [xpath],[aspect ratio name]
        /// The aspect ratio is optional
        /// </summary>
        /// <param name="datasets"></param>
        /// <param name="xpath"></param>
        /// <param name="annotationid"></param>
        /// <returns></returns>
        private PackagePart GetAnnotationImage(XmlDocument datasets, string xpath, out string annotationid, out CropRectangle crop)
        {
            // Extract the crop
            crop = null;
            string[] parts = xpath.Split(',');
            xpath = parts[0];
            string cropParameter = parts.Length > 1 ? parts[1] : null;

            string expr = ExpandXPath(xpath);

            if (expr.EndsWith("/"))
            {
                expr += ".";
            }
            Trace("GetImageNode:{0}", expr);

            XmlElement noteNode = (XmlElement)datasets.SelectSingleNode(expr, _nsManagerDataset);

            if (noteNode == null)
            {
                annotationid = Guid.Empty.ToString();
                return(null);
            }
            XmlElement mimeTypeNode     = (XmlElement)noteNode.SelectSingleNode(@"mimetype", _nsManagerDataset);
            XmlElement bodyNode         = (XmlElement)noteNode.SelectSingleNode(@"documentbody", _nsManagerDataset);
            XmlElement urlNode          = (XmlElement)noteNode.SelectSingleNode(@"url", _nsManagerDataset);
            XmlElement annotationidNode = (XmlElement)noteNode.SelectSingleNode(@"annotationid", _nsManagerDataset);
            XmlElement fileNameNode     = (XmlElement)noteNode.SelectSingleNode(@"filename", _nsManagerDataset);
            XmlElement widthNode        = (XmlElement)noteNode.SelectSingleNode(@"width", _nsManagerDataset);
            XmlElement heightNode       = (XmlElement)noteNode.SelectSingleNode(@"height", _nsManagerDataset);
            XmlElement cropsNode        = (XmlElement)noteNode.SelectSingleNode(@"crops", _nsManagerDataset);

            if (mimeTypeNode == null)
            {
                throw new Exception("Cannot find the mimetype node for image");
            }
            if (bodyNode == null && urlNode == null)
            {
                throw new Exception("Cannot find the body or url node for image");
            }
            if (annotationidNode == null)
            {
                throw new Exception("Cannot find the annotationid node for image");
            }
            if (fileNameNode == null)
            {
                throw new Exception("Cannot find the filename node for image");
            }

            // Is there a crop selected?
            if (cropParameter != null)
            {
                XmlElement selectedCrop = (XmlElement)datasets.SelectSingleNode("//datasets/AspectRatios/Entity[jll_name='" + cropParameter + "']", _nsManagerDataset);
                if (selectedCrop != null)
                {
                    crop = GetUserDefinedCropRectangle(cropParameter, cropsNode);

                    if (crop == null && widthNode != null && heightNode != null)
                    {
                        crop = GetDefaultCrop(widthNode, heightNode, selectedCrop);
                    }
                }
            }

            // Determine if it's already added as a part - if not add it
            annotationid = annotationidNode.InnerText;
            string fileName           = GetImageFileName(new Guid(annotationid), fileNameNode.InnerText);
            string imageData          = null;
            bool   imageDataCollected = false;

            if (bodyNode != null)
            {
                imageData = bodyNode.InnerText;
                //imageDataCollected = true;
            }
            else if (DownloadAndEmbedImageUrls)
            {
                // Download the image
                try
                {
                    using (WebClient webClient = new WebClient())
                    {
                        // TODO: optimise for quality and set max width /upload/q_auto/if_w_gt_1500,w_1500/v14
                        // TODO: use cloudinary cropping rather than PPT upload/x_0.0,y_0.0,w_1.0,h_1.0,c_crop
                        var imageBytes = webClient.DownloadData(urlNode.InnerText);
                        imageData          = Convert.ToBase64String(imageBytes);
                        imageDataCollected = true;
                    }
                }
                catch (Exception e)
                {
                }
            }
            if (!imageDataCollected)
            {
                string mimetype  = mimeTypeNode.InnerText;
                var    imagePart = AddImage(new Guid(annotationid), imageData, mimetype, fileName);
                return(imagePart);
            }
            else if (urlNode != null)
            {
                // We are embedding a url - make the annotiationid the url
                annotationid = urlNode.InnerText;
                return(null);
            }
            throw new Exception("Cannot find image url/data");
        }