none
Bitmap.LockBits Method throws "System.ArgumentException" saying "Parameter is not valid" RRS feed

  • Question

  • The program breaks at the Image.LockBits(new rectangle(x, y, width, height), ImageLockMode.ReadWrite, Image.PixelForma) method with the exception I mentioned in the title. I checked the image format and everything. I'm stumped.

    private unsafe void frmArtAnalyzer_Load(object sender, EventArgs e)
            {
                // Load Default Canvas
                imageX = gbImageView.Location.X + gbImageView.Size.Width + 16;
                imageY = 16;
                Image = new Bitmap(640, 480, PixelFormat.Format24bppRgb);
                ImageData = Image.LockBits(new Rectangle(imageX, imageY, 640, 480), ImageLockMode.ReadWrite, Image.PixelFormat);
                {
                    // Fill canvas
                    byte* row;
                    int scan0 = (int)ImageData.Scan0;
                    for (int y = 0; y < ImageData.Height; y++)
                    {
                        row = (byte*)scan0 + ImageData.Stride * y;
                        for (int x = 0; x < 640; x++ )
                        {
                            row[x * 24] = 255;
                            row[x * 24 + 8] = 255;
                            row[x * 24 + 16] = 255;
                        }
                    }
                }
                
                // Load the selection resources
                selections = new Bitmap(640, 480);
                selection = new Selection[8];
                selection[0] = new Selection();
                selection[0].Width = 640;
                selection[0].Height = 480;
            }


    Tuesday, April 7, 2015 5:02 PM

Answers

  • You're trying to lock an area outside the bitmap. The bitmap size is 640x480, the lock area size is also 640x480 but the lock area offset is something like 16x16.
    Tuesday, April 7, 2015 5:48 PM
    Moderator

All replies

  • You're trying to lock an area outside the bitmap. The bitmap size is 640x480, the lock area size is also 640x480 but the lock area offset is something like 16x16.
    Tuesday, April 7, 2015 5:48 PM
    Moderator
  • Oh! I get it! The rectangle determines the specific region of the bitmap to lock, not the specific region within the window it resides in. That will be super useful.

    Thanks for clearing that up. Now I have different bugs to work out later.

    Tuesday, April 7, 2015 6:53 PM