Не достаточно памяти при обработке изображений - C#
Формулировка задачи:
Сделал программу, начинающий программер, на моих системах х32 и х64 все выполняется отлично: после выполнения каждого цикла память сбрасывается до начального значения, при выполнении на системе друга выскакивает исключение(на скрине) и в диспетчере видно что память просто используется до придела
суть программы в поиске преобладающего цвета на картинке. Я указываю папку с файлами и папку сохранения, дальше уже программно загружается каждый файл pictureBox и получаю из него каждый 10-й пиксель
Решение задачи: «Не достаточно памяти при обработке изображений»
textual
Листинг программы
public static void pixels(Bitmap picture, string path_of_file, string pathToSave, string name_of_file) { string[] save_colours = new string[3]; // kol-vo uint[] cveta_kol = new uint[12]; // kol-vo kajdogo cveta byte[,] cveta = { // masive kodov cvetov }; int[] summ = new int[cveta.GetUpperBound(0) + 1]; // symma modyley raznostey kanalov pixeley double hei = picture.Height; double wid = picture.Width; #region if razmeri pikchi bolshe 500px for (int i = 0; i < hei; i++) // visota { for (int j = 0; j < wid; j += 10) // shirina { Color myPix = picture.GetPixel(j, i); for (int b = 0; b <= cveta.GetUpperBound(0); b++) { summ[b] = (int)(Math.Abs(cveta[b, 0] - myPix.R) + Math.Abs(cveta[b, 1] - myPix.G) + Math.Abs(cveta[b, 2] - myPix.B)); // dlia opredilenia prinadlejnosti k cvetu: // *1* otnimaju ot main colors znachenije kanalov pixelov } int min = summ[0]; for (int s = 0; s <= cveta.GetUpperBound(0); s++) { if (summ[s] < min) min = summ[s]; // gde min znachenie, k tomy cvety i otnositca } if (min == summ[0]) cveta_kol[0]++; else if (min == summ[1]) cveta_kol[0]++; else if (min == summ[2]) cveta_kol[0]++; else if (min == summ[3]) cveta_kol[0]++; else if (min == summ[4]) cveta_kol[0]++; else if (min == summ[5]) cveta_kol[1]++; else if (min == summ[6]) cveta_kol[1]++; else if (min == summ[7]) cveta_kol[1]++; else if (min == summ[8]) cveta_kol[1]++; else if (min == summ[9]) cveta_kol[1]++; else if (min == summ[10]) cveta_kol[1]++; else if (min == summ[11]) cveta_kol[2]++; else if (min == summ[12]) cveta_kol[2]++; else if (min == summ[13]) cveta_kol[2]++; else if (min == summ[14]) cveta_kol[2]++; else if (min == summ[15]) cveta_kol[2]++; else if (min == summ[16]) cveta_kol[3]++; else if (min == summ[17]) cveta_kol[3]++; else if (min == summ[18]) cveta_kol[3]++; else if (min == summ[19]) cveta_kol[3]++; else if (min == summ[20]) cveta_kol[3]++; else if (min == summ[21]) cveta_kol[3]++; else if (min == summ[22]) cveta_kol[3]++; else if (min == summ[23]) cveta_kol[3]++; else if (min == summ[24]) cveta_kol[3]++; else if (min == summ[25]) cveta_kol[3]++; else if (min == summ[26]) cveta_kol[3]++; else if (min == summ[27]) cveta_kol[3]++; else if (min == summ[28]) cveta_kol[4]++; else if (min == summ[29]) cveta_kol[4]++; else if (min == summ[30]) cveta_kol[4]++; else if (min == summ[31]) cveta_kol[4]++; else if (min == summ[32]) cveta_kol[4]++; else if (min == summ[33]) cveta_kol[4]++; else if (min == summ[34]) cveta_kol[4]++; else if (min == summ[35]) cveta_kol[5]++; else if (min == summ[36]) cveta_kol[5]++; else if (min == summ[37]) cveta_kol[5]++; else if (min == summ[38]) cveta_kol[5]++; else if (min == summ[39]) cveta_kol[5]++; else if (min == summ[40]) cveta_kol[5]++; else if (min == summ[41]) cveta_kol[5]++; else if (min == summ[42]) cveta_kol[6]++; else if (min == summ[43]) cveta_kol[6]++; else if (min == summ[44]) cveta_kol[6]++; else if (min == summ[45]) cveta_kol[6]++; else if (min == summ[46]) cveta_kol[6]++; else if (min == summ[47]) cveta_kol[6]++; else if (min == summ[48]) cveta_kol[6]++; else if (min == summ[49]) cveta_kol[6]++; else if (min == summ[50]) cveta_kol[6]++; else if (min == summ[51]) cveta_kol[6]++; else if (min == summ[52]) cveta_kol[6]++; else if (min == summ[53]) cveta_kol[7]++; else if (min == summ[54]) cveta_kol[7]++; else if (min == summ[55]) cveta_kol[7]++; else if (min == summ[56]) cveta_kol[7]++; else if (min == summ[57]) cveta_kol[7]++; else if (min == summ[58]) cveta_kol[7]++; else if (min == summ[59]) cveta_kol[7]++; else if (min == summ[60]) cveta_kol[8]++; else if (min == summ[61]) cveta_kol[8]++; else if (min == summ[62]) cveta_kol[8]++; else if (min == summ[63]) cveta_kol[8]++; else if (min == summ[64]) cveta_kol[8]++; else if (min == summ[65]) cveta_kol[9]++; else if (min == summ[66]) cveta_kol[9]++; else if (min == summ[67]) cveta_kol[9]++; else if (min == summ[68]) cveta_kol[9]++; else if (min == summ[69]) cveta_kol[9]++; else if (min == summ[70]) cveta_kol[9]++; else if (min == summ[71]) cveta_kol[9]++; else if (min == summ[72]) cveta_kol[9]++; else if (min == summ[73]) cveta_kol[10]++; else if (min == summ[74]) cveta_kol[10]++; else if (min == summ[75]) cveta_kol[10]++; else if (min == summ[76]) cveta_kol[11]++; else cveta_kol[11]++; } } #endregion #region max1 uint max1 = 0; for (int kol = 0; kol < cveta_kol.Length; kol++) { if (cveta_kol[kol] > max1) max1 = cveta_kol[kol]; // find the first most popular colour on the picture } if (max1 == cveta_kol[0]) { save_colours[0] = name_of_file; save_colours[1] = "красный"; cveta_kol[0] = 0; } else if (max1 == cveta_kol[1]) { save_colours[0] = name_of_file; save_colours[1] = "оранжевый"; cveta_kol[1] = 0; } else if (max1 == cveta_kol[2]) { save_colours[0] = name_of_file; save_colours[1] = "желтый"; cveta_kol[2] = 0; } else if (max1 == cveta_kol[3]) { save_colours[0] = name_of_file; save_colours[1] = "зеленый"; cveta_kol[3] = 0; } else if (max1 == cveta_kol[4]) { save_colours[0] = name_of_file; save_colours[1] = "голубой"; cveta_kol[4] = 0; } else if (max1 == cveta_kol[5]) { save_colours[0] = name_of_file; save_colours[1] = "синий"; cveta_kol[5] = 0; } else if (max1 == cveta_kol[6]) { save_colours[0] = name_of_file; save_colours[1] = "фиолетовый"; cveta_kol[6] = 0; } else if (max1 == cveta_kol[7]) { save_colours[0] = name_of_file; save_colours[1] = "розовый"; cveta_kol[7] = 0; } else if (max1 == cveta_kol[8]) { save_colours[0] = name_of_file; save_colours[1] = "серый"; cveta_kol[8] = 0; } else if (max1 == cveta_kol[9]) { save_colours[0] = name_of_file; save_colours[1] = "коричневый"; cveta_kol[9] = 0; } else if (max1 == cveta_kol[10]) { save_colours[0] = name_of_file; save_colours[1] = "черный"; cveta_kol[10] = 0; } else { save_colours[0] = name_of_file; save_colours[1] = "белый"; cveta_kol[11] = 0; } // **************************************************************** uint max2 = 0; for (int kol = 0; kol < cveta_kol.Length; kol++) { if (cveta_kol[kol] > max2) max2 = cveta_kol[kol]; // find the second most popular colour on the picture } if (Math.Round((double)max2 / max1, 2) <= 0.1) save_colours[2] = "---"; else { if (max2 == cveta_kol[0]) { save_colours[2] = "красный"; } else if (max2 == cveta_kol[1]) { save_colours[2] = "оранжевый"; } else if (max2 == cveta_kol[2]) { save_colours[2] = "желтый"; } else if (max2 == cveta_kol[3]) { save_colours[2] = "зеленый"; } else if (max2 == cveta_kol[4]) { save_colours[2] = "голубой"; } else if (max2 == cveta_kol[5]) { save_colours[2] = "синий"; } else if (max2 == cveta_kol[6]) { save_colours[2] = "фиолетовый"; } else if (max2 == cveta_kol[7]) { save_colours[2] = "розовый"; } else if (max2 == cveta_kol[8]) { save_colours[2] = "серый"; } else if (max2 == cveta_kol[9]) { save_colours[2] = "коричневый"; } else if (max2 == cveta_kol[10]) { save_colours[2] = "черный"; } else { save_colours[2] = "белый"; } } // *********************************************************************** #endregion max1 save_func(pathToSave, save_colours); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д