c# - WinForms Bitmap region is already locked -


i'm trying slide through few images have 2 buttons forward , backwards. they're function scroll through list images. once 1 of them reaches end must go other side of list. here's have

private list<bitmap> rotatepacks = new list<bitmap> { new bitmap(@"assets\all_cards\all_royal\all_royal.png"),                                                         new bitmap(@"assets\all_cards\all_classic\all_classic.jpg")};  private void bnext_click(object sender, eventargs e) {     bitmap currentimage = (bitmap)pickcards.image;     (int = 0; < rotatepacks.count; i++)     {         if (areequal(currentimage, rotatepacks[i]))         {             try             {                 pickcards.image = rotatepacks[i + 1];             }             catch (exception)             {                 bprevious_click(sender, e);                 pickcards.image = rotatepacks[i - 1];             }         }     } } private void bprevious_click(object sender, eventargs e) {     bitmap currentimage = (bitmap)pickcards.image;     (int = 0; < rotatepacks.count; i++)     {         if (areequal(currentimage, rotatepacks[i]))         {             try             {                 pickcards.image = rotatepacks[i - 1];             }             catch (exception)             {                 bnext_click(sender, e);             }         }     } } 

those 2 buttons. here i'm trying compare image of picturebox that's holding images list rotatepacks. im getting current image being shown. here's areequal method :

public unsafe static bool areequal(bitmap b1, bitmap b2) // copy pasted {     if (b1.size != b2.size)     {         return false;     }      if (b1.pixelformat != b2.pixelformat)     {         return false;     }      /*if (b1.pixelformat != pixelformat.format32bppargb)     {         return false;     }*/     rectangle rect = new rectangle(0, 0, b1.width, b1.height);     bitmapdata data1         = b1.lockbits(rect, imagelockmode.readonly, b1.pixelformat);     bitmapdata data2         = b2.lockbits(rect, imagelockmode.readonly, b1.pixelformat);     int* p1 = (int*)data1.scan0;     int* p2 = (int*)data2.scan0;     int bytecount = b1.height * data1.stride / 4; //only format32bppargb       bool result = true;     (int = 0; < bytecount; ++i)     {         if (*p1++ != *p2++)         {             result = false;             break;         }     }      b1.unlockbits(data1);     b2.unlockbits(data2);      return result; } 

so problem buttons working way want them work once. if press button next , button previous or press next button twice program crash. gives me exception here

            bitmapdata data2             = b2.lockbits(rect, imagelockmode.readonly, b1.pixelformat); 

here's screenshots of actual exception :

http://prntscr.com/9ug3nl

http://prntscr.com/9ug3vv

  • p.s im using comparing method haven't program it. copied code stackoverflow question

there seems couple of issues here:

  1. it worth have check

    if (b1 == b2) //put     return true;  //do else rectangle rect = new rectangle(0, 0, b1.width, b1.height); //and on 

    likely b1 == b2 cause problem

  2. seems lockbits refer same exact items (same rect, same size, mode, same pixel format):

    rectangle rect = new rectangle(0, 0, b1.width, b1.height); bitmapdata data1     = b1.lockbits(rect, imagelockmode.readonly, b1.pixelformat); bitmapdata data2     = b2.lockbits(rect, imagelockmode.readonly, b1.pixelformat); 

    this cause of issue...


Comments