Search code examples
c#graphicsmouseselectiondraw

c# Drawing square to highlight mouse selection area


I am building screen capture for part of desktop app with c#. Z keyup event to start drawing red rectangle as mouse move to next point then Z keyup again to stop drawing red box.

The problem is that it will run very slow or crash in slow computers like laptops. I did something wrong and I tried to fix it for a month but it didn't work. Help me, plz. Also, is there a better way or a library to solve it?

   private void step1()
        {            
                start = true;
                mypic.pics = mypic.capturepic(Cursor.Position);
                Bmp = new Bitmap(mypic.pics);

              timer1.Enabled = true;
              timer1.Start();
        }
        private void draw_rec_repeat()
        {
            InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
            IntPtr desktopPtr = GetDC(IntPtr.Zero);
            using (System.Drawing.Graphics gg = System.Drawing.Graphics.FromHdc(desktopPtr))
            {
                Rectangle bb = DrawRec(Cursor.Position.X, Cursor.Position.Y, desktopPtr, gg);
                gg.DrawRectangle(new Pen(Color.Red, 3), bb);
            }            
            ReleaseDC(this.Handle, desktopPtr);                                                      
        }



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

Solution

  • C# How to Draw a Rubber Band Selection Rectangle on Panel, like one used in Windows Explorer?

    It helped. The screen was saved then pasted into a maximized picturebox. Then I draw rectangles onto double-buffered picturebox control.

    Also, I tried to use customize timer.interval so slow laptop users could slow down the draw time.

    That's about it.