Original Post
Hello. I'm having some trouble with a function I've created to join two textures together: The problem is that sometimes one of the UnlockRectangle calls will cause an exception "Exception System.AccessViolationException was thrown in debuggee: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". Also occasionally I get the same exception on one of the Stream.WriteByte calls, which seems odd considering all the bounds checking I checked the MSDN and as usual there wasn't much there(this exception isn't even listed under the possible Exceptions). My textures are created like this: Texture = new Direct3D.Texture( Game.Device, Width, Height, 1, Direct3D.Usage.SoftwareProcessing, Direct3D.Format.A8R8G8B8, Direct3D.Pool.Managed ); Any help is appreciated.
protected void Add( TextureDefinition.PatchDescriptor DescriptorReference )
{
Patch Patch = Resources.GetPatch( DescriptorReference.PatchNumber );
if( Patch == null )
{
return;
}
int R, G, B, A;
DirectX.GraphicsStream Stream = Texture.LockRectangle( 0, Direct3D.LockFlags.None );
DirectX.GraphicsStream PatchStream = Patch.Texture.LockRectangle( 0, Direct3D.LockFlags.None );
for( int y = 0, ty = DescriptorReference.Y; y < Patch.Height && ty < Height; y++, ty++ )
{
for( int x = 0, tx = DescriptorReference.X; x < Patch.Width && tx < Width; x++, tx++ )
{
PatchStream.Position = ( ( y * Patch.Width ) + x ) * 4;
R = PatchStream.ReadByte();
G = PatchStream.ReadByte();
B = PatchStream.ReadByte();
A = PatchStream.ReadByte();
if( A == 255 )
{
Stream.Position = ( ( ty * Width ) + tx ) * 4;
Stream.WriteByte( (byte)R );
Stream.WriteByte( (byte)G );
Stream.WriteByte( (byte)B );
Stream.WriteByte( (byte)A );
}
}
}
PatchStream.Close();
Patch.Texture.UnlockRectangle( 0 );
Stream.Close();
Texture.UnlockRectangle( 0 );
return;
}