0 / 0 / 0
Регистрация: 17.11.2015
Сообщений: 74
|
|
1
|
Медленная работа GetPixel
04.06.2017, 12:33. Показов 1573. Ответов 1
добрый день, возникла некие проблемы при работе такой штуки как getPixel, а именно скорость работы. При обработки большого файла получается очень долго.
C# | 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
| int B = 0;
int G = 0;
int R = 0;
int scorer = 0;
progressBar1.Value = 0;
progressBar1.Maximum = pictureBox1.Image.Width * pictureBox1.Image.Height;
for (int i = 0; i < pictureBox1.Image.Height; i++)
{
for (int j = 0; j < pictureBox1.Image.Width; j++)
{
B += ((Bitmap)pictureBox1.Image).GetPixel(j, i).B;
G += ((Bitmap)pictureBox1.Image).GetPixel(j, i).G;
R += ((Bitmap)pictureBox1.Image).GetPixel(j, i).R;
scorer++;
progressBar1.Value += 1;
}
}
B = B / scorer;
G = G / scorer;
R = R / scorer;
progressBar1.Value = 0;
Bitmap bmp = new Bitmap(135, 135);
using (var gl = Graphics.FromImage(bmp))
gl.Clear(Color.FromArgb(255,R,G,B));
pictureBox2.Image = bmp; |
|
я решил проблему гениально, нашел код который по идее должен работать в разы быстрее (ну как пишут). Добавил его отдельным классом, но я если честно слабо понимаю как его использовать. Можете мне немножко подсказать с комментами это супер кода?
C# | 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
| public unsafe class UnsafeBitmap
{
Bitmap bitmap;
int width;
BitmapData bitmapData = null;
Byte* pBase = null;
public UnsafeBitmap(Bitmap bitmap)
{
this.bitmap = new Bitmap(bitmap);
}
public UnsafeBitmap(int width, int height)
{
this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}
public void Dispose()
{
bitmap.Dispose();
}
public Bitmap Bitmap
{
get
{
return (bitmap);
}
}
private Point PixelSize
{
get
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref unit);
return new Point((int)bounds.Width, (int)bounds.Height);
}
}
public void LockBitmap()
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap.GetBounds(ref unit);
Rectangle bounds = new Rectangle((int)boundsF.X,
(int)boundsF.Y,
(int)boundsF.Width,
(int)boundsF.Height);
width = (int)boundsF.Width * sizeof(PixelData);
if (width % 4 != 0)
{
width = 4 * (width / 4 + 1);
}
bitmapData =
bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
pBase = (Byte*)bitmapData.Scan0.ToPointer();
}
public PixelData GetPixel(int x, int y)
{
PixelData returnValue = *PixelAt(x, y);
return returnValue;
}
public void SetPixel(int x, int y, PixelData colour)
{
PixelData* pixel = PixelAt(x, y);
*pixel = colour;
}
public void UnlockBitmap()
{
bitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}
public PixelData* PixelAt(int x, int y)
{
return (PixelData*)(pBase + y * width + x * sizeof(PixelData));
}
}
public struct PixelData
{
public byte blue;
public byte green;
public byte red;
} |
|
0
|