N
N
Nelo-0012018-11-28 23:12:57
Canvas
Nelo-001, 2018-11-28 23:12:57

Cut rectangle from Panel in WinForm C#?

Good afternoon, I'm doing paint, I've done everything! it remains to print a rectangular area, the workspace of the paint is in the "panel" component, you need to somehow cut or copy through rectangl and what will be inside the rectangla should be made the background of the picture box. Tried a lot of things and nothing worked, help plz

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-11-29
@vakym

As an option.
5c0009319fc88080499096.png

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        internal bool IsCut = false;// Флаг.
        internal Point startPoint;//стартовая точка выделения
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            IsCut = true;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if(IsCut==true)
            {
                startPoint = Cursor.Position;//при нажатии кнопки мыши по panel запоминаем абсолютные координаты указателя мыши.
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)//события "отпускания" кнопки мыши
        {
            if(IsCut==true)
            {
                Size size = new Size(Math.Abs(startPoint.X - Cursor.Position.X), Math.Abs(startPoint.Y - Cursor.Position.Y));//вычисляем размер выделенной области
                var bm = new Bitmap(size.Width, size.Height);// создаем bitmap заданного размера

                Graphics graphics = Graphics.FromImage(bm as Image);

                graphics.CopyFromScreen(startPoint, new Point(0, 0), bm.Size); //копируем часть изображения экрана в bitmap

                pictureBox1.Image = bm; //выводим в picturebox
                IsCut = false;// снимаем флаг необходимости выделения
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question