我正在构建一个 3d(红色,青色)程序。进展顺利,但在组合时,“图像”(见代码)似乎没有受到影响。
我的错误是什么?
认为:
RImage_P(point) = {5,5}
startCyan(point) = {0,0}
startRed(point) = {5,5}
CImage
,RImage
位于代码下方。图像结果是全黑透明图像。对于测试和调试,我注释了Alpha(结果是一样的)。
private void bitCombine(System.Drawing.Point startCyan, System.Drawing.Point startRed)
{
using (Bitmap image = new Bitmap(CImage.Width + Math.Abs(RImage_P.X), CImage.Height + Math.Abs(RImage_P.Y),PixelFormat.Format32bppArgb))
{
var rectfull = new Rectangle(0, 0, image.Width, image.Height);
var rect = new Rectangle(0, 0, CImage.Width, CImage.Height);
var bitsC = CImage.LockBits(rect, ImageLockMode.ReadOnly
, PixelFormat.Format32bppArgb);
var bitsA = alpha.LockBits(rect, ImageLockMode.ReadOnly
, PixelFormat.Format32bppArgb);
var bitsOutput = image.LockBits(rectfull, ImageLockMode.WriteOnly
, PixelFormat.Format32bppArgb);
unsafe
{
for (int y = 0; y < CImage.Height; y++)
{
byte* ptrC = (byte*)bitsC.Scan0 + y * bitsC.Stride;
byte* ptrA = (byte*)bitsA.Scan0 + y * bitsA.Stride;
byte* ptrOutput = (byte*)bitsOutput.Scan0
+ (y+startCyan.Y)* bitsOutput.Stride;
for (int x = 0; x < CImage.Width; x++)// cyan values
{// gui+ is reversed BGRA
ptrOutput[4 * (x+startCyan.X)] = ptrC[4 * x]; // blue
ptrOutput[4 * (x + startCyan.X) + 1] = ptrC[4 * x + 1];// green
//ptrOutput[4 * (x + startCyan.X) + 3] = ptrA[4 * x + 3];// alpha
}
}
CImage.UnlockBits(bitsC);
var bitsR = RImage.LockBits(rect, ImageLockMode.ReadOnly
, PixelFormat.Format32bppArgb);
for (int y = 0; y < RImage.Height; y++)// red values
{
byte* ptrR = (byte*)bitsR.Scan0 + y * bitsR.Stride;
byte* ptrA = (byte*)bitsA.Scan0 + y * bitsA.Stride;
byte* ptrOutput = (byte*)bitsOutput.Scan0
+ (y+ startRed.Y) * bitsOutput.Stride;
for (int x = 0; x < RImage.Width; x++)
{// gui+ is reversed BGRA
ptrOutput[4 * (x + startRed.X) + 2] = ptrR[4 * x + 2]; // red
//ptrOutput[4 * (x + startRed.X) + 3] = ptrA[4 * x + 3];// alpha
}
}
RImage.UnlockBits(bitsR);
alpha.UnlockBits(bitsA);
image.UnlockBits(bitsOutput);
this.image = image;
}
}
}
图像: