我试图通过将PNG中的alpha值复制到另一个相同大小的图像上,来创建具有alpha通道的PNG的剪切蒙版。全部使用LockBits,然后使用UnlockBits。我的通道似乎被正确地设置了,但我看不到它在后续的绘制操作中被使用。
为了尽可能简化,我使用了几乎相同的逻辑,只在单个图像中设置红色通道的值,但在保存图像后,再一次没有更改。如果我单步执行代码,红色通道的有效设置是正确的。以下是简化的代码。任何意见或帮助,我们将不胜感激。
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);
}发布于 2010-09-05 07:43:28
您的问题与您使用提供给扩展方法的图像作为参数创建一个新的Bitmap实例有关。但是,一旦该方法完成,您将保存原始图像,而不是修改后的位图。
更改您的扩展方法以在System.Drawing.Bitmap类型上工作。
https://stackoverflow.com/questions/3644308
复制相似问题