Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question