I'm trying to use unsafe mode to do some operations on two different bitmaps.
-I loaded 2 images
-Called my Change method.
- Locked their Bitmap data as bmDataLeft and bmDataRight.
- Doing some operations on BGR channel bits.
The problem is that no operation between them is affected.
First look at my code:
As you can see at the commented lines, When I try to use a data from a bitmap data on itself it works OK, but when I try to use both BitmapData datas I get no result!
I will appreciate all your solutions or if someone could tell me my mistake. Or may be in one Statement line there is only one bitmapData accesible in memory?
Thank you,
BestRegards,
Aram Azhari
-I loaded 2 images
-Called my Change method.
- Locked their Bitmap data as bmDataLeft and bmDataRight.
- Doing some operations on BGR channel bits.
The problem is that no operation between them is affected.
First look at my code:
Code:
public static Bitmap Change(Bitmap LeftBitamp, Bitmap RightBitamp)
{
Bitmap LeftB = LeftBitamp;
Bitmap RightB = RightBitamp;
// the return format is BGR.
// This is where I lock the data so I can declare a pointer to them.
// bmDataLeft for the LeftBitmap, bmDataRight for the RightBitmap,
BitmapData bmDataLeft = LeftB.LockBits(new Rectangle(0, 0, LeftB.Width, LeftB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmDataRight = RightB.LockBits(new Rectangle(0, 0, RightB.Width, RightB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// The normal procedure for pointer to an image variables
int strideL = bmDataLeft.Stride;
IntPtr Scan0L = bmDataLeft.Scan0;
int strideR = bmDataRight.Stride;
IntPtr Scan0R = bmDataRight.Scan0;
unsafe
{
#region 3 bytes
// These are my actual data I will do operations on.
byte* pL = (byte*) (void*) Scan0L;
byte* pR = (byte*) (void*) Scan0R;
// These are for the correct indexing.
// There shouldn't be a problem here.
int nOffsetLeft = strideL - LeftB.Width*3;
int nOffsetRight = strideR - RightB.Width*3;
int nWidthLeft = LeftB.Width*3;
int nWidthRight = RightB.Width*3;
// Looping through all pixels.
for (int y = 0; y < LeftB.Height; ++y)
{
for (int x = 0; x < nWidthLeft; ++x)
{
// This is the line that is not functioning properly
// Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
pL[0] = (byte)(pR[0]);
// But strangely this one does work! :
// pL[0] = (byte) (pL[0]+50);
// Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
++pL;
++pR;
}
pL += nOffsetLeft;
pR += nOffsetRight;
}
#endregion
}
LeftB.UnlockBits(bmDataLeft);
RightB.UnlockBits(bmDataRight);
LeftB.Save("LeftOne.jpg");
RightB.Save("RightOne.jpg");
return LeftB;
}
I will appreciate all your solutions or if someone could tell me my mistake. Or may be in one Statement line there is only one bitmapData accesible in memory?
Thank you,
BestRegards,
Aram Azhari
Comment