Getting System.InvalidOperationException: 'Bitmap region is already locked.' with a Basler Pylon SDK
vcidst opened this issue · comments
Hi,
Thanks for writing this! I wrote a .NET Framework GUI with the C# bindings and it works perfectly! However, I tried to use it with the C# program with Basler Pylon SDK and I got the following error,
System.InvalidOperationException
HResult=0x80131509
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)
I am using an earlier commit because the latest does not let me use System.Drawing Bitmap type. This error was generated by the following code,
var fpath1 = Directory.GetFiles($"..\\..\\..\\..\\re\\1")[0];
var fpath2 = Directory.GetFiles($"..\\..\\..\\..\\re\\2")[0];
DiffResult result;
var First = new Bitmap(fpath1);
var Second = new Bitmap(fpath2);
//crop
Rectangle roi = new Rectangle(1400, 450, 1100, 1900);
First = cropAtRect(First, roi);
Second = cropAtRect(Second, roi);
pictureBox.Image = First;
pictureBox1.Image = Second;
try
{
result = ImageDiff.Binding.Diff(First, Second,
new DiffOptions()
{
ErrorColor = Color.FromArgb(255, 255, 0, 255),
Tolerance = 0.26f,
OverlayTransparency = 1.0f,
OverlayType = OverlayType.Flat,
WeightByDiffPercentage = false,
IgnoreColor = false
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Update:
I resolved this. There were a few issues in the code above,
- The project was targeting .NET Framework 2.0, I upgraded it to 4.5
- The build was selected to Any CPU, I set it to x86 explicitly from the build options
- The following two lines should be commented or used after the result in the try catch block,
//pictureBox.Image = First;
//pictureBox1.Image = Second;