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.
Related
The code below won't work no matter how many times I press the button, it's really annoying me
private void button2_Click(object sender, EventArgs e)
{
Bitmap screen = new Bitmap(600, 800);
Graphics gfx = Graphics.FromImage(screen);
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 0));
gfx.FillRectangle(brush, 0, 0, 600, 800);
gfx.Dispose();
brush.Dispose();
screen.Dispose();
}
You didn't ever draw your bitmap to the screen. You could do this like that:
private void button2_Click(object sender, EventArgs e)
{
Bitmap screen = new Bitmap(600, 800);
Graphics gfx = Graphics.FromImage(screen);
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 0));
gfx.FillRectangle(brush, 0, 0, 600, 800);
CreateGraphics().DrawImage(screen, 0, 0);
gfx.Dispose();
brush.Dispose();
screen.Dispose();
}
However you shouldn't use that, as it prevents optimal DoubleBuffering to work and as the changes are erased whenever the paint event is fired, and therefore isn't good practice.
Instead you should handle the Paint event (To do this, go to the designer (press F7 in the code behind), select your form, open the properties window (Press F4), select the events tab (click onto the lightning bolt) and double click into the field right to Paint):
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap screen = new Bitmap(600, 800);
Graphics gfx = Graphics.FromImage(screen);
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 0));
gfx.FillRectangle(brush, 0, 0, 600, 800);
e.Graphics.DrawImage(screen, 0, 0);
gfx.Dispose();
brush.Dispose();
screen.Dispose();
}
However it's not recommendable to use a bitmap in that case, as you don't have much being displayed - instead you should directly draw to the graphics of the form:
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (var gfx = e.Graphics)
using (var brush = new SolidBrush(Color.FromArgb(255, 255, 0)))
gfx.FillRectangle(brush, 0, 0, 600, 800);
}
You could notice that I used using, which is good practice, as it automatically disposes the objects.
Now all you have to do (you have to do this for both solutions including the handling of the paint event) is to call Invalidate() in your button click event handling and set a property that tells the paint method that it is being called because a button got pressed:
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (!_button2Pressed)
return;
using (var gfx = e.Graphics)
using (var brush = new SolidBrush(Color.FromArgb(255, 255, 0)))
gfx.FillRectangle(brush, 0, 0, 600, 800);
_button2Pressed = false;
}
private void button2_Click(object sender, EventArgs e)
{
_button2Pressed = true;
Invalidate();
}
private bool _button2Pressed;
This should do it.
I trying draw a rectangles in PictureBox by mouse click:
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, e.X, e.Y, 50, 50);
pen.Dispose();
}
}
And rectangles are drawning. But when i move mouse beyond the PictureBox all rectangles are disappear. How to avoid it?
UPDATE
I added a Paint event:
private List<Rectangle> Rectangles { set; get; }
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
pen.Dispose();
}
}
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
Rectangles.Add(new Rectangle(e.X, e.Y, 50, 50));
MyPictureBox.Refresh();
}
But now rectangles not drawning.
Update
Oh it was my mistake.
g.DrawRectangle(pen, rect); -> e.Graphics.DrawRectangle(pen, rect);
Yes, you're drawing over the picture box. When the next paint messgae arrives, picturebox re-paints itself again at that time it'll overwrite your rectangles.
You either need to draw it in Paint event in order to make your rectangles survive or you can Draw over the PictureBox.Image so it will stay there.
For your edit: You need to use e.Graphics property. For instance following code works for me.
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (var pen = new Pen(Color.Black, 2))
{
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
}
}
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);
}
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.