Пример #1
0
        /// <summary>
        /// This mehtod generates a composite image that includes all layers so we have a full product thumbnail
        /// </summary>
        /// <param name="size"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static byte[] Thumbnail(string size, string d)
        {
            Size thumbSize = new Size(107, 67);

            if (size == "medium")
            {
                thumbSize = new Size(160, 100);
            }
            else if (size == "large")
            {
                thumbSize = new Size(320, 200);
            }

            using (AtalaImage image = new AtalaImage(thumbSize.Width, thumbSize.Height, PixelFormat.Pixel32bppBgra, Color.Transparent))
            {
                string product = string.Empty;
                string view    = "default";

                #region Build layers from input json
                //expected format
                //[["productName","Bed"],["Size","Small"],["Top","NavyBlue"],["Side","NavyBlue"],["Piping","Pink"],["text",""],["banner","Oval"],["font","BigDog.TTF"],["textColor","000000"],["image1",""],["image2",""],["image3",""]]
                var      props             = JsonHelper.Decode(d);
                string[] skippedProperties = { "Size", "text", "banner", "font", "textColor", "image1", "image2", "image3" };
                var      layers            = new List <KeyValuePair <string, string> >();
                var      allProperties     = new List <KeyValuePair <string, string> >();
                foreach (var prop in props)
                {
                    string key   = prop[0];
                    string value = prop[1];
                    allProperties.Add(new KeyValuePair <string, string>(key, value));
                    if (skippedProperties.Contains(key))
                    {
                        continue;
                    }
                    if (key == "productName")
                    {
                        product = value;
                    }
                    else
                    {
                        layers.Add(new KeyValuePair <string, string>(key, value));
                    }
                }
                #endregion

                #region Draw Layers
                var g = image.GetGraphics();
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                #region Draw Base
                string basePath = HttpContext.Current.Server.MapPath(string.Format("~/AppContent/ProductImages/{0}/{1}/base.png", product, view));
                if (File.Exists(basePath))
                {
                    using (AtalaImage uploadImage = new AtalaImage(basePath))
                    {
                        g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                    }
                }
                #endregion
                foreach (var layer in layers)
                {
                    string imgPath = HttpContext.Current.Server.MapPath(string.Format("~/AppContent/ProductImages/{0}/{1}/{2}-{3}.png", product, view, layer.Key, layer.Value));
                    using (AtalaImage uploadImage = new AtalaImage(imgPath))
                    {
                        g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                    }
                }
                #endregion

                #region Draw Text
                //Get product properties
                //string[] skippedProperties = { "Size", "text", "banner", "font", "textColor", "image1", "image2", "image3" };
                var textProp = allProperties.Where(kvp => kvp.Key == "text").FirstOrDefault();
                if (!string.IsNullOrEmpty(textProp.Value))
                {
                    dynamic productSchema = ProductSchemaProvider.GetProductSchema(product);
                    var     defaultView   = productSchema.views[0];
                    var     textLayer     = ((IEnumerable)defaultView.layers).Cast <dynamic>().Where(l => l.displayType == "text").FirstOrDefault();
                    if (textLayer != null)
                    {
                        JToken defaults = (JToken)textLayer.defaults;

                        var colorProp  = allProperties.Where(kvp => kvp.Key == "textColor").FirstOrDefault();
                        var fontProp   = allProperties.Where(kvp => kvp.Key == "font").FirstOrDefault();
                        var bannerProp = allProperties.Where(kvp => kvp.Key == "banner").FirstOrDefault();
                        var image1Prop = allProperties.Where(kvp => kvp.Key == "image1").FirstOrDefault();

                        var placeholderImageLocation = "";

                        // TODO: Clean this up
                        if (image1Prop.Value != null)
                        {
                            if (product == "Frisbee")
                            {
                                placeholderImageLocation = "placeholder";
                            }
                            else
                            {
                                placeholderImageLocation = "placeholder2";
                            }
                        }

                        byte[] textImage = TextImage(defaults.GetValue <string>("drawMode", ""),
                                                     textProp.Value,
                                                     colorProp.Value != null ? colorProp.Value : defaults.GetValue <string>("textColor"),
                                                     defaults.GetValue <int>("Size", 40),
                                                     fontProp.Value != null ? fontProp.Value : defaults.GetValue <string>("font"),
                                                     defaults.GetValue <int>("x", 0),
                                                     defaults.GetValue <int>("y", 0),
                                                     defaults.GetValue <int>("h", 0),
                                                     defaults.GetValue <int>("w", 0),
                                                     defaults.GetValue <int>("r1", 0),
                                                     defaults.GetValue <int>("s2", 0),
                                                     defaults.GetValue <string>("align", ""),
                                                     defaults.GetValue <int>("x2", 320),
                                                     defaults.GetValue <int>("y2", 110),
                                                     defaults.GetValue <int>("h2", 260),
                                                     defaults.GetValue <int>("w2", 220),
                                                     defaults.GetValue <int>("r2", 0),
                                                     defaults.GetValue <int>("s2", 0),
                                                     bannerProp.Value != null ? bannerProp.Value : defaults.GetValue <string>("banner"),
                                                     image1Prop.Value != null ? placeholderImageLocation : string.Empty);
                        using (var textImageMemory = new MemoryStream(textImage))
                        {
                            using (AtalaImage uploadImage = new AtalaImage(textImageMemory))
                            {
                                g.DrawImage(uploadImage.ToBitmap(), new RectangleF(0, 0, thumbSize.Width, thumbSize.Height));
                            }
                        }
                    }
                }
                #endregion

                #region Save Image
                byte[] data = null;
                using (MemoryStream m = new MemoryStream())
                {
                    PngEncoder encoder = new PngEncoder();
                    encoder.Save(m, image, null);
                    data = m.ToArray();
                    return(data);
                }
                #endregion
            }
        }