Results 1 to 4 of 4

Thread: Problem in Graphics using C#

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    14

    Problem in Graphics using C#

    i am trying to create "PaintBrush" short of project.... but after creating a line/circle/rectangle, when i want to create another line/circle/rectangle.. then it removes my last line/circle/rectangle......

    here is my code.......
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

    namespace Testing_Project
    {
    /// <summary>
    /// Summary description for DrawForm.
    /// </summary>
    public class DrawForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Panel pnlTool;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ImageList imageList1;
    private System.ComponentModel.IContainer components;
    private Graphics gB;
    private Pen pen;
    private Bitmap bitmap;
    private int x0, y0, x, y;
    private int cx, cy;
    private bool blDrag = false;
    private System.Windows.Forms.Label lblMsg;
    private int DrawMode=1;

    public DrawForm()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DrawForm));
    this.pnlTool = new System.Windows.Forms.Panel();
    this.button3 = new System.Windows.Forms.Button();
    this.imageList1 = new System.Windows.Forms.ImageList(this.components);
    this.button2 = new System.Windows.Forms.Button();
    this.button1 = new System.Windows.Forms.Button();
    this.lblMsg = new System.Windows.Forms.Label();
    this.pnlTool.SuspendLayout();
    this.SuspendLayout();
    //
    // pnlTool
    //
    this.pnlTool.BackColor = System.Drawing.SystemColors.Control;
    this.pnlTool.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.pnlTool.Controls.Add(this.button3);
    this.pnlTool.Controls.Add(this.button2);
    this.pnlTool.Controls.Add(this.button1);
    this.pnlTool.Location = new System.Drawing.Point(0, 0);
    this.pnlTool.Name = "pnlTool";
    this.pnlTool.Size = new System.Drawing.Size(72, 368);
    this.pnlTool.TabIndex = 0;
    //
    // button3
    //
    this.button3.BackColor = System.Drawing.Color.FloralWhite;
    this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button3.ImageIndex = 2;
    this.button3.ImageList = this.imageList1;
    this.button3.Location = new System.Drawing.Point(14, 72);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(40, 21);
    this.button3.TabIndex = 3;
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // imageList1
    //
    this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
    this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
    this.imageList1.TransparentColor = System.Drawing.Color.Silver;
    //
    // button2
    //
    this.button2.BackColor = System.Drawing.Color.FloralWhite;
    this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button2.ImageIndex = 1;
    this.button2.ImageList = this.imageList1;
    this.button2.Location = new System.Drawing.Point(14, 48);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(40, 21);
    this.button2.TabIndex = 2;
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button1
    //
    this.button1.BackColor = System.Drawing.Color.FloralWhite;
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.ImageIndex = 0;
    this.button1.ImageList = this.imageList1;
    this.button1.Location = new System.Drawing.Point(14, 24);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(40, 21);
    this.button1.TabIndex = 1;
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // lblMsg
    //
    this.lblMsg.Location = new System.Drawing.Point(72, 344);
    this.lblMsg.Name = "lblMsg";
    this.lblMsg.Size = new System.Drawing.Size(424, 16);
    this.lblMsg.TabIndex = 1;
    //
    // DrawForm
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
    this.ClientSize = new System.Drawing.Size(504, 365);
    this.Controls.Add(this.lblMsg);
    this.Controls.Add(this.pnlTool);
    this.Name = "DrawForm";
    this.Text = "DrawForm";
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawForm_MouseDown);
    this.Load += new System.EventHandler(this.DrawForm_Load);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawForm_MouseUp);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawForm_Paint);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DrawForm_MouseMove);
    this.pnlTool.ResumeLayout(false);
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>

    [STAThread]
    static void Main()
    {
    Application.Run(new DrawForm());
    }

    private void DrawForm_Load(object sender, System.EventArgs e)
    {
    Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize;
    bitmap = new Bitmap(size.Width, size.Height);
    gB = Graphics.FromImage(bitmap);
    // Color bgColor=this.BackColor;
    // gB.Clear(bgColor);
    // gB.Dispose();
    }

    private void DrawForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    Point p=new Point(e.X,e.Y);
    //p.X=e.X;
    //p.Y=e.Y;
    x0=p.X;
    y0=p.Y;
    blDrag = true;
    }

    private void DrawForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    Point p=new Point(e.X,e.Y);
    //p.X=e.X;
    //p.Y=e.Y;
    x=p.X;
    y=p.Y;
    cx=x-x0;
    cy=y-y0;
    if(blDrag)
    {
    Invalidate();
    }
    }

    private void DrawForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    cx=x-x0;
    cy=y-y0;
    //pen=new Pen(Color.Blue);
    //try
    //{
    // if (DrawMode==1)
    // {
    // gB.DrawLine(pen,x0,y0,x,y);
    // }
    blDrag = false;
    // pen.Dispose();
    //}
    //catch(Exception er)
    //{
    // lblMsg.Text = er.Message +"//" + er.Source;
    //}
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    DrawMode=1;
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    DrawMode=2;
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
    DrawMode=3;
    }

    private void DrawForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    gB=e.Graphics;
    pen=new Pen(Color.Blue);
    if (blDrag)
    {
    if (DrawMode==1)
    {
    gB.DrawLine(pen,x0,y0,x,y);
    }
    else if (DrawMode==2)
    {
    gB.DrawEllipse(pen, x0, y0, cx, cy);
    }
    else if (DrawMode==3)
    {
    gB.DrawRectangle(pen, x0, y0, cx, cy);
    }
    }
    pen.Dispose();
    }
    }
    }


    do you have any suggestion/code to help me out.......

  2. #2
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: Problem in Graphics using C#

    I'm not positive, this is just a guess. In your constructor you have a bunch of SetStyle method calls. I'm thinking one of those is causing the shapes you draw to not be saved by windows. I'm guessing you've made the program think you will redraw each shape yourself when it calls the Paint event.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem in Graphics using C#

    Everything you've previously drawn gets erased every time the Paint event is raised. Anything you want to keep you will need to store in class-level variables so that you can redraw it every time the Paint event is raised. One advnatge of this is that at any time you can erase one paricular shape by simply removing it from the list of things to redraw. If you want something to be permanent then you need to create a Bitmap object, create a Graphics object from that and then draw on the Bitmap. Usually for a drawing program you would use a PictureBox as the background and do your GDI+ drawing on the PictureBox. When the user saves their work you would recreate the same drawing on the Image contained in the PictureBox to make it permanent and then clear all your class level variables that contain the objects to be drawn on the Paint event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: Problem in Graphics using C#

    Lol, the Paint event, of course. What was I thinking? I completely forgot the meaning of the Paint event!
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width