none
Flicker in redrawing the picture box on a button press RRS feed

  • Question

  • hi !

     I am trying to implement Zoom In/Out functionality.For that I am redrawing the picturebox with different width and height of picture.
    Have a look

    g.DrawImage(originalImage, new Rectangle(posLeft, posTop, pictureBox1.Width - 1,

    pictureBox1.Height - 1), posLeft, posTop, (int)(originalImage.Width / zoomFactor),

    (int)(originalImage.Height / zoomFactor), GraphicsUnit.Pixel, new System.Drawing.Imaging.ImageAttributes());



    But the problem is when i try to redraw the picture box controlon click of Button, it flickers.

    i cant use

    using (Graphics gx = Graphics.FromImage(bitmap))

    as i have to take graphic pointer from picturebox control.

    Please help how to set double buffer in this case.
    Thursday, January 22, 2009 12:16 PM

Answers

  • Hi omniteam,

    this code works like a charm, really. Without flickering, side effects or anything strange:)

     

    using System;  
    using System.IO;  
    using System.Linq;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Windows.Forms;  
    using System.Reflection;  
     
    namespace DoubleBufferingPictureBox  
    {  
        public partial class Form1 : Form  
        {  
            private float m_zoomFactor;  
            private Bitmap m_origBitmap;  
     
            public Form1()  
            {  
                InitializeComponent();  
                m_zoomFactor = 1;  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);  
                path = path + @"\TestInt.Bmp";  
                m_origBitmap = new Bitmap(path);  
                drawPictureBox(pictureBox1.CreateGraphics(), true);  
            }  
     
            private void drawPictureBox(Graphics graphics, bool ownGraphics)  
            {  
     
                using (Bitmap tempBmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))  
                {  
                    using (Graphics tempG = Graphics.FromImage(tempBmp))  
                    {  
                        tempG.Clear(Color.White);  
     
                        //Now zoom from only 0,0 point  
                        tempG.DrawImage(m_origBitmap, new Rectangle(0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1),  
                                            0, 0, (int)(m_origBitmap.Width / m_zoomFactor),(int)(m_origBitmap.Height / m_zoomFactor),   
                                            GraphicsUnit.Pixel, new System.Drawing.Imaging.ImageAttributes());  
     
     
                          
                        Rectangle srcRect = new Rectangle(0, 0, tempBmp.Width, tempBmp.Height);  
                        graphics.DrawImage(tempBmp, 0, 0, srcRect, GraphicsUnit.Pixel);  
                    }  
     
                      
                }  
     
                if (ownGraphics)  
                {  
                    graphics.Dispose();  
                }  
            }  
     
     
            private void pictureBox1_Paint(object sender, PaintEventArgs e)  
            {  
                drawPictureBox(e.Graphics, false);  
            }  
     
            private void pictureBox1_Click(object sender, EventArgs e)  
            {  
     
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                m_zoomFactor += 0.1f;  
                if (m_zoomFactor >= 3f)  
                {  
                    m_zoomFactor = 1f;  
                }  
       
                drawPictureBox(pictureBox1.CreateGraphics(), true);  
                  
            }  
        }  

     

    HTH

    Rene Stein


    http://blog.renestein.net Twitter: renestein
    • Marked as answer by Omniteam1 Friday, January 23, 2009 11:13 AM
    Friday, January 23, 2009 10:15 AM
  • Hi Rene,

         Thanks a lot for ur replies.

       My probs just got solved .

      I read this article http://www.bobpowell.net/pictureboxhowto.htm

    which says picturebox.creategraphics is not the right way of using the picturebox.

     pictureBox1.Image = Properties.Resources.Water_lilies;

    using (Graphics g = Graphics.FromImage(pictureBox1.Image))

    now use this graphics object g for drawing image or drawing other things.

    Hope to get your replies continuously on the form.

    Roohi

     

    • Marked as answer by Omniteam1 Friday, January 23, 2009 11:14 AM
    Friday, January 23, 2009 11:13 AM

