Deleting graphics - c#

I am trying to delete all the graphics on a group box when pressing a button, but I cannot figure it out how. Adding a new background to the group box seems slow, I want to make it faster.
Moreover, I've read about the Invalidate() method, but it doesn't do what I need.
The piece of code I am talking about :
private void button1_Click(object sender, EventArgs e)
{
groupBox0.Invalidate();
}
private void groupBox0_Paint(object sender, PaintEventArgs e)
{
Graphics graphObj = groupBox0.CreateGraphics();
Pen mypen = new Pen(System.Drawing.Color.Red, 2);
graphObj.DrawRectangle(mypen, 0, 0, 10, 10);
}
When I run the code it does nothing, the graphics are still there. What can I do or change ?

Related

Drawing Graphics not working on Form load event

private void DrawIt()
{
System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
50, 50, 150, 150);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}
private void Form1_Load(object sender, EventArgs e)
{
DrawIt();
}
private void button1_Click(object sender, EventArgs e)
{
DrawIt();
}
when putting the 'DrawIt' method in a button event it works, but in a form load event it doesn't, why?
Change Load event to Paint.
If you want redraw your form use this.Refresh();
When you are in Paint method use:
private void mForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(...);
}
The Load event is run before the Form is drawn. So anything you draw is overwritten by the Form.
Call your DrawIt method from an event which fires after the Form has loaded.
You need to an event to trigger DrawIt(). You can use a panel or sth else. Then write an OnClik event. Your drawing will start after a click.
if vb6 just add call to your sub or function in Form_Activate
Instead of using Form_Load event use Form_Shown event. That should work.

Can't get DrawLine to work when triggered by a button press.

this code works just fine when it's in a different delegate, but doesn't work when it's triggered by a button. "Draw_Click" is a button called "Draw" and it's supposed to make a drawing on the screen based on a pre-made array of points.
private void Draw_Click(object sender, EventArgs e)
{
Graphics myInstance = this.CreateGraphics();
for (int t = 0; t <= numberOfPoints - 1; t++)
{
myInstance.DrawLine(pen, spiroArray[0, t], spiroArray[1, t], spiroArray[0, t + 1], spiroArray[1, t + 1]);
}
}
But when I have this same code in this class, it works fine (minus the myInstance line).
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
I use "e" instead of "myInstance" just to make this question as brief as possible.
Basically, what's so special about Form1_Paint? Shouldn't I be able to make it draw something even outside that special Form1Paint class? If I use that Form1_Paint, I don't have control over when it does the drawing. I'm basically trying to have it update the drawing as the user changes parameters, etc.
The error is "Error 1 The name 'DrawLine' does not exist in the current context"
Thanks for any help.
I am not sure I am getting your question correctly, because if I read your question and code the way I think, the error you should be getting is that "pen" does not exist in the current context. If you use that same code to draw, it will work on either the button_click or the form_paint events. "this" will still refer to the form, and using it to instantiate a graphics object will give you access to draw anywhere in the form. From either the button click handler or the form paint method. However, the only think I can see that would fail is if you never defined the pen. Here is the code I used, and it drew a red line when the form first came up, and then drew a black line when I clicked on the button I had put on the same form.
private void button1_Click(object sender, EventArgs e)
{
Graphics myInstance = this.CreateGraphics();
myInstance.DrawLine(Pens.Black, 15, 10, 25, 20);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics myInstance = this.CreateGraphics();
myInstance.DrawLine(Pens.Red, 10, 10, 20, 20);
}
There has to be something that is not coming across right in the statement of your question.

Override paint method doesn't work correctly

I am trying to draw a rectangle on PictureBox without using Paint event of picturebox,So i override the Onpaint method :
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics dc = pictureBox1.CreateGraphics();
Pen bPen=new Pen(Color.Blue,3);
dc.DrawRectangle(bPen,0,0,50,50);
}
}
But the rectangle doesn't appear in first time ,but when i change the size of form using mouse my rectangle is appeared why ?!!!
I understand that you do not want to use the Paint event of the PictureBox. Your code wouldn't work since the form gets rendered before its containing elements.
I offer you a solution: draw on a bitmap and then insert that bitmap into the PictureBox through its Image public member.
private void loadDrawing(){
Bitmap map = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
var graph = Graphics.FromImage(map);
graph.DrawRectangle(new Pen(Color.Blue, 3), 0, 0, 50, 50);
pictureBox1.Image = map;
}
Let's say you want to make the Rectangle to show up upon load:
private void Form1_Load(object sender, EventArgs e)
{
loadDrawing();
}
The problem is that you override OnPaint method of the form instead of Paint event of the PictureBox. Form's OnPaint happens, when the form needs repainting, and that's independent of what happens with the PictureBox.
Implement OnPaint event of the PictureBox and then you will not have to create Graphics object manually - simply use one provided in the event arguments.
private void Form1_Load(object sender, EventArgs e)
{
// No need to do that
// pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(10, 10, 20, 20));
}
Edit: (in response to comments)
If you want to update the paintbox periodically, do the following:
Keep the data required to draw the scene somewhere in the form, possibly as a private field
Use these data to draw the scene in Paint event of the PictureBox
When you need to update the scene, modify the data accordingly, and then call the Invalidate method of the PictureBox. It will cause the Paint event to fire and the scene will be redrawn.
Remember though, that all calls to UI methods from the threads has to be synchronized to the main UI thread (otherwise they won't work or cause problems).
You are overriding the paint method of the form, while you paint the PictureBox. That isn't how it is meant to be. The PictureBox will still do it's own rendering.
If you really want to do painting of the PictureBox, implement the Paint event of the PictureBox or create a custom control where you draw a rectangle, and a picture.
After you draw the form refresh the form using this :-
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics dc = pictureBox1.CreateGraphics();
Pen bPen=new Pen(Color.Blue,3);
dc.DrawRectangle(bPen,0,0,50,50);
this.Refresh() ;
}

DrawToBitmap on Panel is blank

So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Size oldSize = printData.Size;
printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
InvertZOrderOfControls(printData.Controls);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
InvertZOrderOfControls(printData.Controls);
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
bitmap.Save(#"C:\Users\jdudley\Documents\File.bmp");
printData.Size = oldSize;
}
Following this advice of this thread inverted the Z-Order of the controls but it didn't change anything.
The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
Edit: This is in the context of printing but I have not issue with printing what so ever. My error is in creating the bitmap. The save line I added proves this because it creates a blank bitmap file.
Change your entire event to this
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
Edit
This is my whole Project. I created a panel named printData and I added two buttons, I attached an event to button1.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
}
private void button1_Click(object sender, EventArgs e)
{
pd.Print();
}
}
}
You have to try this and see if it works, or else I wont be able to sleep tonight!!

Problem with e.Graphics.DrawImage()

This is my code and when I call this code from localhost, printer worked,but from anoder
IP noting.I think DrawImage function not worked.Who can help me.I check the Bitmap object properly created and Image exist.
protected void printButton__Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument document =
new System.Drawing.Printing.PrintDocument();
document.PrintPage += new PrintPageEventHandler(document_PrintPage);
document.Print();
}
void document_PrintPage(object sender, PrintPageEventArgs e)
{
string s = Server.MapPath("Temp.jpg");
Bitmap objBmpImage = new Bitmap(s);
e.Graphics.DrawImage( objBmpImage, 10, 10, 200, 100);
}
It seems you are in asp.net environment (Server.MapPath) & are assuming that you can get hold of a printer on the client machine.
If you are trying to do that, it cannot be done.
On the other hand, you can't use Server.MapPath in a winforms environment.

Categories