We had recently few threads (below) on SO where one of the common suggestion was do not use pointer.
is strings in .net get changed?? is there some bug?
I recently had requirement to convert Bitmap images white background to transparent. I tried around and most optimum implementation I could come up with is below.
public unsafe Bitmap MakeWhiteAreaTransparent(Bitmap source)
{
Bitmap bitmap = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
BitmapData bitmapdata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData data2 = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int* numPtr = (int*)bitmapdata.Scan0;
int* numPtr2 = numPtr + (bitmapdata.Width * bitmapdata.Height);
int* numPtr3 = (int*)data2.Scan0;
int num = Color.FromArgb(1, Color.White).ToArgb();
while (numPtr < numPtr2)
{
numPtr++;
Color color = Color.FromArgb(numPtr[0]);
if (((color.R < 200) || (color.G < 200)) || (color.B < 200))
{
numPtr3[0] = color.ToArgb();
}
else
{
numPtr3[0] = num;
}
numPtr3++;
}
source.UnlockBits(bitmapdata);
bitmap.UnlockBits(data2);
return bitmap;
}
I was wondering if I am correct in suing pointer or I should use byte array and System.Runtime.InteropServices.Marshal.Copy and not mug around with pointers???