The code is executed in a separate thread, it calculates the average color of the image and draws it on the Control. When getting the pixel color _bmp.LockBits(new Rectangle(0, 0, _bmp.Width, _bmp.Height), ImageLockMode.ReadWrite, _bmp.PixelFormat); the message is: System.InvalidOperationException in System.Drawing.dll В данный момент объект используется другим процессом .

 private void SomeThread() { //.. Thread th0 = new Thread(new ParameterizedThreadStart(TH_showArt_)); th0.Start(Puth_); } private void TH_showArt_(object puth_) { String Puth_ = puth_.ToString(); Image img64x64 = Image.FromFile(Puth_); //.. //Считается средний цвет изображения string[] MD_C = MPLAY.Lib.BitmapClass.Middle_RGB_str((Bitmap)img64x64).Split(';'); //.. } public static string Middle_RGB_str(Bitmap Image_) { string MD_C = "0;0;0"; var bb = new BufferedBitmap(Image_); for (int i = bb.Height - 1; i > -1; i--) { for (int j = bb.Width - 1; j > -1; j--) { //Обертка LockBits, здесь кстати и ошибка Color clr = bb.GetPixel(i, j); //_bmp.LockBits( // new Rectangle(0, 0, _bmp.Width, _bmp.Height), // ImageLockMode.ReadWrite, // _bmp.PixelFormat //); //System.InvalidOperationException in System.Drawing.dll В данный момент объект используется другим процессом. //.. } } //.. MD_C = avgR.ToString() + ";" + avgG.ToString() + ";" + avgB.ToString(); return MD_C; } 

What can be done?

    1 answer 1

    You simultaneously use _bmp in some other thread (most likely, in the main). Look for where. Bitmap does not support multi-threaded work, so you need to either synchronize access to it, or work with its copy.

    • Everything related to bitmap goes in a separate thread, not basically, but the solution was the most stupid. I added the string BitmapData _bd; before var bb = new BufferedBitmap(Image_); mistake as a hand flicked away. - zayana