M
minimega
Hello to all NG,
I've this problem: I'd like to give my application a better look, so
I'm trying to use gradient pattern and bitmaps/icons in my forms.
To draw gradient pattern I've write a little code that draws continuos
lines adding a fixed value to each RGB color component and makes the
gradient effect. However the DrawLine can be applied only to a Graphics
object, and the problem is how create this Grapichs object.
Until now I'm only be able to use the Graphics object in the Paint
event of each control in the form (panel, picture, command..) but if I
want to use it outside the Paint object I'm not been able to do it.
Cut and paste this code in button1_Click:
Graphics g = null;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 100);
g = System.Drawing.Graphics.FromImage(bitmap);
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
pictureBox1.Image = bitmap;
g.Dispose();
This works! I created a new bitmap (100x100 pixels) in memory, created
a graphics Object from it, drawed a rectangle in it, finally displayed
it in a Picture control. Outside Paint object this works.
g = this.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
This works! Creating Graphics object with this.CreateGraphics() is
working also outside Paint event.
g = pictureBox1.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
This doesn't works.. I get a "System.NotSupportedException" error in
line g = pictureBox1.CreateGraphics();
g = panel1.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
same error as above.
So, it seems that I can safe use CreateGraphics outside Paint event
only with form (this)object, or create a bitmap in memory, then apply
it to a picture object setting it to Picture.Image property. Why
..CreateGraphics() with other controls in the form raises an Exception?
In my form I've more than 15 panels, and for everyone I have to draw a
gradient. Putting it in Paint event I get as result that when forms
appears for the first time, or big parts of the form must be repainted,
I can see the "work in progress" and between the first panel and the
last panel drawer I get at least 500-600 ms, and the effect is no good.
More, if I want to force a repaint of an object, the only way I've is
to .Invalidate() the object, that automatically fires a paint event (I
cant manualli call the Paint event via code because I can't create the
"e" parameter). However the .Invalidate() clears the object with his
..BackColor property, so I get a "flicking" effects because the control
first is filled with a solid color, then redraws the gradient, and this
is not good to see.. Is there a way to .Invalidate the object, to
automatically fire the Paint event, without before clear the control
surface?
How can be well done this mechanism of quick-drawing? Must I create at
form_load many in-memory bitmaps with the size of each control, fill
them with the gradient or other bitmaps/icon I want to display, then on
every control's Paint event draw the in-memory bitmap in the surface of
the control?
Any other ideas?
Thanks,
Massimo
I've this problem: I'd like to give my application a better look, so
I'm trying to use gradient pattern and bitmaps/icons in my forms.
To draw gradient pattern I've write a little code that draws continuos
lines adding a fixed value to each RGB color component and makes the
gradient effect. However the DrawLine can be applied only to a Graphics
object, and the problem is how create this Grapichs object.
Until now I'm only be able to use the Graphics object in the Paint
event of each control in the form (panel, picture, command..) but if I
want to use it outside the Paint object I'm not been able to do it.
Cut and paste this code in button1_Click:
Graphics g = null;
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 100);
g = System.Drawing.Graphics.FromImage(bitmap);
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
pictureBox1.Image = bitmap;
g.Dispose();
This works! I created a new bitmap (100x100 pixels) in memory, created
a graphics Object from it, drawed a rectangle in it, finally displayed
it in a Picture control. Outside Paint object this works.
g = this.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
This works! Creating Graphics object with this.CreateGraphics() is
working also outside Paint event.
g = pictureBox1.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
This doesn't works.. I get a "System.NotSupportedException" error in
line g = pictureBox1.CreateGraphics();
g = panel1.CreateGraphics();
g.DrawRectangle(new System.Drawing.Pen(Color.Red), new Rectangle(0, 0,
50, 50));
g.Dispose();
same error as above.
So, it seems that I can safe use CreateGraphics outside Paint event
only with form (this)object, or create a bitmap in memory, then apply
it to a picture object setting it to Picture.Image property. Why
..CreateGraphics() with other controls in the form raises an Exception?
In my form I've more than 15 panels, and for everyone I have to draw a
gradient. Putting it in Paint event I get as result that when forms
appears for the first time, or big parts of the form must be repainted,
I can see the "work in progress" and between the first panel and the
last panel drawer I get at least 500-600 ms, and the effect is no good.
More, if I want to force a repaint of an object, the only way I've is
to .Invalidate() the object, that automatically fires a paint event (I
cant manualli call the Paint event via code because I can't create the
"e" parameter). However the .Invalidate() clears the object with his
..BackColor property, so I get a "flicking" effects because the control
first is filled with a solid color, then redraws the gradient, and this
is not good to see.. Is there a way to .Invalidate the object, to
automatically fire the Paint event, without before clear the control
surface?
How can be well done this mechanism of quick-drawing? Must I create at
form_load many in-memory bitmaps with the size of each control, fill
them with the gradient or other bitmaps/icon I want to display, then on
every control's Paint event draw the in-memory bitmap in the surface of
the control?
Any other ideas?
Thanks,
Massimo