The main goal is to get all pixels from the bitmap very fast, and now this is done in 30fps.
I save the bitmap to byte[]
array which has length: 4196406(4mb).
This is the code I use which give me ~5fps:
using (var data = new MemoryStream())
{
bitmap.Save(data, ImageFormat.Bmp);
ScreenRefreshed?.Invoke(this, data.ToArray());
_init = true;
}
Color[,] Pixels = new Color[Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height];
Pixels = GetPixels(data);
public Color[,] GetPixels(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;
Color[,] buffer = new Color[width, height];
for (uint y = 0; y < height; y++)
{
uint index = (uint)curRowOffs;
for (uint x = 0; x < width; x++)
{
if (index >= 0)
{
var b = data[index];
var g = data[index + 1];
var r = data[index + 2];
var a = data[index + 3];
buffer[x, y] = Color.FromArgb(a,r,g,b); //This line is reducing the fps
}
index += 4;
}
curRowOffs -= stride;
}
return buffer;
}
How to assign the values of array to another one and loop through the second array keeping the same speed(30fps)?
This line: buffer[x, y] = Color.FromArgb(a,r,g,b);
- reduces fps from 30
to 5