❔ Get Pixels cords from Image by their color
Hey, I'm writing a game and I need to get the pixels cords by their colors from an Image, I tried using Bitmap but it cannot be converted from System.drawing.Image to System.Drawing.Bitmap.
There is any other way to get pixel cords by their colors ?
11 Replies
huh?
If you are writing a game, don't you already have the pixel coords?
Either way, you can use GetPixel / SetPixel.
But they are not fast.
No, I will explain, I'm making pacman in my own terms ..
To make the map(gameCanvas) I want to draw image that's every color will determent the objects, for example:
Blue for walls
Yellow for coins
Red for traps and more..
I wouldn't recommend checking object by color value.
But instead use classes of their own.
And do you have any idea for making the game borders ? because I want to find smarter way then building the game borders manually
In the game code behind
I personally would make a system to easily make maps.
That way I can make my own editor in the future.
Although Pacman doesn't really have "other maps" it's just a single map
But I would still recommend to use classes instead of color values (including borders)
That way you aren't limited by colors
oh I get it, but if I don't have time to make map editor ? I have to hand over the project soon, would you recommend me in this case to just build in manually ?
If it's an assignment, I wouldn't want to remake the entire thing.
I would just go with what I have.
So as I mentioned before, the way to check a pixel value from an image is via the method GetPixel.
It's not the fastest but it will do.
There is LockBits that allows you to go a little more low level, but it's significantly faster. https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits?view=dotnet-plat-ext-7.0
Just not recommended for a beginner.
That's why I'm using traps instand of ghosts it more fast and easy to make them then the original ghosts
The thing that's i cant convert the image to bitmap, that's why i cant check the pixels cords by their colors
I saw in the link right now it may work
A bitmap takes in an image in the constructor.
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.-ctor?view=dotnet-plat-ext-7.0#system-drawing-bitmap-ctor(system-drawing-image)
But I wouldn't want to initialize a new bitmap each frame.
there is any thing else I can do to make the map unmanually ? I don't think I will finish in time with the method I had in mind
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.