c# - 在 Picturebox 中拖动图像并将其定位,然后选择图像的一部分

标签 c# winforms visual-studio picturebox

您好,我有 C# 表单应用程序。我想将图像加载到图片框中并将图像拖动到我想要的位置。完成拖动后,我添加了一个复选框 让用户点击复选框。

然后,用户可以使用鼠标选择图像的一部分。该部分将显示在另一个图片框内。所以,我做了一些搜索并提出了一个解决方案 这实际上不起作用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HCRLibrarytest
{
    public partial class MainForm : Form
    {
        private Point startingPoint = Point.Empty;
        private Point movingPoint = Point.Empty;
        private bool panning = false;
        Image _OrginalBitmap;
        Image _NewBitmap;
        private bool IsSelecting = false;
        private int X0, Y0, X1, Y1;
        static bool isimagepositioned = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_openimage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog2 = new OpenFileDialog
            {
                Filter = "Bitmap Image (*.jpeg)|*.jpeg"
            };

            using (OpenFileDialog dialog = dialog2)
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        using (StreamReader reader = new StreamReader(dialog.FileName))
                        {
                            this._OrginalBitmap = new Bitmap(dialog.FileName);
                            this.pb_fullimage.Image = this._OrginalBitmap;
                        }
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.ToString());
                    }
                }
            }
        }

        private void pb_fullimage_MouseUp(object sender, MouseEventArgs e)
        {
            if (_OrginalBitmap != null)
            {
                if(!isimagepositioned)
                {
                    panning = false;
                }

                else
                {
                    if (!IsSelecting) return;
                    IsSelecting = false;
                    pb_fullimage.Image = _OrginalBitmap;
                    int wid = Math.Abs(X0 - X1);
                    int hgt = Math.Abs(Y0 - Y1);
                    if ((wid < 1) || (hgt < 1)) return;
                    Bitmap area = new Bitmap(wid, hgt);
                    using (Graphics gr = Graphics.FromImage(area))
                    {
                        Rectangle source_rectangle = new Rectangle(Math.Min(X0, X1), Math.Min(Y0, Y1), wid, hgt);
                        Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt);
                        gr.DrawImage(pb_fullimage.Image, dest_rectangle, source_rectangle, GraphicsUnit.Pixel);
                    }
                    pb_selectedportion.Image = area;
                }
            }
        }

        private void pb_fullimage_MouseDown(object sender, MouseEventArgs e)
        {
            if (_OrginalBitmap != null)
            {
                if (!isimagepositioned)
                {
                    panning = true;
                    startingPoint = new Point(e.Location.X - movingPoint.X,e.Location.Y - movingPoint.Y);
                }
                else
                {
                    _NewBitmap = new Bitmap(pb_fullimage.Image);
                    IsSelecting = true;
                    X0 = e.X;
                    Y0 = e.Y;
                }
            }
        }

        private void pb_fullimage_MouseMove(object sender, MouseEventArgs e)
        {
            if (_OrginalBitmap != null)
            {
                if (!isimagepositioned)
                {
                    if (panning)
                    {
                        movingPoint = new Point(e.Location.X - startingPoint.X,e.Location.Y - startingPoint.Y);
                        pb_fullimage.Invalidate();
                    }
                }

                else
                {
                    if (!IsSelecting) return;
                    X1 = e.X;
                    Y1 = e.Y;
                    Bitmap bm = new Bitmap(_NewBitmap);
                    using (Graphics gr = Graphics.FromImage(bm))
                    {
                        gr.DrawRectangle(Pens.Red, Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1));
                    }
                    pb_fullimage.Image = bm;
                }
            }
        }

        private void pb_fullimage_Paint(object sender, PaintEventArgs e)
        {
            if (_OrginalBitmap != null && !isimagepositioned)
            {
                e.Graphics.Clear(Color.White);
                e.Graphics.DrawImage(_OrginalBitmap, movingPoint);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                isimagepositioned = true;
            }
            else
            {
                isimagepositioned = false;
            }
        }
    }
}

当我拖动并选中“图像定位”并使用鼠标移动进行选择时。它总是给我相对于原始图像位置的图像。

那么,有人可以帮忙解决这个问题吗。

enter image description here

最佳答案

