473,672 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

16 New Member
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
8 3821
Plater
7,872 Recognized Expert Expert
You will need to maintain what is drawn and have it re-draw it in the paint event
Dec 28 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
... 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
Dec 28 '09 #3
martinsmith160
16 New Member
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
3,525 Recognized Expert Specialist
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
3,525 Recognized Expert Specialist
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
martinsmith160
16 New Member
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
martinsmith160
16 New Member
At tlhintoq:

The bitmap is initialised in the forms load event.
Dec 29 '09 #8
tlhintoq
3,525 Recognized Expert Specialist
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.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
Dec 29 '09 #9

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

Similar topics

1
9700
by: Dennis | last post by:
Hello, Ive to draw a line on a picture i used this code: Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) x1 = z.X y1 = z.Y lbl3 = "x1: " & x1 lbl4 = "y1: " & y1
3
4246
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
11
5288
by: Crirus | last post by:
I need to derive the Windows.Forms.Control 2 times so I design a class like this Public Class BMControl Inherits System.Windows.Forms.UserControl Public Class MapControl Inherits BMControl
1
3759
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw the rectangle using the right mouse button to represent the area of the chart they want to zoom on. I haev been able to draw the rectangle on a blank form but I cannot get it to draw on top of other controls. I have pasted in the code i am using ....
8
2765
by: George | last post by:
Hello everyone, I am using C# on a Pocket PC 2003 project based on .Net Compact Framework of Visual Studio 2005. I want to re-draw some controls of a Form (Window) at a regular interval (for example, change the title of some Label or something similar). The issues I met with are, 1. My application has several Forms/Windows. How to check whether the specific Form/Window (which I want to re-draw) is active? If the Form/Window
9
4019
by: zhaow | last post by:
Hi, All Greetings! I want to develop as appllication that requires a line-drawing function in the blank area between two forms. I have looked up the MSDN, it says that a graphics object need a reference to a control or a form. I guess it means that lines can't be draw on blank area between two forms. Can anybody guarantee this for me? Is there any method can realize this function? I mainly want to draw a line from a button in form1 to...
4
2419
by: annsh | last post by:
Hi On a windows form - I want to be able to allow a user to draw an image (on a graphics tablet). Its for an application for selling carpets - customer comes in and describes room where carpet will be laid - user wants to transfer this image to application so it can be printed for the fitter at a later stage. Thanks for any help ann.
4
4630
by: martinsmith160 | last post by:
Hi Everyone I am creating a level bulider using windows forms and the background for the level is stored in a picture box control. All i want to do is draw a number of lines over the image to create a grid allowing better placement on the builder. here is my code so far. It draws a single line but if i place a control over it, it is drawn over. namespace GraphicsTest {
1
5593
by: martinsmith160 | last post by:
Hi all I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is working fine. My aim is to double click the picture box and the desired image will be drawn at the mouse position. This works fine unless I scroll or minimise the form because the image isnt repainted after movement. I looked up drawing the image to...
0
8508
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8428
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8704
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7484
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2849
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.