Results 1 to 5 of 5

Thread: How to draw picture via coding

  1. #1
    Join Date
    Nov 2009
    Posts
    33

    How to draw picture via coding

    I want to draw a picture. This is the first time I set foot in this area (more used to SQL and co).

    So I see 2, 3 stuff on the net ... But ... (yes there is always a but ...)

    I use the following code:
    Code:
    public Form1()
            {
                InitializeComponent();
     
                bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                //bmp2 = new Bitmap(pictureBox2.Width, pictureBox2.Height);
                des = Graphics.FromImage(bmp);
                des2 = pictureBox2.CreateGraphics();
                
            }
            private void button1_Click(object sender, EventArgs e)
            {
                //routine design
                des.FillRectangle(new SolidBrush(Color.DarkCyan), new Rectangle(new Point(10, 10), new Size(50, 50)));
                pictureBox1.Image = bmp;
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                //routine design
                des2.FillRectangle(new SolidBrush(Color.DarkCyan), new Rectangle(new Point(10, 10), new Size(50, 50)));
                //pictureBox2.Image = bmp2;
            }
    In the case of the picture box 1, I can not define the background as my PictureBox method replaces.

    In the case of the picture box 2, I can define the background and graphic elements will come to position above. What I want!

    But the problem is at a displacement of the window offscreen, or redirect, the Element graphic picture box 1 continues, then that element of the picture box 2 disappears ..

    I grant you that it's probably a matter maternal 2nd year, but how to draw on my background without graphic element that disappears when handling the window?

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to draw picture via coding

    Answer 1: By making the draw in the OnPaint() component

    Answer 2: This kind of question is "often" put on this forum, I invite you to research that will explain all good practices to achieve what you want you have done. It seems to me a tutorial on the UserControl explicit all the information you need for your creation!

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to draw picture via coding

    Or we get the Paint event since you are in form and not in the component. Above all use the Graphics provided by the argument of the function.

    Code:
    protected override void OnPaint(PaintEventArgs e) 
    { 
        // Recover graphics
        Graphics gfx = e.Graphics;
    }
    Code:
    // Normally generate by designer
    pictureBox1.Paint +=  new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
     
    // Recuperation of the event
    protected override void PictureBox1_Paint(Object Sender,PaintEventArgs e) 
    { 
        // Recover graphics
        Graphics gfx = e.Graphics;
    }
    Also avoid declaring an object like field graphics class.

    In GDI+, your best friends are Using and Dispose which can release the resources that are not needed.

    Not those needing to gfx.dispose (), it will automatically as it is declared in function.

  4. #4
    Join Date
    Nov 2009
    Posts
    33

    Re: How to draw picture via coding

    I'll see it.

    What I wish the finally that just add a point on an existing image .. one that will apply broadly after a search we show the location of the item on a map (plan). It look like X, and it shows on our map (picture) or is placed item X.

    There's easier, faster, why? or no ...

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to draw picture via coding

    If it is to put one point (or ten, but not type 10000)

    Code:
    Bitmap img = new Bitmap("c:\...\img.bmp");
    img.SetPixel ( 100,50,Color.Red);
    This relatively slow downs approach to the Lock/ Unlock but as the number of points is limited, it will be done by one hundredth of a second

Similar Threads

  1. Replies: 4
    Last Post: 07-10-2011, 12:24 AM
  2. Replies: 2
    Last Post: 09-01-2011, 05:07 PM
  3. Help me to use the transform() in C++ coding
    By Tailor in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 04:17 PM
  4. Replies: 2
    Last Post: 10-03-2009, 11:25 PM
  5. Replies: 1
    Last Post: 11-04-2008, 05:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,287,829.33683 seconds with 16 queries