public ActionResult Create(GalleryCreateViewModel galleryCreate)
 {
     var image = galleryCreate.Image;
     var imageSave = WorkImage.CreateImage(galleryCreate.Image, 800, 600);
     if(imageSave!=null)
     {
         string path = Server.MapPath("~/Upload/ImageGallery/");
         string fileName = Guid.NewGuid().ToString() + ".jpg";
         imageSave.Save(path + fileName, ImageFormat.Jpeg);
     }
     return View();
 }
Exemple #2
0
        public ActionResult Create(HttpPostedFileBase image)
        {
            Bitmap imageSave = WorkImage.CreateImage(image, 800, 600);

            if (imageSave != null)
            {
                string path     = Server.MapPath(ConfigurationManager.AppSettings["ImagePathGallery"]);
                string fileName = Guid.NewGuid().ToString() + ".jpg";
                imageSave.Save(path + fileName, ImageFormat.Jpeg);
            }

            return(View());
        }
 public WorkImage UpdateWorkImage(WorkImage workImage)
 {
     try
     {
         workImage.LastEditedDateTime = DateTime.UtcNow;
         DbContext.WorkImages.Update(workImage);
         DbContext.SaveChanges();
         return(workImage);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(null);
     }
 }
Exemple #4
0
 private void SaveImages(HttpPostedFileBase[] Images, int postId, string path, int height, int width, string miniPath, int miniHeight, int miniWidth)
 {
     foreach (var img in Images)
     {
         string name     = Path.GetFileName(img.FileName);
         string miniName = Path.GetFileName(img.FileName);
         Bitmap imgs     = WorkImage.CreateImage(img, width, height);
         Bitmap miniImg  = WorkImage.CreateImage(img, miniHeight, miniWidth);
         int    i        = 1;
         if (imgs != null)
         {
             string[] dirs = Directory.GetFiles(path, name);
             while (dirs.Length != 0)
             {
                 name = "copy-" + i + "-" + Path.GetFileName(img.FileName);
                 i++;
                 dirs = Directory.GetFiles(path, name);
             }
             string filename = path + name;
             imgs.Save(filename, ImageFormat.Jpeg);
         }
         int h = 1;
         if (miniImg != null)
         {
             string[] miniDirs = Directory.GetFiles(miniPath, miniName);
             while (miniDirs.Length != 0)
             {
                 miniName = "copy-" + h + "-" + Path.GetFileName(img.FileName);
                 h++;
                 miniDirs = Directory.GetFiles(miniPath, miniName);
             }
             string filename = miniPath + miniName;
             miniImg.Save(filename, ImageFormat.Jpeg);
         }
         SaveImage(name, miniName, postId);
     }
 }
