public async Task <IActionResult> Create([Bind("AmazonProductColorID,AmazonProductID,DesignURL,AmazonDesignID,Top,Left,Right,Bot")] AmazonListing amazonListing)
        {
            if (ModelState.IsValid)
            {
                _context.Add(amazonListing);
                _context.SaveChanges();

                AmazonProductColor amazonProductColor = await _context.AmazonProductColors.SingleOrDefaultAsync(e => e.AmazonProductColorID == amazonListing.AmazonProductColorID);

                AmazonDesign amazonDesign = await _context.AmazonDesigns.SingleOrDefaultAsync(e => e.AmazonDesignID == amazonListing.AmazonDesignID);

                var baseImage   = Path.Combine(_environment.WebRootPath, amazonProductColor.DesignURL);
                var opacity     = amazonProductColor.Opacity;
                var coverImage  = Path.Combine(_environment.WebRootPath, amazonDesign.DesignURL);
                var designURL   = Path.Combine(COLOR_DIR, amazonListing.AmazonListingID + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
                var outputImage = Path.Combine(_environment.WebRootPath, designURL);

                if (amazonListing.Left == null || amazonListing.Left == 0)
                {
                    amazonListing.Left = amazonProductColor.Left;
                }
                if (amazonListing.Top == null || amazonListing.Top == 0)
                {
                    amazonListing.Top = amazonProductColor.Top;
                }
                if (amazonListing.Right == null || amazonListing.Right == 0)
                {
                    amazonListing.Right = amazonProductColor.Right;
                }
                if (amazonListing.Bot == null || amazonListing.Bot == 0)
                {
                    amazonListing.Bot = amazonProductColor.Bot;
                }

                Point topLeft  = new Point((int)amazonListing.Left, (int)amazonListing.Top);
                Point topRight = new Point((int)amazonListing.Right, (int)amazonListing.Top);
                Point botLeft  = new Point((int)amazonListing.Left, (int)amazonListing.Bot);
                Point botRight = new Point((int)amazonListing.Right, (int)amazonListing.Bot);

                using (FileStream savePic = System.IO.File.OpenWrite(outputImage))
                {
                    T_generator.Services.Amazon.ImageCollider.MergeImages(
                        new BackPicture(baseImage, topLeft, topRight, botLeft, botRight),
                        new FrontPicture(coverImage, (int)(opacity * 100)))
                    //.DrawLines(new Color(1, 1, 1), 1, new Vector2[] { new Vector2(topLeft.X, 0), new Vector2(topLeft.X, 1000) })
                    //.DrawLines(new Color(1, 1, 1), 1, new Vector2[] { new Vector2(0, topLeft.Y), new Vector2(1000, topLeft.Y) })
                    .Save(savePic);
                    amazonListing.DesignURL = designURL;
                }
                // _context.Add(amazonDesign);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Edit", new { id = amazonListing.AmazonListingID }));
            }
            ViewData["AmazonProductID"]      = new SelectList(_context.AmazonProducts, "AmazonProductID", "Name");
            ViewData["AmazonProductColorID"] = new SelectList(_context.AmazonProductColors, "AmazonProductColorID", "Name");
            ViewData["AmazonDesignID"]       = new SelectList(_context.AmazonDesigns, "AmazonDesignID", "Name");
            return(View(amazonListing));
        }
        // GET: AmazonProducts/Create
        public IActionResult Create()
        {
            var amazonProductColor = new AmazonProductColor();

            ViewData["AmazonProductID"] = new SelectList(_context.AmazonProducts, "AmazonProductID", "Name");
            ViewData["AmazonColorID"]   = new SelectList(_context.AmazonColors, "AmazonColorID", "Name");
            return(View());
        }
        public async Task <IActionResult> Create([Bind("AmazonProductColorID,AmazonProductID,AmazonColorID,DesignURL,Name,Opacity,Top,Left,Right,Bot")] AmazonProductColor amazonProductColor, IFormFile DesignURL)
        {
            if (ModelState.IsValid && DesignURL != null)
            {
                _context.Add(amazonProductColor);
                _context.SaveChanges();
                var fullUploadPath = Path.Combine(_environment.WebRootPath, COLOR_DIR);
                var extension      = DesignURL.FileName.Split('.').Last();
                var filename       = amazonProductColor.AmazonProductColorID + "." + extension;
                fullUploadPath = Path.Combine(fullUploadPath, filename);

                using (var fileStream = new FileStream(fullUploadPath, FileMode.Create))
                {
                    await DesignURL.CopyToAsync(fileStream);

                    amazonProductColor.DesignURL = Path.Combine(COLOR_DIR, filename);
                }

                var thumbURL    = Path.Combine(THUMB_DIR, amazonProductColor.AmazonProductColorID + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
                var outputImage = Path.Combine(_environment.WebRootPath, thumbURL);
                var baseImage   = Path.Combine(_environment.WebRootPath, amazonProductColor.DesignURL);

                System.Drawing.Image m_baseimage = System.Drawing.Image.FromFile(baseImage);;
                System.Drawing.Pen   myPen       = new System.Drawing.Pen(System.Drawing.Color.Black);

                System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(amazonProductColor.Left, 0), new System.Drawing.Point(amazonProductColor.Left, m_baseimage.Height));
                System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(amazonProductColor.Right, 0), new System.Drawing.Point(amazonProductColor.Right, m_baseimage.Height));
                System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(0, amazonProductColor.Top), new System.Drawing.Point(m_baseimage.Width, amazonProductColor.Top));
                System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(0, amazonProductColor.Bot), new System.Drawing.Point(m_baseimage.Width, amazonProductColor.Bot));

                m_baseimage.Save(outputImage);
                m_baseimage.Dispose();

                amazonProductColor.ThumbURL = thumbURL;

                await _context.SaveChangesAsync();

                return(RedirectToAction("Edit", new { id = amazonProductColor.AmazonProductColorID }));
            }
            ViewData["AmazonProductID"] = new SelectList(_context.AmazonProducts, "AmazonProductID", "Name");
            ViewData["AmazonColorID"]   = new SelectList(_context.AmazonColors, "AmazonColorID", "Name");
            return(View(amazonProductColor));
        }
        public async Task <IActionResult> Edit(int id, [Bind("AmazonListingID,AmazonProductColorID,AmazonProductID,DesignURL,AmazonDesignID,Top,Left,Right,Bot")] AmazonListing amazonListing)
        {
            if (id != amazonListing.AmazonListingID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    AmazonProductColor amazonProductColor = await _context.AmazonProductColors.SingleOrDefaultAsync(e => e.AmazonProductColorID == amazonListing.AmazonProductColorID);

                    AmazonDesign amazonDesign = await _context.AmazonDesigns.SingleOrDefaultAsync(e => e.AmazonDesignID == amazonListing.AmazonDesignID);

                    var baseImage   = Path.Combine(_environment.WebRootPath, amazonProductColor.DesignURL);
                    var opacity     = amazonProductColor.Opacity;
                    var coverImage  = Path.Combine(_environment.WebRootPath, amazonDesign.DesignURL);
                    var designURL   = Path.Combine(COLOR_DIR, amazonListing.AmazonListingID + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
                    var outputImage = Path.Combine(_environment.WebRootPath, designURL);

                    if (amazonListing.Left == null || amazonListing.Left == 0)
                    {
                        amazonListing.Left = amazonProductColor.Left;
                    }
                    if (amazonListing.Top == null || amazonListing.Top == 0)
                    {
                        amazonListing.Top = amazonProductColor.Top;
                    }
                    if (amazonListing.Right == null || amazonListing.Right == 0)
                    {
                        amazonListing.Right = amazonProductColor.Right;
                    }
                    if (amazonListing.Bot == null || amazonListing.Bot == 0)
                    {
                        amazonListing.Bot = amazonProductColor.Bot;
                    }

                    Point topLeft  = new Point((int)amazonListing.Left, (int)amazonListing.Top);
                    Point topRight = new Point((int)amazonListing.Right, (int)amazonListing.Top);
                    Point botLeft  = new Point((int)amazonListing.Left, (int)amazonListing.Bot);
                    Point botRight = new Point((int)amazonListing.Right, (int)amazonListing.Bot);

                    using (FileStream savePic = System.IO.File.OpenWrite(outputImage))
                    {
                        System.IO.File.Delete(Path.Combine(_environment.WebRootPath, amazonListing.DesignURL));
                        T_generator.Services.Amazon.ImageCollider.MergeImages(
                            new BackPicture(baseImage, topLeft, topRight, botLeft, botRight),
                            new FrontPicture(coverImage, (int)(opacity * 100)))
                        //.DrawLines(new Color(1, 1, 1), 1, new Vector2[] { new Vector2(topLeft.X, 0), new Vector2(topLeft.X, 1000) })
                        //.DrawLines(new Color(1, 1, 1), 1, new Vector2[] { new Vector2(0, topLeft.Y), new Vector2(1000, topLeft.Y) })
                        .Save(savePic);
                        amazonListing.DesignURL = designURL;
                    }

                    //var fullUploadPath = Path.Combine(_environment.WebRootPath, COLOR_DIR);
                    //    var extension = DesignURL.FileName.Split('.').Last();
                    //    var filename = amazonListing.AmazonListingID + "." + extension;
                    //    fullUploadPath = Path.Combine(fullUploadPath, filename);
                    //    using (var fileStream = new FileStream(fullUploadPath, FileMode.Create))
                    //    {
                    //        System.IO.File.Delete(Path.Combine(_environment.WebRootPath, oldURL));
                    //        await DesignURL.CopyToAsync(fileStream);
                    //        amazonListing.DesignURL = Path.Combine(COLOR_DIR, filename);
                    //    }
                    _context.Update(amazonListing);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AmazonListingExists(amazonListing.AmazonListingID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Edit", new { id = amazonListing.AmazonListingID }));
            }

            amazonListing = await _context.AmazonListings
                            .Include(i => i.AmazonProduct)
                            .Include(i => i.AmazonProductColor)
                            .Include(i => i.AmazonDesign)
                            .SingleOrDefaultAsync(m => m.AmazonListingID == id);

            ViewData["AmazonProductID"]      = new SelectList(_context.AmazonProducts, "AmazonProductID", "Name", amazonListing.AmazonProductID);
            ViewData["AmazonProductColorID"] = new SelectList(_context.AmazonProductColors, "AmazonProductColorID", "Name", amazonListing.AmazonProductColorID);
            ViewData["AmazonDesignID"]       = new SelectList(_context.AmazonDesigns, "AmazonDesignID", "Name", amazonListing.AmazonDesignID);

            return(View(amazonListing));
        }
        public async Task <IActionResult> Edit(int id, [Bind("AmazonProductColorID,AmazonProductID,AmazonColorID,DesignURL,Name,Opacity,Top,Left,Right,Bot")] AmazonProductColor amazonProductColor, IFormFile DesignURL, string oldURL, string oldThumb)
        {
            if (id != amazonProductColor.AmazonProductColorID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (DesignURL != null)
                    {
                        var fullUploadPath = Path.Combine(_environment.WebRootPath, COLOR_DIR);
                        var extension      = DesignURL.FileName.Split('.').Last();
                        var filename       = amazonProductColor.AmazonProductColorID + "." + extension;
                        fullUploadPath = Path.Combine(fullUploadPath, filename);
                        using (var fileStream = new FileStream(fullUploadPath, FileMode.Create))
                        {
                            System.IO.File.Delete(Path.Combine(_environment.WebRootPath, oldURL));
                            await DesignURL.CopyToAsync(fileStream);

                            amazonProductColor.DesignURL = Path.Combine(COLOR_DIR, filename);
                        }
                    }
                    else
                    {
                        amazonProductColor.DesignURL = oldURL;
                    }

                    var thumbURL    = Path.Combine(THUMB_DIR, amazonProductColor.AmazonProductColorID + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
                    var outputImage = Path.Combine(_environment.WebRootPath, thumbURL);
                    var baseImage   = Path.Combine(_environment.WebRootPath, amazonProductColor.DesignURL);

                    System.IO.File.Delete(Path.Combine(_environment.WebRootPath, oldThumb));

                    System.Drawing.Image m_baseimage = System.Drawing.Image.FromFile(baseImage);
                    System.Drawing.Pen   myPen       = new System.Drawing.Pen(System.Drawing.Color.Black);

                    System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(amazonProductColor.Left, 0), new System.Drawing.Point(amazonProductColor.Left, m_baseimage.Height));
                    System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(amazonProductColor.Right, 0), new System.Drawing.Point(amazonProductColor.Right, m_baseimage.Height));
                    System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(0, amazonProductColor.Top), new System.Drawing.Point(m_baseimage.Width, amazonProductColor.Top));
                    System.Drawing.Graphics.FromImage(m_baseimage).DrawLine(myPen, new System.Drawing.Point(0, amazonProductColor.Bot), new System.Drawing.Point(m_baseimage.Width, amazonProductColor.Bot));

                    m_baseimage.Save(outputImage);
                    m_baseimage.Dispose();

                    amazonProductColor.ThumbURL = thumbURL;

                    _context.Update(amazonProductColor);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AmazonProductColorExists(amazonProductColor.AmazonProductColorID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Edit", new { id = amazonProductColor.AmazonProductColorID }));
            }

            amazonProductColor = await _context.AmazonProductColors
                                 .Include(i => i.AmazonProduct)
                                 .Include(i => i.AmazonColor)
                                 .SingleOrDefaultAsync(m => m.AmazonProductColorID == id);

            ViewData["AmazonProductID"] = new SelectList(_context.AmazonProducts, "AmazonProductID", "Name", amazonProductColor.AmazonProductID);
            ViewData["AmazonColorID"]   = new SelectList(_context.AmazonColors, "AmazonColorID", "Name", amazonProductColor.AmazonColorID);
            return(View(amazonProductColor));
        }