- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Bitmap x = new Bitmap(s.Width, s.Height);
Graphics g = Graphics.FromImage(x);
g.CopyFromScreen(0, 0, 0, 0, new Size(s.Width, s.Height), CopyPixelOperation.SourceCopy);
g.Dispose();
x.Save("x_before.png", ImageFormat.Png);
Graphics g1 = Graphics.FromImage(x);
g1.DrawImage(x); // to make sure the image is in g1 context
g1.CopyFromScreen(0, 0, 0, 0, new Size(s.Width, s.Height), CopyPixelOperation.SourceInvert);
g1.Dispose();
x.Save("x_after.png", ImageFormat.Png);
我将屏幕复制到位图中。然后使用 Invert XOR 参数进入同一屏幕的同一位图。
1 xor 1 = 0
0 异或 0 = 0
0 异或 1 = 1
1 异或 0 = 1
结果应该是黑色图像。但事实并非如此。
这是否意味着 CopyPixelOperation.SourceInvert 不起作用?
最佳答案
Does it mean that CopyPixelOperation.SourceInvert doesn't work?
是的;我运行了您提供的代码,发现 CopyPixelOperation.SourceInvert
实际上并没有按照预期的方式工作。使用 Reflector 进一步调查发现 CopyFromScreen
方法在内部调用 gdi32.dll
方法 BitBlt
,可能是该方法中的错误。
更新:测试代码后,结果应该是 alpha 图像而不是黑色图像。 new Bitmap(s.Width, s.Height);
等于 new Bitmap(s.Width, s.Height, PixelFormat.Format32bppArgb);
所以结果是黑色alpha 设置为 0 的图像,因此透明图像。但如果使用 24 位,它应该是黑色图像:new Bitmap(s.Width, s.Height, PixelFormat.Format24bppRgb);
我做了一个替代代码,它使用 unsafe
并使用 PixelFormat.Format32bppArgb
或 PixelFormat.Format24bppRgb
“你可以扩展它的功能如果你必须”:
public unsafe static Bitmap ImageXOR(this Bitmap source, Bitmap destination)
{
#region Verification
if (destination == null)
{
throw new ArgumentNullException("newBitmap");
}
if (source.PixelFormat != destination.PixelFormat)
{
throw new ArgumentException("PixelFormat does not match");
}
if (source.Size != destination.Size)
{
throw new ArgumentException("Size does not match");
}
if (source.PixelFormat != PixelFormat.Format24bppRgb && source.PixelFormat != PixelFormat.Format32bppArgb)
{
throw new NotSupportedException(string.Format("Pixel format \"{0}\" not supported", source.PixelFormat));
}
#endregion//Verification
BitmapData sourceBitmapData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, source.PixelFormat);
try
{
BitmapData destinationBitmapData = destination.LockBits(
new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.ReadWrite, destination.PixelFormat);
try
{
int colorDepth = source.PixelFormat == PixelFormat.Format32bppArgb ? 4 : 3;
byte* sourceFirstPixelPhysicalAddress = (byte*)sourceBitmapData.Scan0.ToPointer();
byte* destinationFirstPixelPhysicalAddres = (byte*)destinationBitmapData.Scan0.ToPointer();
for (int heightIndex = 0; heightIndex < source.Height; heightIndex++)
{
for (int widthIndex = 0; widthIndex < sourceBitmapData.Width; widthIndex++)
{
byte* sourceRowPhysicalAddress = (byte*)sourceFirstPixelPhysicalAddress +
(heightIndex * sourceBitmapData.Stride);
byte* destinationRowPhysicalAddress = (byte*)destinationFirstPixelPhysicalAddres +
(heightIndex * destinationBitmapData.Stride);
int pixelPosition = widthIndex * colorDepth;
int indexOfBlue = 0 + pixelPosition;
int indexOfGreen = 1 + pixelPosition;
int indexOfRed = 2 + pixelPosition;
int indexOfAlpha = 3 + pixelPosition;
//get color values
//get blue
byte blue = (byte)((byte)sourceRowPhysicalAddress[indexOfBlue] ^ (byte)destinationRowPhysicalAddress[indexOfBlue]);
//get green
byte green = (byte)((byte)sourceRowPhysicalAddress[indexOfGreen] ^ (byte)destinationRowPhysicalAddress[indexOfGreen]);
//get red
byte red = (byte)((byte)sourceRowPhysicalAddress[indexOfRed] ^ (byte)destinationRowPhysicalAddress[indexOfRed]);
byte alpha = 0;
if (colorDepth > 3)
{
//get alpha
alpha = (byte)((byte)sourceRowPhysicalAddress[indexOfAlpha] ^ (byte)destinationRowPhysicalAddress[indexOfAlpha]);
}
//set blue
destinationRowPhysicalAddress[indexOfBlue] = blue;
//set green
destinationRowPhysicalAddress[indexOfGreen] = green;
//set red
destinationRowPhysicalAddress[indexOfRed] = red;
if (colorDepth > 3)
{
//set alpha
destinationRowPhysicalAddress[indexOfAlpha] = alpha;
}
}
}
}
finally
{
destination.UnlockBits(destinationBitmapData);
}
}
finally
{
source.UnlockBits(sourceBitmapData);
}
return destination;
}
关于.net - CopyPixelOperation.SourceInvert 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6727011/
Bitmap x = new Bitmap(s.Width, s.Height); Graphics g = Graphics.FromImage(x); g.Copy
我是一名优秀的程序员,十分优秀!