Question

I'm working on a small application that needs to display some generated test screens on a chosen monitor. I'm generating these screens as bitmaps and for ease and speed of generation I'm using the LockBits() method to generate these bitmaps.

The problem I am seeing is showing up when I try to run LockBits on the bitmap generated for this 2nd monitor.

Rectangle Res = MonSelect.Resolution;
 private Bitmap GenerateColorBitmap(Color c)
    {
        Bitmap bmp = new Bitmap(Res.Width, Res.Height);
        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(Res, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

This same bit of code works fine if I'm running LockBits on the bitmap generated using the rectangle taken from my primary monitor's bounds but throws an ArgumentException if the rectangle is defined by the bounds of the second monitor. The relevent rectangle members are as follows:

X = -1440
Y = 0
Width = 1440
Height = 900
Left = -1440
Location {X = -1440 Y = 0}

My first thought is that maybe the negative X value is causing a problem with LockBits however replacing the X value with the absolute value of X yields the same exception. This all of course assumes that the rectangle is the invalid parameter but as a rectangle generated from my primary monitor works fine, I think it must be something about the second monitor's bounds that LockBits doesn't like.

Thanks in advance for any advice.

Was it helpful?

Solution

From MSDN LockBits about the rect parameter:

Type: System.Drawing.Rectangle
A Rectangle structure that specifies the portion of the Bitmap to lock.

So the Rectangle you passed in should be in your Bitmap Rectangle which always has Location as Point.Empty and the Size as the size of the bitmap. So the exception is thrown correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top