Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetImageData as Color[] #53

Closed
horseyhorsey opened this issue May 24, 2020 · 2 comments
Closed

GetImageData as Color[] #53

horseyhorsey opened this issue May 24, 2020 · 2 comments

Comments

@horseyhorsey
Copy link

Hi

I've been attempting to get a Color[] from the screen but not sure how to get the array of pixels from the ToPointer, just returns a single Color ?

var img = Raylib.GetScreenData();
var img2 = Raylib.GetImageData(img);
Color* mapPixels = (Color*)img2.ToPointer();
@ChrisDill
Copy link
Owner

ChrisDill commented May 24, 2020

It shows a single element because mapPixels is a pointer and it shows the element it is pointing to. This does not mean there is only one element inside it.

You can see this by looping through the colors. Note there is a large number of colors in screendata.

unsafe
{
    var img = Raylib.GetScreenData();
    var img2 = Raylib.GetImageData(img);
    Color* mapPixels = (Color*)img2.ToPointer();

    for (int i = 0; i < img.width * img.height; i += 1)
    {
        Color color = mapPixels[i];
        // Use color here
    }

You could also use the newer Span which may be more suitable for your needs.

unsafe
{
    var img = Raylib.GetScreenData();
    var img2 = Raylib.GetImageData(img);

    Span<Color> colors = new Span<Color>(img2.ToPointer(), img.width * img.height);
    foreach (var color in colors)
    {
        // Use color here
    }
}

@horseyhorsey
Copy link
Author

Thanks. I wasn't aware of Span or the Color* asterix reference in C#. That should've rang bells from using C.

Note there is a large number of colors in screendata.

Yes, I do have a small display though which may help it get through the work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants