【发布时间】:2010-09-04 23:01:36
【问题描述】:
我正在尝试通过将我的 PNG 中的 alpha 值复制到另一个相同大小的图像上来创建带有 alpha 通道的 PNG 的剪贴蒙版。全部使用 LockBits,然后是 UnlockBits。看来我的频道设置正确,但我没有看到它在后续绘图操作中使用。
为了尽可能简化事情,我使用几乎相同的逻辑仅在单个图像中设置红色通道值,但在保存图像后,再次没有任何变化。如果我单步执行代码,则红色通道有效设置正确。这是简化的代码。感谢任何 cmets 或帮助。
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);
}
【问题讨论】: