How can I draw these specific shapes on the panels?
I have two Panels to which I want to draw these two figures:
A figure in each one, I could put the image as such in the background in the
BackgroundImage
panel property, but at certain times I must change the color of the figure, therefore if I use it as a background image I would have to create an image of each color you want to use or create a method to change the color to an image that sounds complicated.
For example in this way:
private readonly List<Image> imageList;
private int currentImageIndex = 1;
private List<Image> CreateImages()
{
var lista = new List<Image>();
int width = 250;
int height = 50;
using (SolidBrush brush = new SolidBrush(Color.SteelBlue))
using (SolidBrush whiteBrush = new SolidBrush(Color.White))
{
for (int i = 0; i <= width; i += 10)
{
var bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.FillRectangle(whiteBrush, 0, 0, width, height);
gfx.FillRectangle(brush, width / 2 - i / 2, 0, i, height);
}
lista.Add(bmp);
}
}
return lista;
}
Its use would be the following:
panel1.BackgroundImage = imageList[currentImageIndex]; //Indice 0
panel2.BackgroundImage = imageList[currentImageIndex]; //Indice 1
Or converting the figure to BitMap (But how would I use this?):
var bmp1 = new Bitmap(Properties.Resources.Figura1);
var bmp2 = new Bitmap(Properties.Resources.Figura2);
Environment: Visual Studio 2010 - C# & .NET Framework 4
What you can do is draw directly on the
Panel
, making use of thePaint
. This event returns you an object of typeGraphics
that you can draw on directly.I would probably do something like that. First, you must subscribe to the event
Paint
of yourPanel
, either from the layout window properties window, or by adding this in your constructor:Later we create a method that will be in charge of drawing your figures. In my example it receives 2 parameters, one with the panel in which it should draw and another with what type of drawing (in this example, this last parameter changes the color of the drawing, it could receive a type parameter
Color
for example, it depends on your needs ):Finally, you create an event handler that takes care of calling the previous method:
As you will see, in the example if you pass it
0
in the second parameter, the figure will be blue, and if you pass it a1
, green.There are things to improve, but I think this can serve you for what you require.
Edit
Making a rounded rectangle is more complex, since the windows graphics library does not have any method for it. The only way is using one
Figure
using aPath
. The method would be this:To display it, simply using
FillPath
: