Panel background image flickering

Season

I've a Panel which fills the parent Form.
And I used a Timer to capture screen ,
and set the screenshot as background image of Panel periodically.

However, it runs into crazy flickering. What can I do to solve it?

//Part of code
 public partial class Form1 : Form
    {

        DxScreenCapture sc = new DxScreenCapture();

        public Form1()
        {
            InitializeComponent();

            panelMain.BackgroundImageLayout = ImageLayout.Zoom;
         }

        private void Form1_Load(object sender, EventArgs e)
        {
        }



        void RefreshScreen()
        {
            Surface s = sc.CaptureScreen();

            DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp);
            panelMain.BackgroundImage = Image.FromStream(ds);

            s.Dispose();
        }

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

        }
    }
toATwork

Try using a double buffered panel. Inherit panel, set DoubleBuffered to true and use that panel instead of default panel:

    namespace XXX
    {
        /// <summary>
        /// A panel which redraws its surface using a secondary buffer to reduce or prevent flicker.
        /// </summary>
        public class PanelDoubleBuffered : System.Windows.Forms.Panel
        {
            public PanelDoubleBuffered()
                : base()
            {
                this.DoubleBuffered = true;
            }
        }
    }

EDIT

Additionally I want to encourage you to take care a little more about the resources you use. Whenever an object implements the IDisposable interface - dispose the object when not needed any more. This is very important when dealing with unmanaged resources, such as streams!

    void RefreshScreen()
    {
            using (Surface s = sc.CaptureScreen())
            {
                using (DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp))
                {
                    Image oldBgImage = panelMain.BackgroundImage;
                    panelMain.BackgroundImage = Image.FromStream(ds);
                    if (oldBgImage != null)
                        oldBgImage.Dispose();
                }
            }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Background image flickering in a div

Flickering of background image when it is set with data binding

asp .net background image flickering on postback

How to prevent a background image flickering on change

if background image of a panel is equal to image in my resources

flickering with timer on update panel

How do I fix white flickering on an animated background image?

Framework 7 left panel background image issue

How to add an image on ggplot background (not the panel)?

Are we able to set opacity of the background image of a panel?

Image flickering in canvas

Image flickering on mouseover and mouseout

PyGame Image Flickering

White flickering when changing pages using an Image Background in React-native

Why is the background flashing/flickering when transitioning to another image? css animation keyframes

How do you adjust CSS to fix flickering from Keyframes changing the background image?

How to configure Panel Background Image in ASP.NET

background pixels flickering on bootstrap modal

Panel Background color, Transparency and background image don't work with Ambiance theme

How to avoid image flickering in a listview

Image flickering while scrolling in RecyclerView

css image swap on mouseover flickering

How to eliminate flickering with erase background in mfc

Background color of a panel

How do I add a responsive background image in panel heading. Twiitter Bootstrap 3

Descenders in facet panel intrude on panel background

Why does this image cause screen flickering?

Reload Live Image without Flickering in React

How to remove monitor flickering noise from an image?