Home  >  Article  >  Backend Development  >  C# Development Example-Customized Screenshot Tool (5) Optimization for flickering and freezing when dragging

C# Development Example-Customized Screenshot Tool (5) Optimization for flickering and freezing when dragging

黄舟
黄舟Original
2017-03-14 13:27:422241browse

Due to a mistake during implementation, the area redrawing technology of the main form was not used, but a Label component was used to display the intercepted picture area, so the area will be intercepted when dragging When making the screenshot smaller or taking a reverse screenshot, flickering and freezing will be more serious. Here are some targeted optimizations to address these two issues.

C#Double buffering explanation:

Simply put, when we are performing drawing operations, the system is not directly Instead of presenting the content to the screen, save it in memory first, and then output the results all at once. If you don't use double buffering, you will find that the screen will flicker violently during the drawing process, because the background is constantly refreshing. This situation will not occur if you wait for the user to finish drawing before outputting. The specific method is to first create a bitmap object , then save the content in it, and finally present the picture.

Enable double buffering

        public Form1()
        {
            InitializeComponent();
            // 解决窗口闪烁的问题
            SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }

Delayed redraw

When the mouse is dragged, redrawing will be very frequent. Time is used to judge here to reduce the need for redrawing. frequency.

AddVariable:

        /// <summary>
        /// 记录鼠标上一次移动的时间
        /// </summary>
        private long lastMouseMoveTime = System.DateTime.Now.Ticks;

Add redraw control in the "UpdateCutInfoLabel" method:

        /// <summary>
        /// 更新截图信息显示框,截图编辑工具框
        /// </summary>
        private void UpdateCutInfoLabel(UpdateUIMode updateUIMode) // UpdateUIMode updateUIMode = UpdateUIMode.None
        {
            //大于300毫秒或有组件显示或隐藏才进行重绘
            long mouseMoveTimeStep = System.DateTime.Now.Ticks - lastMouseMoveTime;
            if (mouseMoveTimeStep < 300 && updateUIMode == UpdateUIMode.None) { return; }
            lastMouseMoveTime = System.DateTime.Now.Ticks;

            if (this.lbl_CutImage.Visible || (updateUIMode & UpdateUIMode.ShowCutImage) != UpdateUIMode.None)
            {
                this.lbl_CutImage.SetBounds(this.cutImageRect.Left, this.cutImageRect.Top, this.cutImageRect.Width, this.cutImageRect.Height, BoundsSpecified.All);
                if (!this.lbl_CutImage.Visible)
                {
                    this.lbl_CutImage.Show();
                }
            }
        }

After testing, it was found that flickering and stuck when dragging The phenomenon of sudden improvement has been significantly improved.



The above is the detailed content of C# Development Example-Customized Screenshot Tool (5) Optimization for flickering and freezing when dragging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn