为什么我在使用backgroundworker时遇到异常InvalidOperationException?

时间:2014-10-11 20:35:13

标签: c# .net winforms

异常消息是:位图区域已经被锁定,它位于Program.cs中:

Application.Run(new Form1());

该异常与方法有关:get_images_with_clouds

如果我没有使用backgroundworker并且在form1构造函数中我做了:

bitmaps = ImagesComparison.get_images_with_clouds(b);

它工作正常,但是当我在后台工作中执行此行时,我正在处理此异常。

在新的表单设计器中,我添加了一个新的backgroundworker1 在新表单构造函数中,我做了:

b = new Bitmap(InitGifFile);
pictureBox1.Image = b;
backgroundWorker1.RunWorkerAsync();

b是Bitmap变量,InitGifGile是字符串变量

只有当我开始使用backgroundworker时才会启动异常。 然后在backgroundworker dowork事件中我做了:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgw = (BackgroundWorker)sender;
                if (bgw.CancellationPending == true)
                {

                }
                else
                {
                    bitmaps = ImagesComparison.get_images_with_clouds(b);
                }
        }

方法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;

        }

完整的异常错误消息:

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: 

我不确定为什么在使用backgroundworker时它正在制作这个例外但是没有后台工作者它可以正常工作。

1 个答案:

答案 0 :(得分:2)

问题来自于b正在您的BackgroundWorker中使用并被分配到表单上的图片框。两者都试图同时在同一个对象上调用LockBitsbradar_image是对同一个对象的引用),而Bitmap只能有一次LockBits实例在一段时间。

如果您想在LockBits使用radar_image,您的后台工作人员或图片框必须处理位图的副本,而不是原始副本。