How to make a circle shape label in Window Form? - c#

As you all know, a label is usually in a square or rectangle shape. I really need to make circle shaped label. Can anyone please tell me is this possible or at least point me in a right direction?
Sorry, just to make things clear. I want a circle shaped label. Not just drawing a circle on the screen.

You can set the Region property of your Label :
var path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(0, 0, label1.Width, label1.Height);
this.label1.Region = new Region(path);

System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);

Related

How to paint a certain area

I am new to drawing and paints in c# & I am trying to make a simple program it has 3 intersecting circles (A,B,C). What i want to do is paint a certain (according to result I get).
For example: If I get 1 as a result I want to fill the yellow bordered region, if I get 4 I want to fill green bordered region and so on.
My Code to draw these circles:
private void button1_Click(object sender, EventArgs e)
{
Graphics A = this.CreateGraphics();
Graphics B = this.CreateGraphics();
Graphics C = this.CreateGraphics();
Pen Bluepen = new Pen(Color.Blue, 2);
Pen RedPen = new Pen(Color.Red, 2);
Pen BlackPen = new Pen(Color.Black, 2);
A.DrawEllipse(Bluepen,100, 100, 150, 150);
B.DrawEllipse(RedPen, 195, 100, 150, 150);
C.DrawEllipse(BlackPen, 145, 190, 150, 150);
}
Since you are new to this topic I have to tell you: This is a lot harder that one would hope for.
Three solutions come to mind:
Construct a GraphicsPath you could fill from three Arcs. To calculate the arcs you need the rectangles you have but also the sweeping angle and also the starting angle. This will take quite some math..
After having drawn into a Bitmap you could floodfill the area you want to color. This will only work for bitamps from which you can extract the current color of each pixel, not for drawing onto controls..
The simplest way it still a bit involved, but only mildly so
Solution 3 (Create a Region and fill it)
You can use all sorts of set operations to combine areas called Regions. And you can construct a Region from a GraphicsPath. And you can construct a GraphicsPath by adding an ellipse. And you can clip the drawing area of a Graphics object to a Region.
Let's try:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle r1 = new Rectangle(100, 100, 150, 150);
Rectangle r2 = new Rectangle(195, 100, 150, 150);
Rectangle r3 = new Rectangle(145, 190, 150, 150);
GraphicsPath gp1 = new GraphicsPath();
GraphicsPath gp2 = new GraphicsPath();
GraphicsPath gp3 = new GraphicsPath();
gp1.AddEllipse(r1);
gp2.AddEllipse(r2);
gp3.AddEllipse(r3);
Region r_1 = new Region(gp1);
Region r_2 = new Region(gp2);
Region r_3 = new Region(gp3);
r_1.Intersect(r_2); // just two of five..
r_1.Exclude(r_3); // set operations supported!
g.SetClip(r_1, CombineMode.Replace);
g.Clear(Color.Magenta); // fill the remaining region
g.ResetClip();
g.DrawEllipse(Pens.Red, r1);
g.DrawEllipse(Pens.Blue, r2);
g.DrawEllipse(Pens.Green, r3);
// finally dispose of all Regions and GraphicsPaths!!
r_1.Dispose();
gp1.Dispose();
.....
}
Do note that the region operations change the current region; if you want to fill more areas you need to restore the changed region!
Also note that I draw where any persistent drawing belongs: In the Paint event and that I use its e.Graphics object..
GraphicsPaths as Regions are GDI objects and should be disposed off!
Notes on solution 1 (Create a GraphicsPath by Math)
The full math is rather involved. By making a few assumptions the task can be greatly simplified: Let's assume the circles have the same size. Also that we first look at two circles only, with the same y-position. Finally that the circles form a symmetrical figure. (Which btw they don't: the red circle should have x=190 and the green one y=186,45..)
Getting the two intersection points as well as the sweeping angle is not so hard.
Next one can rotate the two points twice around the center of the whole figure by 120° using a Matrix; see here for an example. Now we have six points; we still need the smaller sweeping angle, which is also found with simple math.
Finally we can construct all 12 (!) GraphicsPaths from the 12 arcs and combine them at will.
The good part is that we can both fill and draw those paths. But, the code is rather extensive..
Notes on solution 2 (floodfill)
While you can't floodfill directly on a control you can prepare the result in a bitmap and then display that image on the control with Graphics.DrawImage.
For an example of coding a floodfill see this post!

