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
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
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
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 ************** - 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: - 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.
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. 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. - private Bitmap bit_DrawSurface;// bit_DrawSurface is currently null
-
- 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?
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
At tlhintoq:
The bitmap is initialised in the forms load event.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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:
|
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
|
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 ....
|
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
| |
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...
|
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.
|
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
{
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |