I have MainForm class. Here I can make somthing like this.
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
And I can see result in my Form.
But I have another class Image. And I want to draw from here. How can I do it?
Send the PaintEventArgs (the below came from one i have been using)
class Draw
{
public void Paint(PaintEventArgs e)
{
e.Graphics.DrawRectangles(Pens.Blue, GetRectangle());
}
}
where GetRectangle would be another method to define the rectangle
you should also be able to just send your object (in your case the instance of MainForm)
class Draw
{
public void Paint(MainForm main)
{
Graphics graphics = main.CreateGraphics();
}
}
or the graphics object
class Draw
{
public void Paint(Graphics graphics)
{
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
}
you still need the event handler for the PictureBox, so you would do something like
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Draw image = new Draw();
image.Paint(graphics);
}
Related
I am new with using graphics and I am trying to draw a filled rectangle when the form opens.. but nothing is working and do not know the reason
here is my code :
private void Result_Load(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200,300));
myBrush.Dispose();
formGraphics.Dispose();
}
where Result is my form that is supposed I draw the rectangle on when it is loaded
but when I load the form nothing happens at all
where the problem ?
thanks in advance
Add handler to Paint event in form's constructor:
/// <summary>
/// form constructor
/// </summary>
public frmMain()
{
InitializeComponent();
this.Paint += frmMain_Paint;
}
And create method frmMain_Paint:
void frmMain_Paint(object sender, PaintEventArgs e)
{
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green))
{
e.Graphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
}
}
Tips
Use e.Graphics to draw (not this.CreateGraphics())
Use using keyword as in example above.
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);
}
}
}
The problem isn't that I don't know how to make a border-less form re-sizable, or to how to draw a border. The problem is what happens when you re-size the form with that custom border.
Here is a screenshot, because I don't know how to explain it:
Here is how I created the border (currently):
private void Form1_Paint(object sender, PaintEventArgs e)
{
int width = 1;
Rectangle rec = this.ClientRectangle;
ButtonBorderStyle bbs = ButtonBorderStyle.Solid;
Color clr = Color.Gray;
ControlPaint.DrawBorder(e.Graphics, rec, clr, width, bbs, clr, width, bbs, clr, width, bbs, clr, width, bbs);
}
As for re-sizing a border-less form; I created a repository for the project.
Resize Custom Border - Bitbucket
I don't have any idea as to why this happens, so I wouldn't know where to begin. I just need to draw a border without it doing this. I have tried other ways of drawing one, but the results were the same.
Hopefully this and the repository becomes useful for anyone trying to do the same.
Thank you for taking your time to read if you did.
Try to use Graphics.DrawRectangle instead of DrawBorder
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Single fWidth = 5.0f;
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, fWidth), r);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
Use Graphic library :
Step 1: Override the OnPaint handler for your main form
Step 2: Define a rectangle that covers your current form
Step 3: Draw the defined rectangle
protected override void OnPaint(PaintEventArgs e)
{
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}
You may also implement this using a condition statement like:
this.form.Resize += // some handler1
//in hadler1
{
this.form.Paint += // Your new paint handler2
}
//in handler2
{
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}
i have one picture showing of the human body and i want to use shapes to locate the injuries of the patient. all the shapes will shows off when the user click button. right now im testing with only one shape.
here is my code.
private void button7_Click_4(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
g.DrawImage(img, 0, 0, img.Height, img.Width);
g.Dispose();
}
right now, the shape appear at the back of the image. so how i want to make the shape appear in front of the picture?
Couple of issues.
1) Painting should happen in a paint event. Do not use CreateGraphics since that will only be a temporary drawing.
2) Your DrawImage width and height arguments are reversed.
3) It doesn't look like you are painting the PictureBox control that you have on the form:
private Image img;
public Form1() {
InitializeComponent();
button1.Click += button1_Click;
pictureBox1.Paint += pictureBox1_Paint;
}
void button1_Click(object sender, EventArgs e) {
img = = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
pictureBox1.Invalidate();
}
void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(pictureBox1.BackColor);
if (img != null) {
e.Graphics.DrawImage(img, 0, 0, img.Width, img.Height);
//Draw test shape:
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(10, 10, 20, 60));
}
}
I think you should first get graphics of human image and then draw shape image on it. Some thing like that :
Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
Graphics g = Graphics.FromImage ( img );
g.DrawImage(ShapeImage, 0, 0, 30, 30); // you can set your required x,y,width,height
In my code, let's say I have the PaintObject(Graphics g). In some other function, I want to call the PaintObject function to draw something at an offset, instead of drawing it at (0,0).
I know that in Java, I could use the Graphics.create(x, y, width, height) function to create a copy of my graphics object which I could use, which would draw within those bounds of the original graphics. Is there a way to do something similarly in C#?
Just to give you an example of what my code could look like:
class MyClass : UserControl {
void PaintObject(Graphics g) {
// Example: draw 10x10 rectangle
g.DrawRectangle(new Pen(Color.Black), 0, 0, 10, 10);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
// TODO: Paint object from PaintObject() at offset (50, 50)
}
}
Set a transformation on the Graphics object:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Matrix transformation = new Matrix();
transformation.Translate(50, 50);
g.Transform = transformation;
}
or
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(50, 50);
}
Use the Graphics method
public void TranslateTransform(float dx, float dy)
g.TranslateTransform(dx, dy);
You can use the Graphics.TranslateTransform method:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(50, 50);
PaintObject(e.Graphics);
}