How to draw in c#

I am trying to draw the YinYang symbol on C# windows Form Application. So far, I drew the big outer circle and the two innner circles.
I need help on drawing the curve part that runs down the middle of the circle
Also, how would I fill in the small circle and the other half of the circle to be black.
Also, it is possible to draw this without having to have a button (see code).
Here is a snippet of my code:
private void button1_Click(object sender, EventArgs e)
{
Graphics myGraphics = base.CreateGraphics();
Pen myPen = new Pen(Color.Black);
SolidBrush mySolidBrush = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 50,50, 150, 150);
Graphics innerCircle = base.CreateGraphics();
Pen myPen2 = new Pen(Color.Black);
SolidBrush mySolidBrush2 = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 118, 75, 20, 20);
Graphics innerCircle2 = base.CreateGraphics();
Pen myPen3 = new Pen(Color.Black);
SolidBrush mySolidBrush3 = new SolidBrush(Color.Black);
myGraphics.DrawEllipse(myPen, 118, 150, 20, 20);
}
You do not have to draw a curve, geometry of ying and yang is so beautiful that it lets you draw it only using circles.
Sorry for my paint skills, but I think you know what I mean by this pic. You said
I drew the big outer circle and the two innner circles.
So use this knowledge again without thinking about curves
Is there a method to draw on the panel in c# which not redraw what i've drawn? E.g. when I use refresh() or Invalidate() alway redraw me it, but I need something what not. :(

How to draw a rectangle with a background color in WPF

Hi I want to have a rectangle like the below picture to fill the entire canvas with different sizes and at different position scatter through out the canvas. My solution environment is WPF C#
Could some one please guide me of how to do it? Till now what I have done is canvas_loaded
Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Black);
rect.Width = 100;
rect.Height = 100;
rect.Stroke = new SolidColorBrush(Colors.Black);
But the problem here how will I position it to the different locaion of the canvas, the size and width I can provide at run time with different value but I need to position the rectangles (Square) at diffrent XY co-ordinates so that none of the rectangles or Squares overlapp each other.
Please help.
You can use
Canvas.SetLeft(rect, <offset>) Canvas.SetRight(...), Canvas.SetTop(...), Canvas.SetBottom(...)
to position UIElement in the Canvas container.
Use Random class to generate the xy co-ordinates
Random r=new Random();
r.Next(1,100);

Drawing a Rectangular Border around a Text Drawn by GraphicsPath

I'm using the GraphicPath.Addstring method to add a string to the graphic path and Graphics.drawpath to draw the string. I want to calculate the width and height of the text drawn so that I can draw a rectangle around the drawn text. I have tried using Graphics.MeasureString and TextRenderer.MeasureText to calculate the width and Graphicpath.addrectangle and Graphics.Drawpath to draw the rectangle, but I'm not able to get the correct dimensions with different fonts.
Please help me solve this.
Add your string to the path then call
GraphicsPath.GetBounds();
to determine the region. This will need to be inflated depending on the pen size used to render the string.
Maybe look into Graphics.MeasureCharacterRanges
hope this example helps you
var gp = new GraphicsPath();
var g = baseControl.CreateGraphics();
var textSize = g.MeasureString(text, basefont);
gp.AddRectangle(new Rectangle(new Point(0, 0), textSize);
gp.AddString("Your String", basefont.FontFamily, basefont.Style, basefont.Size - 0.25f);
Little correction :
gp.AddRectangle(new Rectangle(new Point(0, 0), textSize.ToSize());

declaring and instantiating Rectangles and Circles

Is there anyway to declare and instantiate an ellipse in C#? I can do it with a rectangle as shown bellow:
private Rectangle rect = new Rectangle();
Is there a way for me to do the following(or something similar)?:
private Ellipse circ = new Ellipse();
A "Rectangle" is an abstract set of coordinates.
In contrast to "Drawing.DrawRectangle()", "Drawing.FillRectangle()" and "Drawing.DrawEllipse()", which are graphical primitives you can draw on a canvas.
I think you're looking for the "Draw" primitives:
http://msdn.microsoft.com/en-us/library/4cy2c290%28v=VS.71%29.aspx
PS:
The graphics methods "DrawEllipse()" and "FillEllipse()" can take "Rectangle()" coordinates as input parameters :)

Categories

Resources