-
December 13th, 2010, 02:48 AM
#1
c# Drawing a Line in picturebox..weird
Okay I am trying to draw a horzontal and vertical line through a point on the map.
Code:
public partial class PaintBox : System.Windows.Forms.PictureBox
{
public PaintBox()
{
this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.UserPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
InitializeComponent();
}
public PaintBox(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
{
System.Drawing.Pen mypen;
mypen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics gra = this.CreateGraphics();
gra.DrawLine(mypen, 0, Program.mainForm.CurrentPoint.Y, this.Width, Program.mainForm.CurrentPoint.Y);
gra.DrawLine(mypen, Program.mainForm.CurrentPoint.X, 0, Program.mainForm.CurrentPoint.X, this.Height);
mypen.Dispose();
gra.Dispose();
base.OnPaint(pe);
}
}
I call an invalidate via a button link which passes the coordinates to the global points and displays the line although here is the problem. (I have a giant 6000 by 5500 picturebox with a map in it, located inside a panel and i am utilizing the scroll bars) As soon as I click the scrollbar to shift the picture the line disappears, although if I click the scroll bars fast you can see the lines properly but as soon as I stop clicking it disappears again.
I have absoulutely no Idea whats going on seing how I created a new project copied and pasted the same code and it works fine...any ideas?
-
December 13th, 2010, 03:17 AM
#2
Re: c# Drawing a Line in picturebox..weird
you actually don't need a new graphics object. you can utilize the one you get via pe.Graphics, try it. and I think you should call base.OnPaint(pe) before you draw the line, it is probably covered by the image that comes later.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
December 13th, 2010, 10:13 AM
#3
Re: c# Drawing a Line in picturebox..weird
I tried that, pe.Graphics actually crashes my program with an object reference not set to an instance of an object, and having the base.onPaint before no graphics are visible at all
-
December 13th, 2010, 01:24 PM
#4
Re: c# Drawing a Line in picturebox..weird
Originally Posted by Mastermosley
I tried that, pe.Graphics actually crashes my program with an object reference not set to an instance of an object
this is quite weird
would it be possible to upload the project?
Last edited by memeloo; December 13th, 2010 at 01:27 PM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
December 13th, 2010, 06:37 PM
#5
Re: c# Drawing a Line in picturebox..weird
My bad, I gave you the wrong error, I all my unhandled errors get logged here was the error for using pe.graphics
<Exception time="13/12/2010 7:10:41 AM">
<IsUnhandledEx>YES</IsUnhandledEx>
<Is64Bit>True</Is64Bit>
<CLR_Version>v2.0.50727</CLR_Version>
<Operating_System>Microsoft Windows NT 6.1.7600.0</Operating_System>
<CPU_Type>x86</CPU_Type>
<Error_Message>Parameter is not valid.</Error_Message>
<Stack_Trace> 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)</Stack_Trace>
<Data>System.Collections.ListDictionaryInternal</Data>
<Target_Site>IntPtr GetHdc()</Target_Site>
<Source>System.Drawing</Source>
</Exception>
Ill try and figure something out to upload, my project is very large and I use quite a few custom controls
Last edited by Mastermosley; December 13th, 2010 at 06:39 PM.
-
December 13th, 2010, 06:49 PM
#6
Re: c# Drawing a Line in picturebox..weird
Well, that is a very different error. This says that you are doing something wrong, the Graphics object is not null and you should not be calling CreateGraphics. You were likely calling Dispose() on pe.Graphics and then passing it to base.OnPaint. How would you like it if I passed you a disposed Graphics object? You don't need to dispose the Graphics object passed to you in OnPaint. That is handled elsewhere and is not your concern.
-
December 13th, 2010, 07:10 PM
#7
Re: c# Drawing a Line in picturebox..weird
You are correct that is what caused the pe.Graphics error but regardless this actually doesn't draw the lines at all.
Code:
System.Drawing.Pen mypen;
mypen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics gra = pe.Graphics;
gra.DrawLine(mypen, 0, Program.mainForm.CurrentPoint.Y, this.Width, Program.mainForm.CurrentPoint.Y);
gra.DrawLine(mypen, Program.mainForm.CurrentPoint.X, 0, Program.mainForm.CurrentPoint.X, this.Height);
mypen.Dispose();
base.OnPaint(pe);
Off topic from above but my actuall problem. The initial line gets drawn I can see it fine as soon as I move it it disappears, but it is actually still there because if I hold the scroll bar and move the image down fast enough you can see it flickering and it doesnt disappear untill I come to a stop, its like its on a different layer behind the map
-
December 13th, 2010, 07:21 PM
#8
Re: c# Drawing a Line in picturebox..weird
Ok, next on the list to fix are these two lines. They are not doing what you think they are:
Code:
gra.DrawLine(mypen, 0, Program.mainForm.CurrentPoint.Y, this.Width, Program.mainForm.CurrentPoint.Y);
gra.DrawLine(mypen, Program.mainForm.CurrentPoint.X, 0, Program.mainForm.CurrentPoint.X, this.Height);
Specifically, Program.mainForm.CurrentPoint. When you draw you are drawing in client coordinates, that is to say, the canvas has an origin of (0,0) at the top left. This means that everything is relative to your control/canvas location, so you don't need to know where the form is on the screen. Change it to use relative coordinates. I would suggest the code for you, but your example leaves me with no idea of what you are trying to draw.
You should also get familiar with the 'using' syntax for objects that implement IDisposable. instead of writing:
Code:
mypen = new Pen(System.Drawing.Color.Red);
// use mypen
mypen.Dispose();
You can simply write:
Code:
using( mypen = new Pen(System.Drawing.Color.Red) )
{
// use mypen
} // Dispose() called here when the scope exits
This is simply syntactical sugar for this:
Code:
mypen = new Pen(System.Drawing.Color.Red);
try
{
// use mypen
}
finally
{
mypen.Dispose();
}
You can see that this also means that, should an exception occur in the try block, the Pen is still disposed of. Your current code lacks this safety.
You can also 'stack' using statements like so:
Code:
using( mypen = new Pen(System.Drawing.Color.Red) )
using( myfont = new Font( ... ) )
{
} // yay, font and pen disposed with no extra work from you
-
December 13th, 2010, 07:27 PM
#9
Re: c# Drawing a Line in picturebox..weird
I really dont understand this at all but anyways its solved. I deleted my custom control, used the paint event for the picturebox and used this.
Code:
System.Drawing.Pen mypen;
mypen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics gra = e.Graphics;
gra.DrawLine(mypen, 0, Program.mainForm.CurrentPoint.Y, picMap.Width, Program.mainForm.CurrentPoint.Y);
gra.DrawLine(mypen, Program.mainForm.CurrentPoint.X, 0, Program.mainForm.CurrentPoint.X, picMap.Height);
mypen.Dispose();
I dont no why this works but CreateGraphics doesn't work in this project because the exact same code works in a different project. Anyways thanks for your guys help.
-
December 13th, 2010, 07:42 PM
#10
Re: c# Drawing a Line in picturebox..weird
ok, two problems:
You are still using Program.mainform.CurrentPoint. Whatever that is actually returning, it is bad, non-standard, and confusing. Drawing is relative to the boundaries of the control, there is no need for anything like this.
You also say you have no idea why it works, but you don't seem to care. This is about the worst attitude you can have and will almost guarantee that you never will become a competent programmer. Don't be a Cargo Cult Programmer! Learn what you are doing or move on to another profession. there is no point in wasting your time like this.
-
December 13th, 2010, 10:59 PM
#11
Re: c# Drawing a Line in picturebox..weird
It is like that for a reason, I have a bunch of xml coordinates located in an xml database that use coordinates. I have to convert these coordinates to ones that are sychronized to my map thus thats what I use. The Program.mainForm.... was like that because it was located in an outside class and my mainForm is static so I can pass data easily between other forms, it is changed simpy to CurrentPoint.X. You are misinterpretting these coordinates as the form coordinates but that is not so.
And I do no why it works code wise, I simply do not understand why the other code did not because it worked in a different project using the same controls and same settings. You dont need to tell me that I am wasting my time, I do not call this wasting my time.
Last edited by Mastermosley; December 13th, 2010 at 11:05 PM.
-
December 14th, 2010, 01:53 PM
#12
Re: c# Drawing a Line in picturebox..weird
CreateGraphics creates a new device context. the point is, inside the draw method you already have a device context, and so you're re-recreating what you already have. creating a second dc would cause you to have to redraw a section of the screen (which you're already in the middle of inside your OnPaint method) and so you're overwriting what you're creating by handling the default paint implementation. make sense? Create graphics is for drawing outside of the built in painting plumbing where you have to create your dc. overriding onpaint and using the graphics dc created by the windowing subsystem is the better default mechanism, so that's why this method works.
-
December 14th, 2010, 06:13 PM
#13
Re: c# Drawing a Line in picturebox..weird
Thanks MadHatter, I find it really weird though how the CreateGraphics didnt work in my main application but works fine in a new project using those controls. I am not complaining though, I have learned a fair bit from this ordeal, Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|