Graphics Issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a windows forms, on which I added a control (panel)
I am creating some graphics objects like lines and rectangles on this panel, through its OnPaint event
The form draws all the graphics at the form load, but when the user resizes the form, all the drawings (both lines and rectangles on the panel) get distorted (rendering is not smooth)
I even included the graphics.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQualit
but this was a useless thing as it didn't have any effect
Is there a way to work this thing out, also i want to ask, if there is a way to get the (lines and drawings made only at the form load or when the form paints for the first time) and the contents remain there during a resize or scroll or minimize. Because i am only making the drawings at the start
With Regard
Sunny
 
Hi Sunny,

Have you tried

graphics.SmoothingMode = SmoothingMode.AntiAlias

It should be good..

Another thing to try is to trigger the OnPaint() when resizing by overriding
the form's OnResize event handler

protected override void OnResize( System.EventArgs e )
{
this.Invalidate();
base.OnResize( e );
}

About the "one-time" drawing you can draw the lines when the form loads but
I wont promise that the lines stay on resize... For that test you will need
some code in your form onload event handler looking something like this:

Graphics gfx = panel1.CreateGraphics();
gfx.DrawLine( pen, x1, y1, x2, y2 );

I hope it helps you!

Best Regards,

Robin Theilade


Sunny said:
Hi,
I have a windows forms, on which I added a control (panel).
I am creating some graphics objects like lines and rectangles on this
panel, through its OnPaint event.
The form draws all the graphics at the form load, but when the user
resizes the form, all the drawings (both lines and rectangles on the panel)
get distorted (rendering is not smooth).
I even included the graphics.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
but this was a useless thing as it didn't have any effect.
Is there a way to work this thing out, also i want to ask, if there is a
way to get the (lines and drawings made only at the form load or when the
form paints for the first time) and the contents remain there during a
resize or scroll or minimize. Because i am only making the drawings at the
start.
 
Hi Sunny,

1)
When you override (not attaching a handler to the Paint event) the OnPaint
method, your OnPaint definition will be executed instead of the class'
default method definition, unless you call the base/parent class' OnPaint
method (base.OnPaint(e);). To this it will require you to inherit the Panel
class and build your own Panel class. This way you will get more control
over the panel.

2)
What kind of flicker are you talking about here? Pixelated or real flicker
like flipping thousands of papers to show an animation?

3)
Just out of curiosity, what programming language did you use before this?

Best Regards,

Robin Theilade

