0
您好我有C#表單應用程序。我想將一幅圖像加載到一個圖片框中,並將圖像拖到我想要的位置。當我完成拖動後,我添加了一個複選框 ,讓用戶點擊複選框。在Picturebox中拖動和定位圖像並選擇圖像的一部分
然後,用戶可以使用鼠標來選擇圖像的一部分。該部分將顯示在另一個圖片框內。所以,我做了一些搜索,並提出了一個解決方案 ,這實際上不起作用。
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;
}
}
}
}
當我拖動並選中「圖像定位」並選擇使用鼠標移動。它總是給我與原始圖像位置相關的圖像。
那麼,有人可以幫助解決這個問題。