Lockbits doesn't affect my Image
-
29-06-2021 - |
Pergunta
I'm building a 3d (red,cyan) program. It went well, but when combining, the "image" (see in code) doesn't seemed to be affected.
What is my mistake?
Assume:
RImage_P(point) = {5,5}
startCyan(point) = {0,0}
startRed(point) = {5,5}
CImage
, RImage
are below the code.
Image result is a fully black transparent image.
For testing and debugging, I have commented the Alpha (the results are the same).
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;
}
}
}
The images:
Solução
I found out with an image visualizer that it does work, but the alpha channel is =0 the answer is to change the alpha channel's value to 255.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow