T
T
TomYol2020-11-18 00:30:56
Windows Forms
TomYol, 2020-11-18 00:30:56

How to speed up the getpixel and setpixel methods?

There are several bitmaps in my application and they are all of high resolution. When using getpixel/setpixel, the application lags. Is it possible to somehow speed up or replace these methods. There is a lot of information on the Internet on this topic, but I still haven’t found a way that is clear to me? I read about Lockbitmap, found a class that uses it. It seems to be connected correctly, but I did not see any changes. Can someone explain in detail how I can speed up getpixel/setpixel in my project?

public static Bitmap myBitmap = new Bitmap("img\\BigMap.png");
        public static Bitmap myBitmapScale1 = new Bitmap("img\\BigMap.png");
        public static Bitmap myBitmapScale2 = new Bitmap("img\\BigMapScale2.png");

public void pictureBox1_Click(object sender, EventArgs e)
        {
            //получение цвета нажатого пикселя
            MouseEventArgs rato = e as MouseEventArgs;
            int x = rato.X * myBitmap.Width / pictureBox1.ClientSize.Width;
            int y = rato.Y * myBitmap.Height / pictureBox1.ClientSize.Height;
            Color backColor = myBitmap.GetPixel(x, y);
            string hex = backColor.R.ToString("X2") + backColor.G.ToString("X2") + backColor.B.ToString("X2");
          }


private void darkmode_Click(object sender, EventArgs e)
        {
            for (var x = 0; x < myBitmap.Width; x++)
                for (var y = 0; y < myBitmap.Height; y++)
                {
                    var pixel = myBitmap.GetPixel(x, y);
                    if (pixel.R == 176 && pixel.G == 244 && pixel.B == 254)
                        myBitmap.SetPixel(x, y, Color.DarkGray);
 
                }
            
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Developer, 2020-11-18
@samodum

The first query on Google gives the answer:
https://stackoverflow.com/questions/24701703/c-sha...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question