I am new in C# and i am trying to draw a color filled ellipse, I foud some code but i couldn't figure out how to do it.
I tried with this code:
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Aquamarine, 2);
g.DrawEllipse(pen, 10, 10, 100, 20);
But the method does not exist.
Colud you help me?
Thanks in advance.
You should do your graphics drawing in the forms Paint Event, otherwise as soon as the screen updates you will loose your drawing. This is a quick and dirty example on how to do so.
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form1_Paint);
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Aquamarine,2);
SolidBrush brush = new SolidBrush(Color.Aquamarine);
e.Graphics.DrawEllipse(pen, 10, 10, 100, 20);
e.Graphics.FillEllipse(brush, 10, 50, 100, 20);
}
Related
The current application am developing has lot of drawings. The Origin of the drawing start from Left,Bottom instead of Top,Left. Drawings works perfectly except "DrawingString".
Graphics g;
g.TranslateTransform(0, Height);
g.ScaleTransform(1, -1);
//All drawings
g.DrawString("1", new Font("Segoei UI", 9), Brushes.Green, new Point(x, y));
The result I get is upside down Text [![enter image description here][1]][1]
I wanted to draw only the text normal and rest of the drawings should always start from the bottom left?
EDIT
private void panel1_Paint(object sender,PaintEventArgs e)
{
var g = e.Graphics;
var height = panel1.Height;
g.TranslateTransform(0,height);
g.ScaleTransform(1,-1);
g.DrawRectangle(new Pen(Brushes.Black,1),new Rectangle(10,10,100,100));
g.DrawString("Test",new Font("Segoei UI",9),Brushes.Green,new Point(10,110));
}
RESULT
I want only the text to be flipped. Keeping the drawing as it is
Flip it over again.
See the example below. The text Sunday appears normally. Then we transpose the graphics object's matrix and write Monday, so Monday appears laterally inverted on the Y-axis, and finally we flip over the Y-axis yet again so it restores itself to its original state before writing Tuesday, which appears normally.
private void Form1_Paint(object sender, PaintEventArgs e)
{
var graphics = e.Graphics; // this.CreateGraphics();
var font = new Font("Georgia", 12.0F);
var brush = new SolidBrush(Color.Black);
var pointF = new PointF(20F, 20F);
graphics.DrawString("Sunday", font, brush, pointF);
graphics.ScaleTransform(1F, -1F);
pointF = new PointF(10F, -210F);
graphics.DrawString("Monday", font, brush, pointF);
graphics.ScaleTransform(1, -1);
pointF = new PointF(200F, 200F);
graphics.DrawString("Tuesday", font, brush, pointF);
brush.Dispose();
font.Dispose();
}
Do that in your code, making sure to calculate the value of the Y-axis where you want your text to appear.
g.ScaleTransform(1,-1);
g.DrawRectangle(new Pen(Brushes.Black,1),new Rectangle(10,10,100,100));
g.ScaleTransform(1,-1);
g.DrawString("Test",new Font("Segoei UI",9),Brushes.Green,new Point(10, -110));
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics graphicObj;
graphicObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Black, 2);
graphicObj.DrawEllipse(myPen, 200, 200, 200, 200);
}
This work on form. But for auto resizing items such as buttons, label I have to use TablePanelLayout, and drawing on it only works with adding to construtor in form
panel1.Paint+=new PaintEventHandler(panel1_draw);
and then by drawing with this method
private void panel1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
Pen myp = new Pen(System.Drawing.Color.Red, 4);
Font fy = new Font("Helvetica", 10, FontStyle.Bold);
Brush br = new SolidBrush(System.Drawing.Color.Red);
g.DrawString(textBox1.Text, fy, br, 0,0);
}
Question is Why/what topic should I master to know it.
I have an image with the dimensions of 100x50 and I want to draw a dot in the center - i.e. at 50x25. How would I do this?
you could use the setPixle() function.
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.Black, new Rectangle(50, 25, 1, 1));
look here for saving the picture
it does not draw on form load so you should add your code in form paintevent :
private void Form1_Paint(object sender, PaintEventArgs e)
{
Application.DoEvents();
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.DarkBlue, new Rectangle(120, 90, 1, 1));
}
Image img = pictureBox1.Image;
Graphics g = Graphics.FromImage(img);
g.DrawEllipse(Pens.DarkBlue, new Rectangle(50, 25, 1, 1));
g.DrawImage(img, new Point(0, 0));
img.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
try this for other question :
Rectangle bounds = new Rectangle(10, 20, 50, 60);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty,Point.Empty, bounds.Size);
bitmap.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
this code will capture screen with bound of bounds rectangle.
Can someone tell me what i don't make as it should, why i cannot save the drawing to a physical storage?
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red, 3);
Bitmap bmp = new Bitmap(700, 900);
Graphics gr = this.CreateGraphics();
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
//when i do this way it saves only a black rectangle, without other drawn content
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
// If i use the following 2 commented lines it saves only a empty rectangle.
//Rectangle rec = new Rectangle(0, 0, 700, 900);
// panel1.DrawToBitmap(bmp, rec);
}
Thank you for advice!
You have two problems here.
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}
The instance gr has nothing to do with your bitmap (bmp). So you're creating graphics that are associated with the form or control, and have a separate bitmap. When you save the bitmap, you haven't drawn anything in it.
You need to get a Graphics object from the Image, not from your form. I have not tested this, but it should work.
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
using (Bitmap bmp = new Bitmap(700, 900))
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}
I am trying to just draw a Rectangle on a PictureBox that is on a Form.
Writing text like shown here works fine.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Font myFont = new Font("Arial", 14))
{
e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
}
}
but when I try to draw a rectangle like so nothing shows up.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle();
rect.Location = new Point(25, 25);
rect.Width = 50;
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
What am I missing?
Well, you might have forgot to set rect.Height to something other than 0.
Did you check if the rectangle you're drawing has the correct dimensions?
Perhaps try
Rectangle rect = new Rectangle(new Point(25, 25), new Size(50, 50));
if you like it shorter.