By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,660 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,660 IT Pros & Developers. It's quick & easy.

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

P: 16
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
Dec 28 '09 #1
Share this Question
Share on Google+
8 Replies


Plater
Expert 5K+
P: 7,872
You will need to maintain what is drawn and have it re-draw it in the paint event
Dec 28 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
... or...
Put your background in the Picture.BackgroundImage property
Put your foreground drawing in the Picture.Image property
Then draw on the foreground image
Dec 28 '09 #3

P: 16
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 **************
Expand|Select|Wrap|Line Numbers
  1. System.ArgumentException: Parameter is not valid.
  2.    at System.Drawing.Graphics.GetHdc()
  3.    at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
  4.    at System.Drawing.BufferedGraphics.Render()
  5.    at System.Windows.Forms.Control.WmPaint(Message& m)
  6.    at System.Windows.Forms.Control.WndProc(Message& m)
  7.    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  8.    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  9.    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace LevelBuilderTool
  11. {
  12.     public partial class LevelBuilderTool : Form
  13.     {
  14.         int selectedImageIndex = -1;
  15.         Point imagePosition = new Point(0, 0);
  16.  
  17.         //memory bitmap
  18.         private Bitmap bit_DrawSurface;
  19.  
  20.         public LevelBuilderTool()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void btn_move_Click(object sender, EventArgs e)
  26.         {
  27.             //Stores index of what image to draw
  28.             selectedImageIndex = this.lst_Objects.SelectedIndex;
  29.         }
  30.  
  31.         private void pcb_ToolWindow_MouseDoubleClick(object sender, MouseEventArgs e)
  32.         {  
  33.             //draws on the memory bitmap
  34.             Graphics g = Graphics.FromImage(bit_DrawSurface);
  35.             g.DrawImage(this.imgLs_Sprites.Images[selectedImageIndex], imagePosition);
  36.             this.panel1.Invalidate();
  37.             g.Dispose();
  38.         }
  39.  
  40.         private void LevelBuilderTool_Load(object sender, EventArgs e)
  41.         {
  42.             //creates a bitmap to draw to the size of the picture box
  43.             bit_DrawSurface = new Bitmap(this.panel1.ClientRectangle.Width,
  44.                 this.panel1.ClientRectangle.Height,
  45.                 System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  46.  
  47.             InitializeBitmap();
  48.         }
  49.  
  50.         private void InitializeBitmap()
  51.         {
  52.             Graphics g = Graphics.FromImage(bit_DrawSurface);
  53.             g.Clear(SystemColors.Control);
  54.             g.Dispose();
  55.         }
  56.  
  57.         private void panel1_Paint(object sender, PaintEventArgs e)
  58.         {
  59.             //draw bitmap
  60.             //e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
  61.             //    bit_DrawSurface.Width,
  62.             //    bit_DrawSurface.Height);
  63.             //e.Graphics.Dispose();
  64.         }
  65.  
  66.         private void pcb_ToolWindow_MouseMove_1(object sender, MouseEventArgs e)
  67.         {
  68.             //stores the mouse position
  69.             this.lbl_xPos.Text = "X: " + e.X.ToString();
  70.             this.lbl_yPos.Text = "Y: " + e.Y.ToString();
  71.             imagePosition.X = (int)e.X;
  72.             imagePosition.Y = (int)e.Y;
  73.         }
  74.  
  75.         private void pcb_ToolWindow_MouseLeave_1(object sender, EventArgs e)
  76.         {
  77.             //updates labels
  78.             this.lbl_xPos.Text = "X:";
  79.             this.lbl_yPos.Text = "Y:";
  80.         }
  81.  
  82.         private void pcb_ToolWindow_Paint(object sender, PaintEventArgs e)
  83.         {
  84.             //draw bitmap
  85.             e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
  86.                 bit_DrawSurface.Width,
  87.                 bit_DrawSurface.Height);
  88.             e.Graphics.Dispose();
  89.         }
  90.     }
  91. }

Thank you for any help.
Dec 28 '09 #4

tlhintoq
Expert 2.5K+
P: 3,525
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.
Dec 28 '09 #5

tlhintoq
Expert 2.5K+
P: 3,525
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.
Expand|Select|Wrap|Line Numbers
  1.   private Bitmap bit_DrawSurface;// bit_DrawSurface is currently null
  2.  
Expand|Select|Wrap|Line Numbers
  1.         private void pcb_ToolWindow_Paint(object sender, PaintEventArgs e)
  2.         {
  3.             //draw bitmap
  4.             e.Graphics.DrawImage(bit_DrawSurface, 0, 0,
  5.                 bit_DrawSurface.Width,
  6.                 bit_DrawSurface.Height);
  7.             e.Graphics.Dispose();
  8.         }
  9.  
See the problem?
Dec 29 '09 #6

P: 16
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
Dec 29 '09 #7

P: 16
At tlhintoq:

The bitmap is initialised in the forms load event.
Dec 29 '09 #8

tlhintoq
Expert 2.5K+
P: 3,525
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.
... or...
Put your background in the Picture.BackgroundImage 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
Dec 29 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.