Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我';当使用backgroundworker时,我得到异常InvalidOperationException?_C#_.net_Winforms - Fatal编程技术网

C# 为什么我';当使用backgroundworker时,我得到异常InvalidOperationException?

C# 为什么我';当使用backgroundworker时,我得到异常InvalidOperationException?,c#,.net,winforms,C#,.net,Winforms,异常消息是:位图区域已被锁定,它位于Program.cs中的以下行: Application.Run(new Form1()); 异常与方法有关:使用云获取图像 如果我没有使用backgroundworker,并且在form1构造函数中使用了: bitmaps = ImagesComparison.get_images_with_clouds(b); b = new Bitmap(InitGifFile); pictureBox1.Image = b; backgroundWorker1.R

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

Application.Run(new Form1());
异常与方法有关:使用云获取图像

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

bitmaps = ImagesComparison.get_images_with_clouds(b);
b = new Bitmap(InitGifFile);
pictureBox1.Image = b;
backgroundWorker1.RunWorkerAsync();
它工作得很好,但是当我在backgroundworker do work事件中执行这一行时,我得到了这个异常

在一个新的表单设计器中,我添加了一个新的backgroundworker1 在新表单构造函数中,我执行了以下操作:

bitmaps = ImagesComparison.get_images_with_clouds(b);
b = new Bitmap(InitGifFile);
pictureBox1.Image = b;
backgroundWorker1.RunWorkerAsync();
b是位图变量,InitGifGile是字符串变量

只有当我开始使用backgroundworker时,异常才开始出现。 然后在backgroundworker嫁妆活动中,我做了:

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

                }
                else
                {
                    bitmaps = ImagesComparison.get_images_with_clouds(b);
                }
        }
该方法使用云获取图像:

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;

        }

我不知道为什么在使用backgroundworker时会出现这种情况,但如果没有backgroundworker,它在同一行中运行良好。

问题在于
b
正在backgroundworker中使用,并且被分配到表单上的图片框中。两者都试图同时调用同一对象上的
LockBits
b
radar\u image
是对同一对象的引用),位图一次只能有一个锁位实例处于活动状态


如果要在
雷达图像上使用
锁定位
,则背景工作者或图片框必须处理位图的副本,而不是原始位图。

问题在于
b
正在后台工作者中使用,并且正在分配给表单上的图片框。两者都试图同时调用同一对象上的
LockBits
b
radar\u image
是对同一对象的引用),位图一次只能有一个锁位实例处于活动状态


如果要在
雷达图像上使用
锁定位
,则背景工作者或图片框必须处理位图的副本,而不是原始位图。

问题在于
b
正在后台工作者中使用,并且正在分配给表单上的图片框。两者都试图同时调用同一对象上的
LockBits
b
radar\u image
是对同一对象的引用),位图一次只能有一个锁位实例处于活动状态


如果要在
雷达图像上使用
锁定位
,则背景工作者或图片框必须处理位图的副本,而不是原始位图。

问题在于
b
正在后台工作者中使用,并且正在分配给表单上的图片框。两者都试图同时调用同一对象上的
LockBits
b
radar\u image
是对同一对象的引用),位图一次只能有一个锁位实例处于活动状态

如果要在
radar\u image
上使用
LockBits
,则背景工作者或图片框必须处理位图的副本,而不是原始位图