Sunny said:
Thanks for the detials,
actually i have seen this override of the base methods in a number of
examples on MSDN, but i still cannot figure out how this implementation
works, and how this avoids the flicker and makes smooth rendering.
See i have a windows form and on that form there is a Panel that I am
using as a Canvas for drawing shapes like rectangles and lines, arrows ....
And all these drawings that i am making i have created them in the
Panel_Paint(), also the resize of this panel is empty i mean there is no
code in that one. Now what do i need to do here to avoid these lines to not
get messed up when the user resizes or scrolls the panel.
Graphics g;
private void p1_Paint_1(object sender, System.Windows.Forms.PaintEventArgs e)
{
g = e.Graphics;
g.Clear(System.Drawing.Color.Ivory);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

foreach (MainCanvas_Class._FlowDiagram.lines_points lp in line_points_coll)
{
Pen pen = new Pen(System.Drawing.Color.SteelBlue,2.5F);
pen.StartCap = lp.b_line_cap;
if(lp.e_line_cap == System.Drawing.Drawing2D.LineCap.Custom)
{
System.Drawing.Drawing2D.AdjustableArrowCap myArrow = new
System.Drawing.Drawing2D.AdjustableArrowCap(3, 3, false);
 
Hi Sunny,

1)
Did you create you own inheritance of Panel and override the OnPaint() on
your child version of the Panel? Eg:

public class MyPanel : Panel
{
protected override OnPaint( PaintEventArgs e )
{
e.Graphics.Clear( Color.Ivory );
// do the drawing of a line here
}
}

and then place an instance of MyPanel on your form instead of an instance of
Panel?

Best Regards,

Robin Theilade

Sunny said:
O.k it's getting more clear now, but let me ask you something, how does it
figure out that it is the Paint() of panel which is to be overriden, if i
simply override the OnPaint() method.
The flicker was not like paper flicker, it was more of like, traces of
lines were left in the original location when the user SCROLLS the panel.
So this is what i did, i added the Invalidate() event in the Form_Resize()
so that solved the problem on Form resizing, the only issue now is that when
user scrolls the panel some portions of the lines are left behind and the
new position is also not accurate sometimes, but it is inconsistent. Like if
then i resize the form, it again draws it correctly.
I kinda figured out the problem, using this.
But this one is on a single form, instead of a PANEL which is what i was
trying to do. Just to add one more thing, the following solution did not
have any problems, and works perfect, unfortunately this one is on a FORM
instead of a PANEL.
Any suggestions on how to use this with Panel
I started of with VB programming two yrs ago, but i have been programming
with C# for about 6 months now.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.Clear(System.Drawing.Color.Ivory);

System.Drawing.Drawing2D.Matrix mx = new System.Drawing.Drawing2D.Matrix(1,0,0,1,AutoScrollPosition.X,AutoScrollPosit
ion.Y);
e.Graphics.Transform=mx;

foreach (Main_Canvas._Trace_Dia.lines_points lp in line_points_coll)
{
Pen pen = new Pen(System.Drawing.Color.SteelBlue,2.5F);
pen.StartCap = lp.b_line_cap;
if(lp.e_line_cap == System.Drawing.Drawing2D.LineCap.Custom)
{
System.Drawing.Drawing2D.AdjustableArrowCap myArrow = new
System.Drawing.Drawing2D.AdjustableArrowCap(3, 3, false);
 
Oh i see,
No i did not, to be honest i didn't knew how to do this.
See, I am actually a systems analyst and my expertise are in the Design and analysis, i have been involved in the design of data structures and class design, but this is the first time, i have to deal with a problem like this.
I don't know how to Thank You, i really appreciate all your assistance.
I did it as you mentioned and it worked fine, lol.
Thanks with Regards
Sunny


----- Robin Theilade wrote: -----

Hi Sunny,

1)
Did you create you own inheritance of Panel and override the OnPaint() on
your child version of the Panel? Eg:

public class MyPanel : Panel
{
protected override OnPaint( PaintEventArgs e )
{
e.Graphics.Clear( Color.Ivory );
// do the drawing of a line here
}
}

and then place an instance of MyPanel on your form instead of an instance of
Panel?

Best Regards,

Robin Theilade

Sunny said:
O.k it's getting more clear now, but let me ask you something, how does it
figure out that it is the Paint() of panel which is to be overriden, if i
simply override the OnPaint() method.
The flicker was not like paper flicker, it was more of like, traces of
lines were left in the original location when the user SCROLLS the panel.
So this is what i did, i added the Invalidate() event in the Form_Resize()
so that solved the problem on Form resizing, the only issue now is that when
user scrolls the panel some portions of the lines are left behind and the
new position is also not accurate sometimes, but it is inconsistent. Like if
then i resize the form, it again draws it correctly.
I kinda figured out the problem, using this.
But this one is on a single form, instead of a PANEL which is what i was
trying to do. Just to add one more thing, the following solution did not
have any problems, and works perfect, unfortunately this one is on a FORM
instead of a PANEL.
Any suggestions on how to use this with Panel
I started of with VB programming two yrs ago, but i have been programming
with C# for about 6 months now.
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; System.Drawing.Drawing2D.Matrix(1,0,0,1,AutoScrollPosition.X,AutoScrollPosit
ion.Y);
{
Pen pen = new Pen(System.Drawing.Color.SteelBlue,2.5F);
pen.StartCap = lp.b_line_cap;
if(lp.e_line_cap == System.Drawing.Drawing2D.LineCap.Custom)
{
System.Drawing.Drawing2D.AdjustableArrowCap myArrow = new
System.Drawing.Drawing2D.AdjustableArrowCap(3, 3, false);
 
Your welcome :)

Sunny said:
Oh i see,
No i did not, to be honest i didn't knew how to do this.
See, I am actually a systems analyst and my expertise are in the Design
and analysis, i have been involved in the design of data structures and
class design, but this is the first time, i have to deal with a problem like
this.
 
Back
Top