Несколько GetPixel с экрана - код работает слишком медленно - C#
Формулировка задачи:
Собственно в данный момент я использую этот код.
Проблема в том, что для получения даже 3-6 пикселей требуется достаточно много времени. Есть ли какой-то более эффективный способ получить несколько пикселей с экрана? Я читал про Bitmap.LockBits и GDI+, но так и не разобрался что это и как с этим работать.
Спасибо.
Листинг программы
- int GetPixel(int X, int Y)
- {
- Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppPArgb);
- Graphics grp = Graphics.FromImage(bmp);
- grp.CopyFromScreen(new Point(X, Y), Point.Empty, new Size(1, 1));
- grp.Save();
- return bmp.GetPixel(0, 0).ToArgb();
- }
- MyMethod()
- {
- if (GetPixel(950, 100) == -239423) do smthing...
- else if (GetPixel(936, 94) == -893452) do smthing...
- else if (GetPixel(987, 98) == -7549234) do smthing...
- else if (GetPixel(845, 104) == -7812373) do smthing...
- else if (GetPixel(738, 104) == -1372823) do smthing...
- else if (GetPixel(855, 102) == -823849) do smthing...
- else if (GetPixel(917, 97) == -8927346) do smthing...
- else if (GetPixel(977, 100) == -162736) do smthing...
- else if (GetPixel(981, 92) == -623809) do smthing...
- }
Решение задачи: «Несколько GetPixel с экрана - код работает слишком медленно»
textual
Листинг программы
- public class MyClass
- {
- static public Bitmap bmp;
- static public Graphics grp;
- static public void Init()
- {
- Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppPArgb);
- Graphics grp = Graphics.FromImage(bmp);
- grp.CopyFromScreen(new Point(X, Y), Point.Empty, new Size(1, 1));
- grp.Save();
- }
- static public int GetPixel(int X, int Y)
- {
- return bmp.GetPixel(0, 0).ToArgb();
- }
- static public void MyMethod()
- {
- Init();
- if (GetPixel(950, 100) == -239423) do smthing...
- else if (GetPixel(936, 94) == -893452) do smthing...
- else if (GetPixel(987, 98) == -7549234) do smthing...
- else if (GetPixel(845, 104) == -7812373) do smthing...
- else if (GetPixel(738, 104) == -1372823) do smthing...
- else if (GetPixel(855, 102) == -823849) do smthing...
- else if (GetPixel(917, 97) == -8927346) do smthing...
- else if (GetPixel(977, 100) == -162736) do smthing...
- else if (GetPixel(981, 92) == -623809) do smthing...
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д