The exception message is: Bitmap region is already locked and it's in Program.cs on the line:
Application.Run(new Form1());
The exception is connected with the method: get_images_with_clouds
If i'm not using the backgroundworker and in the form1 constructor i did:
bitmaps = ImagesComparison.get_images_with_clouds(b);
It was working fine but when i'm doing this line in the backgroundworker do work event i'm getting this exception.
In a new form designer i added a new backgroundworker1 In the new form constructor i did:
b = new Bitmap(InitGifFile);
pictureBox1.Image = b;
backgroundWorker1.RunWorkerAsync();
b is a Bitmap variable and InitGifGile is a string variable
The exception started only when i started to use the backgroundworker. Then in the backgroundworker dowork event i did:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
if (bgw.CancellationPending == true)
{
}
else
{
bitmaps = ImagesComparison.get_images_with_clouds(b);
}
}
And the method get_images_with_clouds:
public static Bitmap[] get_images_with_clouds(Bitmap radar_image)
{
int e = 0;
int f = 0;
int image_clock_area_x = 0;
int image_clock_area_y = 0;
int image_clock_area_x1 = 140;
int image_clock_area_y1 = 21;
Bitmap[] localImages;
localImages = new Bitmap[15];
Bitmap image;
image = new Bitmap(Properties.Resources.radar_without_clouds);
BitmapData bmD = null;
BitmapData bmD2 = null;
try
{
bmD = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
bmD2 = radar_image.LockBits(new Rectangle(0, 0, radar_image.Width, radar_image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr sc0 = bmD.Scan0;
unsafe
{
int* p = (int*)sc0.ToPointer();
int* p2 = (int*)bmD2.Scan0.ToPointer();
for (e = image_clock_area_x; e < image_clock_area_x + image_clock_area_x1; e++)
{
for (f = image_clock_area_y; f < image_clock_area_y + image_clock_area_y1; f++)
{
Color clock_color = Color.FromArgb(p2[e + f * bmD2.Width]);
p[e + f * bmD.Width] = clock_color.ToArgb();
}
}
}
image.UnlockBits(bmD);
radar_image.UnlockBits(bmD2);
}
catch
{
try
{
image.UnlockBits(bmD);
}
catch
{
}
try
{
radar_image.UnlockBits(bmD2);
}
catch
{
}
}
int c;
for (c = 0; c < localImages.Length; c++)
{
localImages[c] = new Bitmap(image);
}
Bitmap new_image = new Bitmap(Properties.Resources.radar_without_clouds);
Bitmap new_image1 = new Bitmap(Properties.Resources.radar_without_clouds);
Bitmap localbmptest = black_and_white(new_image, radar_image);
Image image1 = black_and_white(new_image, radar_image);
image1.Save(@"c:\temp\testclouds666.jpg");
Bitmap clouds = new Bitmap(image1);
int x;
int y;
int a;
int b;
int d = 0;
Bitmap redImage;
redImage = new Bitmap(512, 512);
using (Graphics g = Graphics.FromImage(redImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
g.Clear(Color.Red);
}
BitmapData bmData = null;
BitmapData bmData2 = null;
BitmapData bmDataArray = null;
try
{
bmData = clouds.LockBits(new Rectangle(0, 0, clouds.Width, clouds.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
bmData2 = radar_image.LockBits(new Rectangle(0, 0, radar_image.Width, radar_image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr scan0 = bmData.Scan0;
IntPtr scan02 = bmData2.Scan0;
unsafe
{
int* p = (int*)scan0.ToPointer();
int* p2 = (int*)scan02.ToPointer();
double h, mm;
for (d = 0; d < localImages.Length; d++)
{
bmDataArray = localImages[d].LockBits(new Rectangle(0, 0, localImages[d].Width, localImages[d].Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr scan0Array = bmDataArray.Scan0;
int* pArray = (int*)scan0Array.ToPointer();
for (a = 0; a < new_image.Width; a++)
{
for (b = 0; b < new_image.Height; b++)
{
Color color1 = Color.FromArgb(p[a + b * bmData.Width]);
Color color2 = Color.FromArgb(p2[a + b * bmData2.Width]);
if (color1.R != 0 || color1.G != 0 || color1.B != 0)
{
h = color2.GetHue();
mm = RadarAnalysis.Hue2MMPerHour(h);
if (mm >= treshhold_array[14 - d])
{
pArray[a + b * bmDataArray.Width] = color2.ToArgb();
}
}
}
}
localImages[d].UnlockBits(bmDataArray);
}
}
clouds.UnlockBits(bmData);
radar_image.UnlockBits(bmData2);
}
catch (Exception error)
{
try
{
clouds.UnlockBits(bmData);
}
catch
{
}
try
{
radar_image.UnlockBits(bmData2);
}
catch
{
}
try
{
localImages[d].UnlockBits(bmDataArray);
}
catch
{
}
Logger.Write("Error Exception ==> " + error);
MessageBox.Show("Error Exception ==> " + error);
}
return localImages;
}
The full exception error message:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Bitmap region is already locked.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
I'm not sure why when using the backgroundworker it's making this exception but without the backgroundworker it's working fine with the same line.
The problem comes from the fact that b
is being used both in your BackgroundWorker and is being assigned to a picture box on your form. Both are attempting to call LockBits
on the same object at the same time ( b
and radar_image
are to references to the same object) and a Bitmap can only have once instance of LockBits active at a time.
If you want to use LockBits
on radar_image
either your background worker or your picture box must be working on a copy of the bitmap, not the original.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.