How to form a bitmap image in compact framework

hi all...

i am developing an appliction in which i am drawing some rectangles & lines along with some text. I am using .NET compact framework. But whatevr application i have developed it is showing some flickering problem. So can anyone tell me how to remove this flickering. If we form a bitmap image from the rectangles, lines & text then i think we can remove this flickering. So can anybody tell me how to form a bitmap image at runtime in .NET compact framework.

Thanks,

vinay



Answer this question

How to form a bitmap image in compact framework

  • rpreston

    One way to avoid the 'flicker' is to NOT redraw the background everytime you draw a new line or text. Override the OnPaintBackground method to do nothing.

    That will often help.

    Dan



  • Xadja

    Thanks for your reply....

    I also want to avoid flickering which is i think because of "OnPaint" .... so can u tell me how this flickering can be remmoved...

    Regards,

    Vinay


  • ~rabin

    Well, I have no time to try, but try this:

    In OnPaint
    1) Create bitmap, with the size of the forms ClientRectangle
    2) Create graphics from that bitmap
    3) Clear graphics to suitable back color (black default) and draw to created graphics object
    4) Dispose graphics
    5) Draw the created image to the e.Graphics

    voila, a back buffered drawing. Hope it works :) In compact framework there is no double buffering, so it has to be done manually (and it is not really double buffering, it should filcker less than without...)

    edit: I had some time after all, here's some samples that seems to work:

            private Timer timer1;
            private Random rand = new Random();

            Bitmap backBuffer;
            protected override void OnPaint(PaintEventArgs e)
            {
                if (backBuffer == null)
                    backBuffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

                Graphics g = Graphics.FromImage(backBuffer);
                //Draw here
                g.Clear(Color.White);
                g.DrawEllipse(new Pen(Color.Black), new Rectangle(0, 0, rand.Next(10, ClientRectangle.Width), rand.Next(10, ClientRectangle.Height)));
                g.Dispose();

                e.Graphics.DrawImage(backBuffer, 0, 0);
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                Invalidate();
            }

            private void Form1_Resize(object sender, EventArgs e)
            {
                if (backBuffer != null)
                {
                    backBuffer.Dispose();
                    backBuffer = null;
                }
                backBuffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
            }

  • martona

    Exactly same way that in full .NET: Create image, create graphics object, draw and dispose.

    Bitmap b = new Bitmap(100, 100);
    Graphics g = Graphics.FromImage(b);
    //Draw here
    g.DrawEllipse(new Pen(Color.Black), new Rectangle(0, 0, 100, 100));
    g.Dispose();

  • Rups11

    Then you will need to implement your own background draw. By default (if I'm remebering correctly) windows wipes everything to a base color (I seem to think it is the ActiveControlColor, which ever it is a light color) and then redraws the background to the color set by BackgroundColor. That is what causes the flicker.

    In your handler, do your own background drawing with the base background color that you want. Do not call the base handler.

    Dan



  • mesh2005

    Ya... but the text whixh i am displaying is changing at run-time. So i have to refresh the screen & redraw the lines & text...

    Any other way i can avoid flickering...

    Vinay


  • Will Riley

    rauhanlinnake wrote:
    Well, I have no time to try, but try this:

    In OnPaint
    1) Create bitmap, with the size of the forms ClientRectangle
    2) Create graphics from that bitmap
    3) Clear graphics to suitable back color (black default) and draw to created graphics object
    4) Dispose graphics
    5) Draw the created image to the e.Graphics

    voila, a back buffered drawing. Hope it works Smile In compact framework there is no double buffering, so it has to be done manually (and it is not really double buffering, it should filcker less than without...)

    edit: I had some time after all, here's some samples that seems to work:

    private Timer timer1;
    private Random rand = new Random();

    Bitmap backBuffer;
    protected override void OnPaint(PaintEventArgs e)
    {
    if (backBuffer == null)
    backBuffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

    Graphics g = Graphics.FromImage(backBuffer);
    //Draw here
    g.Clear(Color.White);
    g.DrawEllipse(new Pen(Color.Black), new Rectangle(0, 0, rand.Next(10, ClientRectangle.Width), rand.Next(10, ClientRectangle.Height)));
    g.Dispose();

    e.Graphics.DrawImage(backBuffer, 0, 0);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    Invalidate();
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
    if (backBuffer != null)
    {
    backBuffer.Dispose();
    backBuffer = null;
    }
    backBuffer = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
    }


    the above code works beautifully... however when I draw the bitmap on one of the tab... it starts to flick again...
    and here is the code that I have made changed:

    private void DrawClock(Graphics ee)
    {
    if (backBuffer == null)
    {
    backBuffer = new Bitmap(this.tabPage1.ClientRectangle.Width, this.tabPage1.ClientRectangle.Height);
    }
    Graphics grfx = Graphics.FromImage(backBuffer);
    grfx.Clear(Color.White);

    // Do drawing here blah blah

    ee.DrawImage(backBuffer, 0, 0);
    }


    private void timer1_Tick(object sender, EventArgs e)
    {
    this.tabPage1.Invalidate();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

    }
    protected override void OnPaint(PaintEventArgs e)
    {

    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {

    }

    private void tabPage1_Paint(object sender, PaintEventArgs e)
    {
    DrawClock(e.Graphics);

    }



    Can someone help

    Thanks in advance

  • How to form a bitmap image in compact framework