//[AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveSize(string Id) { string pickSize = Id; ViewData["Size"] = pickSize; Session["Size"] = pickSize; switch (pickSize) { case "Small": ViewData["k"]=Session["ks"]; break; case "Large": ViewData["k"]=Session["k"]; break; case "Normal": ViewData["k"]=Session["km"]; break; } Bitmap bt = (Bitmap)Session["Image"]; try { System.IO.File.Delete((string)Session["File"]); } catch { } int M; M = (int)ViewData["k"]; int X1 = bt.Width - bt.Width % M; int Y1 = bt.Height - bt.Height % M; int X = (bt.Width - bt.Width % M) / M; int Y = (bt.Height - bt.Height % M) / M; MyProject.ImageWorker.Picture picture = new MyProject.ImageWorker.Picture(bt.Height - bt.Height % M, bt.Width - bt.Width % M); /*generate table*/ //Thread drawTable = new Thread(DrawTable); //drawTable.Start(); //Bitmap bt2 = new Bitmap(X, Y); /*Smooth picture*/ int R, G, B; imagePix = new Color[X, Y]; Color c = new Color(); string grid = string.Empty; Dictionary<Color, List<ImageWorker.Point>> imageDict = new Dictionary<Color, List<ImageWorker.Point>>(); for (int i = 0; i < Y1; i = i + M) { for (int j = 0; j < X1; j = j + M) { R = 0; G = 0; B = 0; for (int i1 = 0; i1 < M; i1++) for (int j1 = 0; j1 < M; j1++) { c = bt.GetPixel(j1 + j, i1 + i); R = c.R + R; G = c.G + G; B = c.B + B; } Color newcolor = Color.FromArgb(R / M / M, G / M / M, B / M / M); List<Color> check=new List<Color>(); int ii = i / M; int jj = j / M; //if (i >= 1) //{ // check.Add(imagePix[jj, ii - 1]); //} //if (j >= 1) //{ // check.Add(imagePix[jj-1, ii]); //} //if (j >= 1&& i>=1) //{ // check.Add(imagePix[jj - 1, ii-1]); //} //List<Color> res = new List<Color>(); //res = check.FindAll(x => ImageWorker.CountDifferenceBetweenColors(newcolor, x) <= 3); //if (res.Count==0) //{ imagePix[jj,ii] = newcolor; //} //else //{ // imagePix[jj, ii] = res.OrderBy(x => ImageWorker.CountDifferenceBetweenColors(newcolor, x)).First(); //} if (!imageDict.ContainsKey(imagePix[jj, ii])) { List<Color> possibeColors = (from x in imageDict.Keys where ImageWorker.CountDifferenceBetweenColors(x, imagePix[jj, ii]) < 5 orderby ImageWorker.CountDifferenceBetweenColors(x, imagePix[jj, ii]) select x).ToList(); if (possibeColors.Count>0) { imageDict[possibeColors[0]].Add(new ImageWorker.Point() { x = jj, y = ii }); } else { imageDict.Add(imagePix[jj, ii], new List<ImageWorker.Point>()); imageDict[imagePix[jj, ii]].Add(new ImageWorker.Point() { x = jj, y = ii }); } } else { imageDict[imagePix[jj, ii]].Add(new ImageWorker.Point() { x = jj, y = ii }); } } } string f=System.IO.Path.GetFileName(Session["File"].ToString()); // bt2.Save(AppDomain.CurrentDomain.BaseDirectory+"Files/tmp_"+f); ViewData["imagePic"] = imagePix; Session["imagePic"] = imagePix; Session.Remove("imageDic"); Session.Add("imageDic", imageDict); //Session["imageDic"] = imageDict; ViewData["X"] = X; ViewData["Y"] = Y; Session["X"] = X; Session["Y"] = Y; Session["kl"] = ViewData["k"]; return View(); }
public ActionResult Threads(string Id) { string pickType = Id; DateTime start = DateTime.Now; int X=(int)Session["X"]; int Y = (int)Session["Y"]; Dictionary<Color, List<ImageWorker.Point>> imageDict=new Dictionary<Color,List<ImageWorker.Point>>(); imageDict = (Dictionary<Color, List<ImageWorker.Point>>)Session["imageDic"]; double Smooth = 0.001; double XYSmooth = X * Y * Smooth; ImageWorker worker = new ImageWorker(); List<ImageWorker.ColorMul> MulNab = worker.GetColors(pickType); MyProject.ImageWorker.Picture picture = new MyProject.ImageWorker.Picture((int)Session["Y"],(int)Session["X"]); int h1 = ((Dictionary<Color, List<ImageWorker.Point>>)Session["imageDic"]).Values.Sum(x => x.Count); Session.Remove("imageDic"); Session.Add("imageDic", imageDict); int h = imageDict.Values.Sum(x => x.Count); foreach (Color c in imageDict.Keys) { MyProject.ImageWorker.ColorMul cm = worker.FindColor(c, ref MulNab); List<ImageWorker.ColorArray> t=picture.Color_array.FindAll(x => ImageWorker.CountDifferenceBetweenColors(x.color.color, cm.color) < 15); if (t.Count > 0) { picture.AddPoint(t.First().color, imageDict[c].AsReadOnly()); } else { picture.AddPoint(cm, imageDict[c].AsReadOnly()); } } DateTime findColors = DateTime.Now; string size = Session["Size"] as string; int kk = 0; switch (size) { case "Small": kk = 50; break; case "Large": kk = 100; break; case "Normal": kk = 80; break; } while (picture.Color_array.Count > kk) { ImageWorker.ColorMul reduceColor = (from x in picture.Color_array from y in picture.Color_array where x!=y orderby ImageWorker.CountDifferenceBetweenColors(x.color.color, y.color.color) select x.color).First(); List<MyProject.ImageWorker.ColorMul> cmtemp = (from x in picture.Color_array where x.color != reduceColor select x.color).ToList(); MyProject.ImageWorker.ColorMul c3 = worker.FindColor(reduceColor.color, ref cmtemp); if (c3 != null) { picture.ReduceColor(reduceColor, c3); } } List<ImageWorker.ColorMul> colorsForReduce = (from x in picture.Color_array where x.points.Count < XYSmooth select x.color).ToList(); List<MyProject.ImageWorker.ColorMul> cmtemp2 = (from x in picture.Color_array where x.points.Count >= XYSmooth select x.color).ToList(); foreach (MyProject.ImageWorker.ColorMul cc in colorsForReduce) { MyProject.ImageWorker.ColorMul c3 = worker.FindColor(cc.color, ref cmtemp2); if (c3 != null) { picture.ReduceColor(cc, c3); } } DateTime ReducingColors = DateTime.Now; ViewData["mess"] = string.Format("DictPOints:{2}FindColors: {0} ReduceColors:{1}", findColors-start, ReducingColors-findColors,h); ViewData["imageSmooth"] = picture; Session.Remove("imageSmooth"); Session.Add("imageSmooth", picture); ViewData["X"] = X; ViewData["Y"] = Y; return View(); }