Drag and position Image inside Picturebox and Select a Portion of the Image
您好,我有 C# 表单应用程序。我想将图像加载到图片框中并将图像拖动到我想要的位置。完成拖动后,我添加了一个复选框
让用户单击复选框。
然后,用户可以使用鼠标选择图像的一部分。该部分将显示在另一个图片框内。所以,我做了一些搜索并想出了一个解决方案
这实际上不起作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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; } } } } |
当我拖动并选中"图像定位"并使用鼠标移动进行选择时。它总是给我相对于原始图像位置的图像。
那么,谁能帮忙解决这个问题。
由于没有人回答我,我找到了答案。这对我有用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 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); } } } } |