0

I have this part of code which converts a bitmap with 32bppArgb pixel format to an 1d byte[] array:

using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
    var boundsRect = new Rectangle(0, 0, width, height);

    // Copy pixels from screen capture Texture to GDI bitmap
    var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
    var sourcePtr = mapSource.DataPointer;
    var destPtr = mapDest.Scan0;
    for (int y = 0; y < height; y++)
    {
        // Copy a single line 
        Utilities.CopyMemory(destPtr, sourcePtr, width * 4);

        // Advance pointers
        sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
        destPtr = IntPtr.Add(destPtr, mapDest.Stride);
    }

    // Release source and dest locks
    bitmap.UnlockBits(mapDest);
    device.ImmediateContext.UnmapSubresource(screenTexture, 0);

    using (var ms = new MemoryStream())
    {
        bitmap.Save(ms, ImageFormat.Bmp); 
        ScreenRefreshed?.Invoke(this, ms.ToArray());
        _init = true;
    }
}

I call my function ReplacePixels() to read and replace rgba values like this:

data = ReplacePixels(data);

data is byte[] array received from code above.

The example function which i use but without success:

private byte[] ReplacePixels (byte[] data)
{
    int width = Screen.PrimaryScreen.Bounds.Width;
    int height = Screen.PrimaryScreen.Bounds.Height;
    

    Int32 curRowOffs = 0;
    Int32 stride = 4 * (width * 4 + 31) / 32;

    try
    {
        for (uint y = 0; y < height; y++)
        {
            // Set offset to start of current row
            Int32 curOffs = curRowOffs;
            for (uint x = 0; x < width; x++)
            {
                // ARGB = bytes [B,G,R,A]
                var b = data[curOffs];

                var g = data[curOffs + 1];

                var r = data[curOffs + 2];

                var a = data[curOffs + 3];

                //bgra changes here..
                
                //apply bgra values
                data[offset] = Convert.ToByte(b);
                data[offset + 1] = Convert.ToByte(g);
                data[offset + 2] = Convert.ToByte(r);
                data[offset + 3] = Convert.ToByte(a);

                // Increase offset to next colour
                curOffs += 4;
            }
            // Increase row offset
            curRowOffs += stride;
        }
    }
    catch (System.Exception e)
    {
        Debug.WriteLine(e);
    }


    return data;
}

The question is: how can i read and replace the argb values from this array?

Edit: this is the solution that i found

    public byte[] ReplacePixels(byte[] data)
    {

        int width = Screen.PrimaryScreen.Bounds.Width;
        int height = Screen.PrimaryScreen.Bounds.Height;
        Int32 stride = width * 4;
        Int32 curRowOffs = (((width * height * 4) + 54) - 1) - stride;

        for (uint y = 0; y < height; y++)
        {

            uint index = (uint)curRowOffs;


            for (uint x = 0; x < width; x++)
            {
                // ARGB = bytes [B,G,R,A]
                if (index >= 0)
                {

                    //var rgba = GetRGB(data, index);
                    var b = data[index];
                    var g = data[index + 1];
                    var r = data[index + 2];
                    var a = data[index + 3];

                    //bgra changes here...
                    
                    data[index] = b;
                    data[index + 1] = g;
                    data[index + 2] = r;
                    data[index + 3] = a;
                    
                }

                index += 4;
            }

            curRowOffs -= stride;


        }
        return data;
    }
Eamrle
  • 3
  • 1
  • What exactly doesn't work? Do you not need to copy the values back into an image? – Charlieface Mar 06 '21 at 21:12
  • Heh, that's my code, [from right here on SO](https://stackoverflow.com/a/49859493/395685). Obviously you need to use the same index variable to write the values back, yes. In fact, there's not even a variable named `offset` _defined_ in that first code. – Nyerguds Mar 16 '21 at 21:48
  • @Nyerguds, yes, you are right, thank you for your efforts! – Eamrle Mar 16 '21 at 22:02

0 Answers0