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>();
Related
I need to draw a rectangle that should be around the size 2X2 Inches when printed on a paper.
I know that i can draw a rectangle using
g.DrawRectangle(pen, 100,100, 100, 200);
This application will only be used in computers.How can i convert the inches to pixels properly so that i can get the desired dimensions when printed.
To make an image print in the right size by default, it needs to have the right combination of dpi and pixels.
Let's look at an example:
// aiming at 150dpi and 4x6 inches:
float dpi = 150;
float width = 4;
float height = 6;
using (Bitmap bmp = new Bitmap((int)(dpi * width), (int)(dpi * height)))
{
// first set the resolution
bmp.SetResolution(dpi, dpi);
// then create a suitable Graphics object:
using (Graphics G = Graphics.FromImage(bmp))
using (Pen pen = new Pen(Color.Orange))
{
pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Center;
G.Clear(Color.FloralWhite);
// using pixels here:
Size sz = new System.Drawing.Size((int)dpi * 2 - 1, (int)dpi * 2 - 1);
G.DrawRectangle(pen, new Rectangle(new Point(0, 0), sz));
G.DrawRectangle(pen, new Rectangle(new Point(0, 300), sz));
G.DrawRectangle(pen, new Rectangle(new Point(0, 600), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 0), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 300), sz));
G.DrawRectangle(pen, new Rectangle(new Point(300, 600), sz));
// alternative code:
// we can also set the Graphics object to measure stuff in inches;
G.PageUnit = GraphicsUnit.Inch;
// or fractions of it, let's use 10th:
G.PageScale = 0.1f;
using (Pen pen2 = new Pen(Color.MediumPurple, 1f / dpi * G.PageScale))
{
// draw one rectangle offset by an inch:
G.DrawRectangle(pen2, 10f, 10f, 20f, 20f);
}
bmp.Save(#"D:\xxx.jpg", ImageFormat.Jpeg);
}
}
Note that I had to subtract 1 pixel from the drawn size as DrawRectangle overdraws by 1 pixel!
Note that the coordinates I draw at depend on the resolution! Also note how the jpeg format creates a lot of smeared colors. Pngcreates crisper results, especially once you print text..
Also note how I had to scale down the PenWidth in the alternative code!
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 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:
Basically I have a form with no border, I want to keep it like that. It's in a fixed position. I'm trying to draw an outline of the form size in the form (so it looks like a border) I'm having trouble and never really used "drawing" techniques in forms.
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
Graphics g = CreateGraphics();
g.DrawRectangle(pen, rect);
Do it in OnPaint:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
e.Graphics.DrawRectangle(pen, rect);
}
I'm currently trying to draw a black rectangle over an image, but it just wont go to the bottom of the image. If I try to push it down, it just fills the image with the black rectangle.
Here's my code:
public void AddTextToImage(string LabelText, string CardNo)
{
BitMap LabelImage = new BitMap("C:\ImageLocation");
int32 y = Convert.toInt32(CardNo;
int32 x = LabelImage.Width;
Rectangle rec = new Rectangle(Convert.ToInt32(LabelText), Convert.ToInt32(CardNo, LabelImage.Width, 250);
\\Mainly been playing with the numbers, can't figure out the right coordinates though
graphic.DrawRectangle(new Pen(Color.Black, LabelImage.Width), 0, -200, LabelImage.Width, y);
graphic.DrawString(LabelText, new Font("Tahoma", 40), Brushes.Black, new System.Drawing.Point(0,y));
picImage.Image = LabelImage;
}
Don't use -200. It will be outside the image
graphic.DrawRectangle(new Pen(Color.Black, LabelImage.Width), 0, 200, LabelImage.Width, y);