由于没有人回答我,所以我找到了答案。这对我有用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HCRLibrarytest
{
    public partial class MainForm : Form
    {
        public Point startingPoint = Point.Empty;
        public Point movingPoint = Point.Empty;
        public bool panning = false;
        Image _OrginalBitmap;
        public static Image _NewBitmap;
        public bool IsSelecting = false;
        public int X0, Y0, X1, Y1;

        public MainForm()
        {
            InitializeComponent();
        }

        public void btn_openimage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog2 = new OpenFileDialog
            {
                Filter = "Bitmap Image (*.jpeg)|*.jpeg"
            };

            using (OpenFileDialog dialog = dialog2)
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        using (StreamReader reader = new StreamReader(dialog.FileName))
                        {
                            this._OrginalBitmap = new Bitmap(dialog.FileName);
                            this.pb_fullimage.Image = this._OrginalBitmap;
                        }
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.ToString());
                    }
                }
            }
        }

        public void pb_fullimage_MouseUp(object sender, MouseEventArgs e)
        {
            if (pb_fullimage.Image != null)
            {
                if (!checkBox1.Checked)
                {
                    panning = false;
                }

                else
                {
                    if (!IsSelecting) return;
                    IsSelecting = false;
                    pb_fullimage.Image = _NewBitmap;
                    int wid = Math.Abs(X0 - X1);
                    int hgt = Math.Abs(Y0 - Y1);
                    if ((wid < 1) || (hgt < 1)) return;
                    Bitmap area = new Bitmap(wid, hgt);
                    using (Graphics gr = Graphics.FromImage(area))
                    {
                        Rectangle source_rectangle = new Rectangle(Math.Min(X0, X1), Math.Min(Y0, Y1), wid, hgt);
                        Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt);
                        gr.DrawImage(_NewBitmap, dest_rectangle, source_rectangle, GraphicsUnit.Pixel);
                    }
                    pb_selectedportion.Image = area;
                }
            }
        }

        public void pb_fullimage_MouseDown(object sender, MouseEventArgs e)
        {
            if (pb_fullimage.Image != null)
            {
                if (!checkBox1.Checked)
                {
                    panning = true;
                    startingPoint = new Point(e.Location.X - movingPoint.X, e.Location.Y - movingPoint.Y);
                }
                else
                {
                    IsSelecting = true;
                    X0 = e.X;
                    Y0 = e.Y;
                }
            }
        }

        public void pb_fullimage_MouseMove(object sender, MouseEventArgs e)
        {
            if (pb_fullimage.Image != null)
            {
                if (!checkBox1.Checked)
                {
                    if (panning)
                    {
                        movingPoint = new Point(e.Location.X - startingPoint.X, e.Location.Y - startingPoint.Y);
                        pb_fullimage.Invalidate();
                        using (Bitmap bitmap = new Bitmap(pb_fullimage.ClientSize.Width, pb_fullimage.ClientSize.Height))
                        {
                            pb_fullimage.DrawToBitmap(bitmap, pb_fullimage.ClientRectangle);
                            try { bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "draw.jpg"); }
                            catch (Exception ex) { Console.Write(ex.ToString()); }
                        }
                    }
                }

                else
                {
                    if (!IsSelecting) return;
                    X1 = e.X;
                    Y1 = e.Y;
                    Bitmap bm = new Bitmap(Bitmap.FromStream(File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "draw.jpg"))));
                    using (Graphics gr = Graphics.FromImage(bm))
                    {
                        gr.DrawRectangle(Pens.WhiteSmoke, Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1));
                    }
                    _NewBitmap = bm;
                }
            }
        }

        public void pb_fullimage_Paint(object sender, PaintEventArgs e)
        {
            if (pb_fullimage.Image != null && !checkBox1.Checked)
            {
                e.Graphics.Clear(Color.White);
                e.Graphics.DrawImage(pb_fullimage.Image, movingPoint);
            }
        }
    }
}

关于c# - 在 Picturebox 中拖动图像并将其定位,然后选择图像的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594485/

相关文章:

c# - 散列密码的最佳实践

c# - 放宽数据表适配器的约束

c# - 每次按下按钮时,如何从字符串的前面连续移动n个字符到末尾?

c# - 在调试器可视化器中获取变量名

c# - Oracle 存储过程适用于 ADO.NET,但使用 OrmLite 会引发异常?

c# - 相当于Xamarin forms中Div的 "float:left"

c# - 停止运行 backgroundworker 并启动新的。

c# - 如何通过代码在一个实例中创建 .NET 程序的另一个实例?

c# - 无法使用 C# 将数据从 Visual Studio 导入 MySQL

.net - 为什么RegisterAllAreas 中区域的顺序随着Visual Studio 2015 发生变化?