So to explain what my problem is, I made a PictureBox, and I need to fill in many FILLED squares inside of it. However, to do so I would need to create a brush and all of the solutions I've found online are returned as errors by Visual Studio 2019. I don't know what to do anymore.
Here's an example for brush declaration:
SolidBrush shadowBrush = new SolidBrush(customColor) (returns error)
Brush randomBrush = new brush(customColor) (returns error)
The way GDI+ drawing works, you should store all the data that represents your drawing in one or more fields and then read that data in the
Paint
event handler of the appropriate control to do the drawing. In your case, you need information to represent a square and the colour it will be drawn in and you need multiple of them. In that case, you should define a type that has aRectangle
property and aColor
property and store a genericList
of that type. You can then loop through that list, create aSolidBrush
with theColor
and callFillRectangle
.Now, to add a square, you simply create a new
Box
object, add it to theList
and then callInvalidate
on thePictureBox
. For simplicity, you can callInvalidate
with no arguments and the wholePictureBox
will be repainted. It is better if you can specify the area that has or may have changed though, because that keeps the repainting, which is the slow part, to a minimum. As you already have aRectangle
that describes the area that has changed, you can pass that, e.g.