C#.net error: Attempt to read or write protected memory or often this is the indication that other memory is currupt
-
the code that causes error is boldfaced...see below the inner most loop code. private void toolStripButton3_Click(object sender, EventArgs e) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// GDI+ still lies to us - the return format is BGR, NOT RGB. ///Cover Image Code Bitmap bmp1 = new Bitmap(textBox1.Text); ///takes image from openfiledlg textbox1 ///takes an object of BitmapData & apply LocBits() BitmapData bmp1data = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); ///finds out Bitmap's stride int stride1 = bmp1data.Stride; ///defines pointer's to scan0 (1st byte of BimapData array System.IntPtr Scan1 = bmp1data.Scan0; ///end of Cover Image Code /////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// /// Stego Image code Bitmap bmp2 = new Bitmap("f:\\bb.jpg"); BitmapData bmp2data = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride2 = bmp2data.Stride; System.IntPtr Scan2 = bmp2data.Scan0; unsafe { ///unsafe stego code byte* ptr2 = (byte*)(void*)Scan2; int offset2 = stride2 - bmp2.Width * 3; byte b2; ///end of unsafe stego //////////////////////////////////////////////////////////////// ///unsafe cover code /// defines a byte type pointer to scan0 byte* ptr1 = null; ptr1 = (byte*)(void*)Scan1; /// finds offset of the BitmapData int offset1 = stride1 - bmp1.Width * 3; /// defines RGB values byte b1,index=0; bool bitt; int x1, x2, y1, y2; ///iterates through the BitmapData ///end of unsafe cover code //////////////////////////////////////////////////////////////// for (y2 = 0; y2