-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoHandler.cs
76 lines (62 loc) · 2.06 KB
/
PhotoHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace MonitorPhotoApp
{
class PhotosHandler
{
public ImageList ImageList { get; private set; }
private List<Bitmap> BitMapList { get; set; }
public PhotosHandler()
{
this.ImageList = new ImageList();
this.ImageList.ImageSize = new Size(56, 56);
this.ImageList.ColorDepth = ColorDepth.Depth32Bit;
this.BitMapList = new List<Bitmap>();
}
public ImageList GetImageListFromURLs(List<PhotoInfo> photoInfoList)
{
this.ImageList.Images.Clear();
this.BitMapList.Clear();
for (int i = 0; i < photoInfoList.Count; i++)
{
WebClient w = new WebClient();
byte[] imageByte = w.DownloadData(photoInfoList[i].URL);
MemoryStream stream = new MemoryStream(imageByte);
this.ImageList.Images.Add(Image.FromStream(stream));
this.BitMapList.Add(new Bitmap(Image.FromStream(stream)));
}
return this.ImageList;
}
public Color GetPicturesAvrColor(int picIndex)
{
return GetMostUsedColor(this.BitMapList[picIndex]);
}
private Color GetMostUsedColor(Bitmap bmp)
{
int r = 0;
int g = 0;
int b = 0;
int total = 0;
// Iterate through every pixel and store the total value of R,G and B
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color clr = bmp.GetPixel(x, y);
r += clr.R;
g += clr.G;
b += clr.B;
total++;
}
}
//Calculate average
r /= total;
g /= total;
b /= total;
return Color.FromArgb(r, g, b);
}
}
}