Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a project that captures images by basler camera and for this i have a onimagegrabbed Function that grabs image. while running my project in any cpu it i did not getting any error. but when i turned to x86 , project runs but after 2 or 3 seconds I am getting exception message " system.argumentexception parameter is not valid. at system.drawing.bitmap..ctor(int32 width, int32 height, pixelformat format) ."


Screenshot-65 — ImgBB[^]

also showing this message in console tab
Exception thrown: 'System.ArgumentException' in System.Drawing.dll


What I have tried:

private void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
        {
            bool invokeRequired = base.InvokeRequired;
            if (invokeRequired)
            {
                base.BeginInvoke(new EventHandler<ImageGrabbedEventArgs>(this.OnImageGrabbed), new object[]
                {
                    sender,
                    e.Clone()
                });
            }
            else
            {
                try
                {
                    this.grabResult = e.GrabResult;
                    bool isValid = this.grabResult.IsValid;
                    if (isValid)
                    {
                        bool flag = !this.stopWatch.IsRunning || this.stopWatch.ElapsedMilliseconds > 33L;
                        if (flag)
                        {
                            this.stopWatch.Restart();
                            Bitmap bitmap =  new Bitmap(this.grabResult.Width, this.grabResult.Height, PixelFormat.Format32bppRgb);
                            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                            this.converter.OutputPixelFormat = PixelType.BGRA8packed;
                            IntPtr scan = bitmapData.Scan0;
                            this.converter.Convert(scan, (long)(bitmapData.Stride * bitmap.Height), this.grabResult);
                            bitmap.UnlockBits(bitmapData);
                            object obj = this.lockObject;
                            lock (obj)
                            {
                                Bitmap bitmap2 = this.DisplayWindow.Image as Bitmap;
                                bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                this.gb = this.CreateNonIndexedImage(bitmap);
                                this.DisplayWindow.Image = bitmap;
                                bool flag3 = bitmap2 != null;
                                if (flag3)
                                {
                                    bitmap2.Dispose();



                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    e.DisposeGrabResultIfClone();
                }
            }
        }




the line:150 is " Bitmap bitmap = new Bitmap(this.grabResult.Width, this.grabResult.Height, PixelFormat.Format32bppRgb);"
Posted
Updated 25-Dec-22 19:48pm
v2

1 solution

We can't tell: one of the three parameters you are passing to the constructor is invalid - which means you need to look at what exactly those values are before you can even begin to work out which one is invalid, let alone why.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the error line, and run your code through the debugger. Find out what the parameter values are when it hits the breakpoint, and check them against the valid values range. When you have worked out which one is wrong, you can look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900