יום שני, 20 במרץ 2017

איך להעתיק webbrowser ל-pictureBox ומשם לקליפבורד בעזרת בחירה

איך להעתיק webbrowser ל-pictureBox ומשם לקליפבורד בעזרת בחירה

namespace WriteBraille
{
    public partial class WebbrowserToPanel : Form
    {
        public WebbrowserToPanel()  {      InitializeComponent();   }

        private void WebbrowserToPanel_Load(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri(textBox1.Text);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
                webBrowser1.Url = new Uri(textBox1.Text);
        }

        private Point RectStartPoint;
        private Rectangle Rect = new Rectangle();
        private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));

        private void button1_Click(object sender, EventArgs e)
        {
            Control c = webBrowser1;
            Bitmap bmp = new Bitmap(c.Width, c.Height);
            c.DrawToBitmap(bmp, c.ClientRectangle);
            pictureBox1.Image = bmp;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // Determine the initial rectangle coordinates...
            RectStartPoint = e.Location;
            Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Draw the rectangle...
            if (pictureBox1.Image != null)
            {
                if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
                {
                    e.Graphics.FillRectangle(selectionBrush, Rect);
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                return;
            Point tempEndPoint = e.Location;
            Rect.Location = new Point(
                Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));
            Rect.Size = new Size(
                Math.Abs(RectStartPoint.X - tempEndPoint.X),
                Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
            pictureBox1.Invalidate();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (Rect.Contains(e.Location))
                {
                    selectionBrush.Dispose();
                    //Debug.WriteLine("Right click");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Image img = (pictureBox1.Image as Bitmap).Clone(Rect, pictureBox1.Image.PixelFormat);
            Clipboard.SetImage(img);
        }
    }
}