[.net] Preventing Graphics to update

Started by
8 comments, last by Bob Janova 18 years, 1 month ago
Is there a lock member or something for Graphics? I am using GDI+ to draw multiple layers in an editor. The problem is that the editor keeps blinking in the areas with multiple layers (only the top layers blink) I certainly need a method to lock the Graphics object so it only updates after I finish drawing all the graphics. After looking for in the code completion and MSDN I couldn't find an obvious name for that member, so I'd better ask here. The alternative would be drawing everything to a bitmap and then just paint the bitmap to the graphics object but that might be less efficient
------ XYE - A new edition of the classic Kye
Advertisement
You can declare a control as double buffered in its constructor (you need to be developing a custom control):
 this.Setstyle(Controlstyles.DoubleBuffered, true)


Alternatively, as you suggested you can do the buffering yourself by drawing to a bitmap instead and then using DrawImage() to paint the bitmap onto your control.
Quote:Original post by Bob Janova
You can declare a control as double buffered in its constructor (you need to be developing a custom control):
 this.Setstyle(Controlstyles.DoubleBuffered, true)


Alternatively, as you suggested you can do the buffering yourself by drawing to a bitmap instead and then using DrawImage() to paint the bitmap onto your control.


Not entirely true. Using reflection you can set the style.

[C#]
try{    Type type = m_control.GetType();    BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;    MethodInfo method = type.GetMethod("Setstyle", flags);    if (method != null)    {        object[] param = { Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true };        method.Invoke(m_control, param);    }}catch{}
Does that work? Ewwww ... that blows open the whole OO encapsulation idea!
Encapsulation is just an idea anyways.

It uses reflection and I think reflection needs special permissions.

But well Seems will use my own control cause there is no way to render a bitmap into another bitmap without using GetPixel / SetPixel Is there?
------ XYE - A new edition of the classic Kye
I'm a little confused as to what you are trying to do here. It is certainly possible to copy one bitmap to another:

Bitmap layer1 = GetLayer(1);Bitmap target = new Bitmap(width, height);Graphics g = Graphics.FromImage(target);g.DrawImage(layer1, 0, 0);g.Dispose();
By developing a custom control, the "custom" portion of the control could be as simple as modifying the constructor to call Setstyle.

public class ControlDoubleBuffered : Control{     public ControlDoubleBuffered()     {          Setstyle(Controlstyles.OptimizedDoubleBuffer |                   Controlstyles.UserPaint |                   Controlstyles.Opaque |                   Controlstyles.AllPaintingInWmPaint, true);     }}


The remainder of the control can be untouched.

An excellent article on this is at http://pluralsight.com/wiki/default.aspx/Craig.FlickerFreeControlDrawing.
..what we do will echo throughout eternity..
Bob Janova I did not have information about that method to get graphics from image, mostly because I was browsing Image and bitmap members thanks a lot for pointing that out
------ XYE - A new edition of the classic Kye
Quote:Original post by Vexorian
Encapsulation is just an idea anyways.

It uses reflection and I think reflection needs special permissions.

But well Seems will use my own control cause there is no way to render a bitmap into another bitmap without using GetPixel / SetPixel Is there?


Right. It definately works, I use it within my game engine because I want the end-programmer to be able to set the control to any object that inherits from the base Control class. Otherwise I would have to inherit every possible control and set the style manually. Or inherit from Control and set the style and only allow end-programmers to inherit from my custom base control class. So reflection definately has pluses.
No problem :). It's in a bit of a weird place, that's for sure; it took me a while to find it the first time I tried to do some layering like stuff too!

This topic is closed to new replies.

Advertisement