I am attempting to create clipping mask out of a PNG with an alpha channel by copying the alpha values from my PNG over another same sized image. All using LockBits and then UnlockBits. It appears that my channel is correctly being set but I don't see it being used in subsequent drawing operations.
In an attempt to simplify things as much as I can, I used virtually the same logic to only set the Red channel value in a single image but after saving the image, again, no change. If I step through the code, the Red channel valid is being correctly set. Here is that simplified code. Any comments or help is appreciated.
var image = Image.FromFile(@"C:\imaging\image.jpg");
image.LoadRed();
image.Save(@"C:\imaging\output.jpg");
// image.jpg and output.jpg are the same.
// I would expect output to be washed over with lots of red but it isn't
public static void LoadRed(this Image destination)
{
var destinationBitmap = new Bitmap(destination);
const int blueChannel = 0;
const int greenChannel = 1;
const int redChannel = 2;
const int alphaChannel = 3;
var rec = new Rectangle(Point.Empty, destination.Size);
var destinationData = destinationBitmap.LockBits(rec, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* destinationPointer = (byte*)destinationData.Scan0.ToPointer();
destinationPointer += redChannel;
for (int i = rec.Width * rec.Height; i > 0; i--)
{
*destinationPointer = 255;
destinationPointer += 4;
}
}
destinationBitmap.UnlockBits(destinationData);
}