Exemple #5
0
        /// <summary>
        ///     Draws the specified word cloud given list of words and frequecies
        /// </summary>
        /// <param name="words">List of words ordered by occurance.</param>
        /// <param name="freqs">List of frequecies.</param>
        /// <param name="bgcolor">Backgroud color of the output image</param>
        /// <param name="img">Backgroud image of the output image</param>
        /// <returns>Image of word cloud.</returns>
        /// <exception cref="System.ArgumentException">
        ///     Arguments null.
        ///     or
        ///     Must have the same number of words as frequencies.
        /// </exception>
        private Image Draw(IList <string> words, IList <int> freqs, Color bgcolor, Image img)
        {
#if DEBUG
            ShowIntegralImgStepDraw(Map.IntegralImageToBitmap());
            _drawWaitHandle.Reset();
#endif
            var fontSize = MaxFontSize;
            if (words == null || freqs == null)
            {
                throw new ArgumentException("Arguments null.");
            }
            if (words.Count != freqs.Count)
            {
                throw new ArgumentException("Must have the same number of words as frequencies.");
            }

            Bitmap result;
            if (img == null)
            {
                result = new Bitmap(WorkImage.Width, WorkImage.Height);
            }
            else
            {
                if (img.Size != WorkImage.Bitmap.Size)
                {
                    throw new Exception("The backgroud img should be with the same size with the mask");
                }
                result = new Bitmap(img);
            }

            using (var gworking = Graphics.FromImage(WorkImage.Bitmap))
                using (var gresult = Graphics.FromImage(result))
                {
                    if (img == null)
                    {
                        gresult.Clear(bgcolor);
                    }
                    gresult.TextRenderingHint  = TextRenderingHint.AntiAlias;
                    gworking.TextRenderingHint = TextRenderingHint.AntiAlias;
                    var lastProgress = 0.0d;
                    for (var i = 0; i < words.Count; ++i)
                    {
                        var progress = (double)i / words.Count;
                        if (progress - lastProgress > 0.01)
                        {
                            ShowProgress(progress);
                            lastProgress = progress;
                        }
                        if (!UseRank)
                        {
                            fontSize = (float)Math.Min(fontSize, 100 * Math.Log10(freqs[i] + 100));
                        }

                        var format = new StringFormat();
                        if (AllowVertical)
                        {
                            if (Random.Next(0, 2) == 1)
                            {
                                format.FormatFlags = StringFormatFlags.DirectionVertical;
                            }
                        }

                        Point p;
                        bool  foundPosition;
                        Font  font;
                        Debug.WriteLine("Word: " + words[i]);
                        do
                        {
                            font = new Font(Fontname, fontSize, GraphicsUnit.Pixel);
                            var size = gworking.MeasureString(words[i], font, new PointF(0, 0), format);
                            Debug.WriteLine("Search with font size: " + fontSize);
                            foundPosition = Map.GetRandomUnoccupiedPosition((int)size.Width, (int)size.Height, out p);
                            if (!foundPosition)
                            {
                                fontSize -= FontStep;
                            }
                        } while (fontSize > 0 && !foundPosition);
                        Debug.WriteLine("Found pos: " + p);
                        if (fontSize <= 0)
                        {
                            break;
                        }
                        gworking.DrawString(words[i], font, new SolidBrush(FontColor), p.X, p.Y, format);
                        gresult.DrawString(words[i], font, new SolidBrush(FontColor), p.X, p.Y, format);
                        Map.Update(WorkImage, p.X, p.Y);
#if DEBUG
                        if (StepDrawMode)
                        {
                            ShowResultStepDraw(new Bitmap(WorkImage.Bitmap));
                            ShowIntegralImgStepDraw(Map.IntegralImageToBitmap());
                            _drawWaitHandle.WaitOne();
                        }
#endif
                    }
                }
            WorkImage.Dispose();
            return(result);
        }
Exemple #6
0
        protected override void WebResponseHandle(string response, ICatchItem catchItem)
        {
            try
            {
                string html      = response;
                var    doc       = new HtmlDocument();
                string coserName = string.Empty;
                string title     = string.Empty;
                string fileName  = string.Empty;

                doc.LoadHtml(html);

                IPage page = RuleConfig.PageRule.GetRule(PageType.Images);

                coserName = page.GetSingleNodeValue(doc.DocumentNode, "CoserName");

                title = page.GetSingleNodeValue(doc.DocumentNode, "Title");

                if (!string.IsNullOrEmpty(title))
                {
                    foreach (char rInvalidChar in System.IO.Path.GetInvalidPathChars())
                    {
                        title = title.Replace(rInvalidChar.ToString(), string.Empty);
                    }
                    string errChar = "\\/:*?";
                    foreach (char rInvalidChar in errChar)
                    {
                        title = title.Replace(rInvalidChar.ToString(), string.Empty);
                    }
                }

                var imgNodes = page.GetNodes(doc.DocumentNode, "WorkImage");
                if (imgNodes != null)
                {
                    if (catchItem.Extend != null && catchItem.Extend is Work)
                    {
                        Work   work        = (Work)catchItem.Extend;
                        string workAddress = catchItem.Uri.AbsoluteUri;

                        imgNodes.ToList()
                        .ForEach(x =>
                        {
                            string address = page.GetSingleNodeValue(x, "WorkImage.Src");

                            // 删除尾部限定大小
                            var regex = new Regex(@"((http|https)://)(([a-zA-Z0-9\._-]+)/)+(w\d+)");
                            if (regex.IsMatch(address))
                            {
                                address = address.Substring(0, address.LastIndexOf('/'));
                            }

                            logger.InfoFormat("添加图片:[Address:{0}, CoserName:{1}, WorkAddress:{2}]", address, coserName, workAddress);

                            WorkImage workImage = new WorkImage()
                            {
                                Work = work, WorkId = work.Id, Address = address
                            };
                            workImageHelper.Add(workImage);
                            // 事件推送图片添加成功
                            syncWorkImageAdd?.Invoke(workImage);
                        });
                        // 完成后,从作品抓取列表中清除
                        logger.InfoFormat("完成抓取: {0}", catchItem.Uri.AbsoluteUri);
                        // 事件推送作品图片已抓取
                        work.IsCatchImage = true;
                        work.Title        = title;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("WorkImageCatcher WebResponseHandle:{0}", ex);
            }
        }