@LevaMagnit

Почему так медленно работает цикл?

Как можно ускорить обработку каждого пикселя в потоке startCol?

[DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetWindowDC(IntPtr window);
        [DllImport("gdi32.dll", SetLastError = true)]
        public static extern uint GetPixel(IntPtr dc, int x, int y);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int ReleaseDC(IntPtr window, IntPtr dc);
        public static Color GetColorAt(int x, int y)
        {
            IntPtr desk = GetDesktopWindow();
            IntPtr dc = GetWindowDC(desk);
            int a = (int)GetPixel(dc, x, y);
            ReleaseDC(desk, dc);
            return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
        }

int distanceUse= 860;
        int distanceClean= 440;
        int pos_X = 0;
        int pos_Y = 0;
        int exp = 0;
        Color color = GetColorAt(0, 0);

private void StartCol()
        {
                for(int i = 0; i < 201; i++)
                  for (int j = 0; j < 201; j++)
                  {
                    pos_X = i;
                    pos_Y = j;
                    color = GetColorAt(i + distanceUse, j + distanceClean);
                    exp++;
                  }
        }

private void Form1_Load(object sender, EventArgs e)
        {
            Draw();
            Thread startCol= new Thread(StartCol);
            startCol.Start();
        }
  • Вопрос задан
  • 250 просмотров
Решения вопроса 1
VoidVolker
@VoidVolker Куратор тега C#
Dark side eye. А у нас печеньки! А у вас?
GetPixel для одиночных пикселей и работает медленно — поэтому для низкоуровеновой работы с изображениями не подходит. Правильное решение выглядит примерно вот так:
https://stackoverflow.com/questions/24701703/c-sha...
public class DirectBitmap : IDisposable
{
    public Bitmap Bitmap { get; private set; }
    public Int32[] Bits { get; private set; }
    public bool Disposed { get; private set; }
    public int Height { get; private set; }
    public int Width { get; private set; }

    protected GCHandle BitsHandle { get; private set; }

    public DirectBitmap(int width, int height)
    {
        Width = width;
        Height = height;
        Bits = new Int32[width * height];
        BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
        Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
    }

    public void SetPixel(int x, int y, Color colour)
    {
        int index = x + (y * Width);
        int col = colour.ToArgb();

        Bits[index] = col;
    }

    public Color GetPixel(int x, int y)
    {
        int index = x + (y * Width);
        int col = Bits[index];
        Color result = Color.FromArgb(col);

        return result;
    }

    public void Dispose()
    {
        if (Disposed) return;
        Disposed = true;
        Bitmap.Dispose();
        BitsHandle.Free();
    }
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы