/// <summary> /// To get R, G and B percentage /// </summary> /// <param name="fileName"></param> /// <param name="RedPercentage"></param> /// <param name="BluePercentage"></param> /// <param name="GreenPercentage"></param> /// <param name="Height"></param> /// <param name="Width"></param> public void GetRGBPercentage(string fileName,ref int RedPercentage ,ref int BluePercentage,ref int GreenPercentage , ref int Height, ref int Width) { try { Boolean val = new CommonImplementation().IsLocalPath(fileName); long Red = 0, Blue = 0, Green = 0, totalPixel=0; Bitmap bmp = null; if (!val) { WebClient l_WebClient = new WebClient(); byte[] l_imageBytes = l_WebClient.DownloadData(fileName); MemoryStream l_stream = new MemoryStream(l_imageBytes); System.Drawing.Image.FromStream(l_stream); bmp = new Bitmap(System.Drawing.Image.FromStream(l_stream)); } else { bmp = new Bitmap(fileName); } Height = bmp.Height; Width = bmp.Width; for (int x = 0; x < bmp.Size.Width; x += pixelIncrementVal) { for (int y = 0; y < bmp.Size.Height; y += pixelIncrementVal) { Red += bmp.GetPixel(x, y).R; Green += bmp.GetPixel(x, y).G; Blue += bmp.GetPixel(x, y).B; } } totalPixel = Red + Green + Blue; RedPercentage = totalPixel != 0 ? (int)Math.Round((Convert.ToDecimal(Red) / Convert.ToDecimal(Red + Green + Blue)) * 100) : 0; GreenPercentage = totalPixel != 0 ? (int)Math.Round((Convert.ToDecimal(Green) / Convert.ToDecimal(Red + Green + Blue)) * 100) : 0; BluePercentage = totalPixel != 0 ? (int)Math.Round((Convert.ToDecimal(Blue) / Convert.ToDecimal(Red + Green + Blue)) * 100) : 0; } catch (Exception) { throw; } }
/// <summary> /// Reason : To get color codes from image /// Image devided into #x# parts to pickup pixel colors /// </summary> /// <param name="fileName">Input file name</param> /// <param name="colorNames">Returns colors and their density in image</param> /// <returns>returns color model</returns> public List<ColorModel> GetImageColors(string fileName) { Boolean val = new CommonImplementation().IsLocalPath(fileName); Bitmap bmp = null; if (!val) { WebClient l_WebClient = new WebClient(); byte[] l_imageBytes = l_WebClient.DownloadData(fileName); MemoryStream l_stream = new MemoryStream(l_imageBytes); System.Drawing.Image.FromStream(l_stream); bmp = new Bitmap(System.Drawing.Image.FromStream(l_stream)); } else { bmp = new Bitmap(fileName); } HashSet<string> colors = new HashSet<string>(); List<string> colorList = new List<string>(); int Red = 0, Blue = 0, Green = 0, Yellow = 0, Pink = 0, SkyBlue = 0, Orange = 0, Purple = 0, White = 0, Black = 0, Grey=0,Brown=0; for (int x = 0; x < bmp.Size.Width; x +=pixelIncrementVal) { for (int y = 0; y < bmp.Size.Height; y += pixelIncrementVal) { try { //var v = from t in colors where t == (bmp.GetPixel(x, y)).Name select t; //if (!(v.Count() > 0)) { colors.Add(bmp.GetPixel(x, y).Name.ToString()); string color = GetColorFromRGB(bmp.GetPixel(x, y).R, bmp.GetPixel(x, y).G, bmp.GetPixel(x, y).B); var colorNameList = from t in colorList where t == color select t; if (!(colorNameList.Count() > 0) && color != "") colorList.Add(color); switch (color.Replace(" ", "")) { case "Red": Red += 1; break; case "Blue": Blue += 1; break; case "Green": Green += 1; break; case "Yellow": Yellow += 1; break; case "Pink": Pink += 1; break; case "SkyBlue": SkyBlue += 1; break; case "Orange": Orange += 1; break; case "Purple": Purple += 1; break; case "White": White += 1; break; case "Black": Black += 1; break; case "Grey": Grey += 1; break; case "Brown": Brown += 1; break; default: break; } } } catch (Exception) { } } } return GetUniqueColorList(colorList, Red, Blue, Green, Yellow, Pink, SkyBlue, Orange, Purple, White, Black, Grey, Brown); }
/// <summary> /// Reason : To get color codes from image /// Image devided into #x# parts to pickup pixel colors /// </summary> /// <param name="fileName">Input file name</param> /// <param name="colorNames">Returns colors and their density in image</param> /// <returns>returns color model</returns> public List <ColorModel> GetImageColors(string fileName) { Boolean val = new CommonImplementation().IsLocalPath(fileName); Bitmap bmp = null; if (!val) { try { WebClient l_WebClient = new WebClient(); byte[] l_imageBytes = l_WebClient.DownloadData(fileName); MemoryStream l_stream = new MemoryStream(l_imageBytes); System.Drawing.Image.FromStream(l_stream); bmp = new Bitmap(System.Drawing.Image.FromStream(l_stream)); } catch (Exception) { return(null); } } else { if (!File.Exists(fileName)) { return(null); } bmp = new Bitmap(fileName); } HashSet <string> colors = new HashSet <string>(); List <string> colorList = new List <string>(); int Red = 0, Blue = 0, Green = 0, Yellow = 0, Pink = 0, SkyBlue = 0, Orange = 0, Purple = 0, White = 0, Black = 0, Grey = 0, Brown = 0; for (int x = 0; x < bmp.Size.Width; x += pixelIncrementVal) { for (int y = 0; y < bmp.Size.Height; y += pixelIncrementVal) { try { //var v = from t in colors where t == (bmp.GetPixel(x, y)).Name select t; //if (!(v.Count() > 0)) { colors.Add(bmp.GetPixel(x, y).Name.ToString()); string color = GetColorFromRGB(bmp.GetPixel(x, y).R, bmp.GetPixel(x, y).G, bmp.GetPixel(x, y).B); var colorNameList = from t in colorList where t == color select t; if (!(colorNameList.Count() > 0) && color != "") { colorList.Add(color); } switch (color.Replace(" ", "")) { case "Red": Red += 1; break; case "Blue": Blue += 1; break; case "Green": Green += 1; break; case "Yellow": Yellow += 1; break; case "Pink": Pink += 1; break; case "SkyBlue": SkyBlue += 1; break; case "Orange": Orange += 1; break; case "Purple": Purple += 1; break; case "White": White += 1; break; case "Black": Black += 1; break; case "Grey": Grey += 1; break; case "Brown": Brown += 1; break; default: break; } } } catch (Exception) { } } } return(GetUniqueColorList(colorList, Red, Blue, Green, Yellow, Pink, SkyBlue, Orange, Purple, White, Black, Grey, Brown)); }