Attempted to read or write protected memory when using lockbits

  • Thread starter Thread starter Jakeinc
  • Start date Start date
J

Jakeinc

Guest
Hello, I am trying to make a method that will shift all of a bitmaps pixels to the left, and the first ones go to the end. I originally tried using GetPixel and SetPixel but with an 1000 x 1000 bitmap, it did not go too well. I read about using lockbits and I was able to construct this method.

public void movetoright(Int32 numberoftimes)
{
BitmapData operabuffa = global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.LockBits(new Rectangle(0, 0, Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Width, Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Height), ImageLockMode.ReadWrite, global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.PixelFormat);
unsafe
{
for (int i = 1; i <= numberoftimes; i++)
{
for (int i3 = 0; i3 < global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Height; i3++) //y
{
byte* row = (byte *)operabuffa.Scan0 + (i3 * operabuffa.Stride);
for (int i4 = 0; i4 < global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Width - 1; i4++) //x
{
row[i4 * 3] = row[(i4 + 1) * 3];
}
}
for (int i5 = 0; i5 < global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Height; i5++) //y
{
byte* row = (byte *)operabuffa.Scan0 + (i5 * operabuffa.Stride);
row[global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.Width * 3] = row[0 * 3];
}
}
}
global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background.UnlockBits(operabuffa);
this.BackgroundImage = global::Abandoned_on_a_Moonlit_Night.Properties.Resources.background;
}
My problem is on the line
row[i4 * 3] = row[(i4 + 1) * 3];

the exception "Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." comes up. The variable i4 can be several different values when the exception comes up. Any ideas on how to fix this? Thanks. Sincerely, Jakeinc


Continue reading...
 
Back
Top