How do you persistantly draw on a windows form picture box control.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martinsmith160
    New Member
    • Dec 2009
    • 16

    How do you persistantly draw on a windows form picture box control.

    Hi all

    I have a picture box placed within a panel so i can scroll across the picture due to it being very wide. My problem is I want to draw an image over the picture box when a user clicks at a desired location. I have this working but when i scroll the panel or minimise the form the drawing is wiped away. Is there a method to draw on the picture box persistantly. Any help would be hugely appreciated because ive been stuck with this problem for weeks.

    All the best,
    Martin
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You will need to maintain what is drawn and have it re-draw it in the paint event

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      ... or...
      Put your background in the Picture.Backgro undImage property
      Put your foreground drawing in the Picture.Image property
      Then draw on the foreground image

      Comment

      • martinsmith160
        New Member
        • Dec 2009
        • 16

        #4
        Hi all

        thanks for the feedback. I tried to draw to a memory bitmap and then draw that bitmap in the picture box's paint event and it builds fine but everytime I launch it i get this exception:

        ************** Exception Text **************
        Code:
        System.ArgumentException: Parameter is not valid.
           at System.Drawing.Graphics.GetHdc()
           at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
           at System.Drawing.BufferedGraphics.Render()
           at System.Windows.Forms.Control.WmPaint(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


        Here is my code so far:

        Code:
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        
        namespace LevelBuilderTool
        {
            public partial class LevelBuilderTool : Form
            {
                int selectedImageIndex = -1;
                Point imagePosition = new Point(0, 0);
                
                //memory bitmap
                private Bitmap bit_DrawSurface;
        
                public LevelBuilderTool()
                {
                    InitializeComponent();
                }
        
                private void btn_move_Click(object sender, EventArgs e)
                {
                    //Stores index of what image to draw
                    selectedImageIndex = this.lst_Objects.SelectedIndex;
                }
        
                private void pcb_ToolWindow_MouseDoubleClick(object sender, MouseEventArgs e)
                {  
                    //draws on the memory bitmap
                    Graphics g = Graphics.FromImage(bit_DrawSurface);
                    g.DrawImage(this.imgLs_Sprites.Images[selectedImageIndex], imagePosition);
                    this.panel1.Invalidate();
                    g.Dispose();
                }
        
                private void LevelBuilderTool_Load(object sender, EventArgs e)
                {
                    //creates a bitmap to draw to the size of the picture box
                    bit_DrawSurface = new Bitmap(this.panel1.ClientRectangle.Width,
                        this.panel1.ClientRectangle.Height,
                        System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        
                    InitializeBitmap();
                }
        
                private void InitializeBitmap()
                {
                    Graphics g = Graphics.FromImage(bit_DrawSurface);
                    g.Clear(SystemColors.Control);
                    g.Dispose();
                }
        
                private void panel1_Paint(object sender, PaintEventArgs e)
                {
                    //draw bitmap
                    //e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
                    //    bit_DrawSurface.Width,
                    //    bit_DrawSurface.Height);
                    //e.Graphics.Dispose();
                }
        
                private void pcb_ToolWindow_MouseMove_1(object sender, MouseEventArgs e)
                {
                    //stores the mouse position
                    this.lbl_xPos.Text = "X: " + e.X.ToString();
                    this.lbl_yPos.Text = "Y: " + e.Y.ToString();
                    imagePosition.X = (int)e.X;
                    imagePosition.Y = (int)e.Y;
                }
        
                private void pcb_ToolWindow_MouseLeave_1(object sender, EventArgs e)
                {
                    //updates labels
                    this.lbl_xPos.Text = "X:";
                    this.lbl_yPos.Text = "Y:";
                }
        
                private void pcb_ToolWindow_Paint(object sender, PaintEventArgs e)
                {
                    //draw bitmap
                    e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
                        bit_DrawSurface.Width,
                        bit_DrawSurface.Height);
                    e.Graphics.Dispose();
                }
            }
        }

        Thank you for any help.
        Last edited by tlhintoq; Dec 28 '09, 11:55 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            thanks for the feedback. I tried to draw to a memory bitmap and then draw that bitmap in the picture box's paint event and it builds fine but everytime I launch it i get this exception:
            Debugging one's programs is a great deal of the process of coding. I suggest you look at things that have not been initialized when used... put in breakpoints and walk through the code line by line (F-10) etc. Yes it is slow and monotonous but that is the job.

            In this case I think you have a issue with objects not being initialized.
            Code:
              private Bitmap bit_DrawSurface;// bit_DrawSurface is currently null
            Code:
                    private void pcb_ToolWindow_Paint(object sender, PaintEventArgs e)
                    {
                        //draw bitmap
                        e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
                            bit_DrawSurface.Width,
                            bit_DrawSurface.Height);
                        e.Graphics.Dispose();
                    }
            See the problem?

            Comment

            • martinsmith160
              New Member
              • Dec 2009
              • 16

              #7
              Hi all

              Ok I looked into drawing on a picture box and found out it is just not designed to be drawn on so I looked into using a panel instead. I placed my image in the background property of the panel but now when i launch the app the background image isnt visible unless i scroll the panel and its only flickering then. Is there a way to get round this, I have heard about double buffering the panel but i have to create a derived custom planel and im not sure how to do it because im new to visual C#. I appreciate your patience with me, thank you very much.

              all the best,
              Martin

              Comment

              • martinsmith160
                New Member
                • Dec 2009
                • 16

                #8
                At tlhintoq:

                The bitmap is initialised in the forms load event.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  Originally posted by Martin
                  I looked into drawing on a picture box and found out it is just not designed to be drawn on
                  Nobody said draw on the picturebox. It was suggested you have a background bitmap and a foreground bitmap and that you draw on (alter/update/re-create) the foreground bitmap.
                  Originally posted by tlhIntoq
                  ... or...
                  Put your background in the Picture.Backgro undImage property
                  Put your foreground drawing in the Picture.Image property
                  Then draw on the foreground image
                  Going back to the most recent issue you reported: Did you see the problem I pointed out causing your exception error? You seemed to be quite close at that point

                  Comment

                  Working...