I am looking forward to make a rosette using c# graphics...
Here is what I'm trying to achieve:
Rosette
Till now i did this:
Pen lapsi = new Pen(Color.Black, 3);
Rectangle katrori = new Rectangle(250, 200, 200, 200);
Rectangle katrori2 = new Rectangle(350, 200, 200, 200);
Rectangle katrori3 = new Rectangle(150, 200, 200, 200);
Rectangle katrori4 = new Rectangle(200, 110, 200, 200);
Rectangle katrori5 = new Rectangle(200, 285, 200, 200);
Rectangle katrori6 = new Rectangle(295, 110, 200, 200);
Rectangle katrori7 = new Rectangle(300, 282, 200, 200);
Graphics g = this.CreateGraphics();
g.DrawEllipse(lapsi, katrori); //kryesor
g.DrawEllipse(lapsi, katrori2); //djatht
g.DrawEllipse(lapsi, katrori3); //majt
g.DrawEllipse(lapsi, katrori4); //nalt-majt
g.DrawEllipse(lapsi, katrori5); //posht-majt
g.DrawEllipse(lapsi, katrori6); //nalt-djatht
g.DrawEllipse(lapsi, katrori7);//posht djatht
I have drawn 7 circles so far, but i only want to show the one in the middle containing rossete. How to get rid of those extra lines outside the main circle..
If someone can give me some lines of code and show me how this is done, I'll appreciate it.
Start with the rectangle for your center circle and Inflate() it by some percentage. Next, create a GraphicsPath() and add an ellipse to it using the inflated rectangle via AddEllipse(). Finally, CLIP your graphics surface using SetClip(). This will prevent anything outside that ellipse from being displayed:
Pen lapsi = new Pen(Color.Black, 3);
Rectangle katrori = new Rectangle(250, 200, 200, 200);
Rectangle katrori2 = new Rectangle(350, 200, 200, 200);
Rectangle katrori3 = new Rectangle(150, 200, 200, 200);
Rectangle katrori4 = new Rectangle(200, 110, 200, 200);
Rectangle katrori5 = new Rectangle(200, 285, 200, 200);
Rectangle katrori6 = new Rectangle(295, 110, 200, 200);
Rectangle katrori7 = new Rectangle(300, 282, 200, 200);
Graphics g = this.CreateGraphics();
Rectangle clippingRectangle = new Rectangle(katrori.Location, katrori.Size);
clippingRectangle.Inflate((int)(katrori.Width * .2), (int)(katrori.Width * .2));
System.Drawing.Drawing2D.GraphicsPath GP = new System.Drawing.Drawing2D.GraphicsPath();
GP.AddEllipse(clippingRectangle);
g.SetClip(GP);
g.DrawEllipse(lapsi, katrori); //kryesor
g.DrawEllipse(lapsi, katrori2); //djatht
g.DrawEllipse(lapsi, katrori3); //majt
g.DrawEllipse(lapsi, katrori4); //nalt-majt
g.DrawEllipse(lapsi, katrori5); //posht-majt
g.DrawEllipse(lapsi, katrori6); //nalt-djatht
g.DrawEllipse(lapsi, katrori7);//posht djatht
g.Dispose();
Here's my finished, clipped rosette:
Related
I am trying to draw a Rectangle on a PictureBox, but when I click on another location the Rectangle moves to the new location. I would like to keep the original Rectangle and draw a new Rectangle at the given coordinates.
private void PictureBox_Paint(...)
Pen pen = new Pen(Color.Red, 3);
Rectangle rect = new Rectangle(x - 20, y - 10, 40, 20);
e.Graphics.DrawRectangle(pen, rect);
You need to add your Rectangles to a list of rectangles.
To do so just replace :
OnClick
Rectangle rect = new Rectangle(x - 20, y - 10, 40, 20);
Draw
e.Graphics.DrawRectangle(pen, rect);
With :
OnClick
myRectangleCollection.Add(new Rectangle(x - 20, y - 10, 40, 20));
Draw
foreach(Rectangle rect in myRectangleCollection)
{
e.Graphics.DrawRectangle(pen, rect);
}
Just don't forget to initialize your List of rectangles in your form initialization with this :
List<Rectangle> myRectangleCollection = new List<Rectangle>();
I have several outputs for my datas and want to create one fill-Object for any output-possibility. For example PrintDocument and Bitmap. Thats works with Graphics-Object, but the result is different.
My test method for fill graphics:
protected void fillGraphics(Graphics g)
{
g.PageUnit = GraphicsUnit.Pixel;
Pen pen = new Pen(Brushes.Black, 3);
g.DrawRectangle(pen, 100, 100, 300, 300);
g.DrawString("Wichtig!", new System.Drawing.Font(FontFamily.GenericSansSerif, 16), new SolidBrush(Color.Black), 120, 120);
}
I have created some Ellipses on Windows form
I want that selectable while mouse over and if i select any ellipse it will delete from Window
Here some code for create ellipse in c#
public void DrawCircle_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 3); Graphics gr = this.CreateGraphics(); gr.DrawEllipse(pen, 40, 45, 20, 20);
Pen pen2 = new Pen(Color.Black, 3); Graphics gr1 = this.CreateGraphics(); gr.DrawEllipse(pen2, 30, 25, 38, 20);
Pen pen3 = new Pen(Color.Black, 3); Graphics gr2 = this.CreateGraphics(); gr.DrawEllipse(pen3, 35, 36, 68, 15);
Pen pen4 = new Pen(Color.Black, 3); Graphics gr3 = this.CreateGraphics(); gr.DrawEllipse(pen4, 50, 60, 67, 35);
}
You could use
this.Invalidate();
That should clear them....
Either that, or
Graphics.Clear();
Edit:
Actually, another method be to have a boolean inside of your mouse on hover, and use that to dictate when to draw the elipse/not. As a general note, you should be putting the elipse drawing in an override of onpaint. Right now, currently, if you were to invalidate the screen (either by you, or by Windows), your shapes will disappear. An Example using on paint.
bool paint = false;
protected override void OnPaint(object sender, PaintEventArgs e)
{
if (paint)
{
Pen pen = new Pen(Color.Black, 3); Graphics gr = this.CreateGraphics(); gr.DrawEllipse(pen, 40, 45, 20, 20);
Pen pen2 = new Pen(Color.Black, 3); Graphics gr1 = this.CreateGraphics(); gr.DrawEllipse(pen2, 30, 25, 38, 20);
Pen pen3 = new Pen(Color.Black, 3); Graphics gr2 = this.CreateGraphics(); gr.DrawEllipse(pen3, 35, 36, 68, 15);
Pen pen4 = new Pen(Color.Black, 3); Graphics gr3 = this.CreateGraphics(); gr.DrawEllipse(pen4, 50, 60, 67, 35);
}
}
I don't remember the specific paramters for it, but in your OnMouseHover (where you want to turn off your elipse)
OnMouseHover(){
...
paint = false;
this.Invalidate();
}
I'd like to know how to paint User Control with background Transparency even if the user control is raised or moved at runtime.
my code is
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;//可以反锯齿
var rectBound = new Rectangle(0, 0, Width-1, Height-1);
var b = new SolidBrush(Color.FromArgb(0, 122, 204));
var rect = new Rectangle(2, 2, Width - 4, Height - 4);
if(!_isSelected)//FillRectangle
g.FillEllipse(b, rectBound);
else
g.FillEllipse(b, rect);
var pen = new Pen(Color.Yellow);
pen.DashStyle = DashStyle.DashDot;
g.DrawLine(pen,10,10,100,10);
pen.DashStyle = DashStyle.Dash;
g.DrawLine(pen, 10, 15, 100, 15);
pen.DashStyle = DashStyle.DashDotDot;
g.DrawLine(pen, 10, 20, 100, 20);
pen.DashStyle = DashStyle.Dot;
g.DrawLine(pen, 10, 25, 100, 25);
pen.DashStyle = DashStyle.Solid;
g.DrawLine(pen, 10, 30, 100, 30);
if (_isSelected)
{
pen = new Pen(Color.Black) { DashStyle = DashStyle.Dot, Width = 1 };
g.DrawRectangle(pen, rectBound);
}
}
and demo picture here
there will be a lot of User Controls Created at runtime,some of them maybe overlapped,then it should be clear the user control's background.how to do that?
I would like to draw some text in a rectangle and have it scale to the maximum size that fits within the rectangle.
So far I have this:
Bitmap bitmapImage = new Bitmap(500, 500);
Graphics graphicImage = Graphics.FromImage(bitmapImage);
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var rect = new Rectangle(0, 0, 500, 500);
graphicImage.DrawString( "testing testing 123!", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, rect);
bitmapImage.Save("test.png");
it draws the text but doesn't scale up the font size.
Call Graphics.MeasureString in a binary search loop.