All replies

  • Hi,
    you can use new Graphics and approach mentioned here yesterday - http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/16b17085-5f59-489c-8de4-4b730818910e.

    using (Bitmap bitmap = new Bitmap(pictureBox1.WidthpictureBox1.Height))  
                {  
                    using(Graphics gx = Graphics.FromImage(bitmap))  
                    {  
                          
                             //draw to the memory graphics

                             Graphics g = pictureBox1.CreateGraphics();
                              g.Graphics.DrawImage(bitmap, 0, 0);  

                    }  
                    
                      
     
                }  


    HTH
    Rene Stein
    http://blog.renestein.net Twitter: renestein
    • Proposed as answer by Rene Stein Thursday, January 22, 2009 5:22 PM
    • Unproposed as answer by Omniteam1 Friday, January 23, 2009 6:04 AM
    Thursday, January 22, 2009 12:51 PM
  • Hi Rene,

       Thanks for answering but still the problem remains.

      In fact after doing this, its behaving strange with screen going complete black and then it zooms and again.

     Since I am redrawing the image with changed width and height(Zooming) on every button press,I have to clear the previous image

    using g.clear(Color).So first the previous image is cleared and then new image is drawn.When i do it contiuously ,the screen flickers

    severly.Please suggest  

     

     Roohi

     

    Friday, January 23, 2009 6:00 AM
  • Hi omniteam,

    this code works like a charm, really. Without flickering, side effects or anything strange:)

     

    using System;  
    using System.IO;  
    using System.Linq;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Windows.Forms;  
    using System.Reflection;  
     
    namespace DoubleBufferingPictureBox  
    {  
        public partial class Form1 : Form  
        {  
            private float m_zoomFactor;  
            private Bitmap m_origBitmap;  
     
            public Form1()  
            {  
                InitializeComponent();  
                m_zoomFactor = 1;  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);  
                path = path + @"\TestInt.Bmp";  
                m_origBitmap = new Bitmap(path);  
                drawPictureBox(pictureBox1.CreateGraphics(), true);  
            }  
     
            private void drawPictureBox(Graphics graphics, bool ownGraphics)  
            {  
     
                using (Bitmap tempBmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))  
                {  
                    using (Graphics tempG = Graphics.FromImage(tempBmp))  
                    {  
                        tempG.Clear(Color.White);  
     
                        //Now zoom from only 0,0 point  
                        tempG.DrawImage(m_origBitmap, new Rectangle(0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1),  
                                            0, 0, (int)(m_origBitmap.Width / m_zoomFactor),(int)(m_origBitmap.Height / m_zoomFactor),   
                                            GraphicsUnit.Pixel, new System.Drawing.Imaging.ImageAttributes());  
     
     
                          
                        Rectangle srcRect = new Rectangle(0, 0, tempBmp.Width, tempBmp.Height);  
                        graphics.DrawImage(tempBmp, 0, 0, srcRect, GraphicsUnit.Pixel);  
                    }  
     
                      
                }  
     
                if (ownGraphics)  
                {  
                    graphics.Dispose();  
                }  
            }  
     
     
            private void pictureBox1_Paint(object sender, PaintEventArgs e)  
            {  
                drawPictureBox(e.Graphics, false);  
            }  
     
            private void pictureBox1_Click(object sender, EventArgs e)  
            {  
     
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                m_zoomFactor += 0.1f;  
                if (m_zoomFactor >= 3f)  
                {  
                    m_zoomFactor = 1f;  
                }  
       
                drawPictureBox(pictureBox1.CreateGraphics(), true);  
                  
            }  
        }  

     

    HTH

    Rene Stein


    http://blog.renestein.net Twitter: renestein
    • Marked as answer by Omniteam1 Friday, January 23, 2009 11:13 AM
    Friday, January 23, 2009 10:15 AM
  • Hi Rene,

         Thanks a lot for ur replies.

       My probs just got solved .

      I read this article http://www.bobpowell.net/pictureboxhowto.htm

    which says picturebox.creategraphics is not the right way of using the picturebox.

     pictureBox1.Image = Properties.Resources.Water_lilies;

    using (Graphics g = Graphics.FromImage(pictureBox1.Image))

    now use this graphics object g for drawing image or drawing other things.

    Hope to get your replies continuously on the form.

    Roohi

     

    • Marked as answer by Omniteam1 Friday, January 23, 2009 11:14 AM
    Friday, January 23, 2009 11:13 AM