GDI+锁定位/阿尔法
我试图通过将 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与您使用提供给扩展方法的图像作为参数创建一个新的 Bitmap 实例有关。但是,一旦该方法完成,您将保存原始图像,而不是修改后的位图。
更改扩展方法以适用于 System.Drawing.Bitmap 类型。
Your problem is related to the fact that you create a new Bitmap instance using the image given to your extension method as a parameter. Once the method is finished, though, you are saving the original image, not the modified bitmap.
Change your extension method to work on types of System.Drawing.Bitmap.