使用鼠标事件在 Picturebox 中移动图像
Move Image inside the Picturebox using mouse events
我有这段代码,其中 upload/place 图片框内的图像。我现在要做的是使用 MouseEvents 将其拖动到 pictureBox 中。当我的图像是使用另一个 class 绘制时我该怎么做?
图片Class
public void ImageDrawing(Bitmap bm, RectangleF rect, PaintEventArgs e)
{
this.image = bm;
this.width = rect.Width;
this.height = rect.Height;
this.rect = rect;
Graphics g = e.Graphics;
bm = ImageClass.GrayscaleImage(bm);
bm.MakeTransparent(Color.White);
g.DrawImage(bm, rect);
}
主窗体
private void btn_Browse_Click(object sender, EventArgs e)
{
PaintImage();
}
public void PaintImage()
{
buttons = (Shape.ShapeType)System.Enum.Parse(typeof(Shape.ShapeType), "image");
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
if (open.ShowDialog() == DialogResult.OK)
{
//Display image in picture box
string strType = Path.GetExtension(open.FileName);
FileInfo fs = new FileInfo(open.FileName);
long fileSize = fs.Length / 1024;
ImageBitmap = new Bitmap(open.FileName);
}
imageHeight = ImageBitmap.Height / 3f;
imageWidth = ImageBitmap.Width / 3f;
imageX = (shape.center.X - (imageWidth / 2));
imageY = (shape.center.Y - (imageHeight / 2));
imageRect = new RectangleF(imageX, imageY, imageWidth, imageHeight);
pictureBox_Canvass.Refresh();
}
使用MouseMove
事件和Mousebutton
检查来确定目标矩形的top/left。 (或者也许是中心?)
然后调用pBox.Invalidate()
触发Paint
事件!
示例:
ImageClass anImage = null;
private void pb_canvas_Paint(object sender, PaintEventArgs e)
{
anImage.ImageDrawing(anImage.image, anImage.rectangle, e);
}
private void pb_canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left))
{
anImage.rectangle = new Rectangle(e.X, e.Y, anImage.image.Width, anImage.image.Height );
pb_canvas.Invalidate();
}
}
参考TaW的代码,我实现了通过鼠标拖动在图片框内移动图片。请查看图片框鼠标和绘画的事件。
// Global Variables
private int _xPos;
private int _yPos;
private bool _dragging;
Image _img;
Rectangle _imgRect;
private void pictureBox1_MouseUp(
object sender, MouseEventArgs e) {
var c = sender as PictureBox;
if (null == c) return;
_dragging = false;
}
private void pictureBox1_MouseDown(
object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) return;
_dragging = true;
_xPos = e.X;
_yPos = e.Y;
}
private void pictureBox1_MouseMove(
object sender, MouseEventArgs e) {
if (!_dragging || _img == null ) return;
if (e.Button == MouseButtons.Left) {
_imgRect = new Rectangle(-(_xPos-e.X), -(_yPos - e.Y), _img.Width, _img.Height);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(
object sender, PaintEventArgs e) {
if (_img != null) {
e.Graphics.DrawImage(_img, _imgRect);
}
}
我有这段代码,其中 upload/place 图片框内的图像。我现在要做的是使用 MouseEvents 将其拖动到 pictureBox 中。当我的图像是使用另一个 class 绘制时我该怎么做?
图片Class
public void ImageDrawing(Bitmap bm, RectangleF rect, PaintEventArgs e)
{
this.image = bm;
this.width = rect.Width;
this.height = rect.Height;
this.rect = rect;
Graphics g = e.Graphics;
bm = ImageClass.GrayscaleImage(bm);
bm.MakeTransparent(Color.White);
g.DrawImage(bm, rect);
}
主窗体
private void btn_Browse_Click(object sender, EventArgs e)
{
PaintImage();
}
public void PaintImage()
{
buttons = (Shape.ShapeType)System.Enum.Parse(typeof(Shape.ShapeType), "image");
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
if (open.ShowDialog() == DialogResult.OK)
{
//Display image in picture box
string strType = Path.GetExtension(open.FileName);
FileInfo fs = new FileInfo(open.FileName);
long fileSize = fs.Length / 1024;
ImageBitmap = new Bitmap(open.FileName);
}
imageHeight = ImageBitmap.Height / 3f;
imageWidth = ImageBitmap.Width / 3f;
imageX = (shape.center.X - (imageWidth / 2));
imageY = (shape.center.Y - (imageHeight / 2));
imageRect = new RectangleF(imageX, imageY, imageWidth, imageHeight);
pictureBox_Canvass.Refresh();
}
使用MouseMove
事件和Mousebutton
检查来确定目标矩形的top/left。 (或者也许是中心?)
然后调用pBox.Invalidate()
触发Paint
事件!
示例:
ImageClass anImage = null;
private void pb_canvas_Paint(object sender, PaintEventArgs e)
{
anImage.ImageDrawing(anImage.image, anImage.rectangle, e);
}
private void pb_canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button.HasFlag(MouseButtons.Left))
{
anImage.rectangle = new Rectangle(e.X, e.Y, anImage.image.Width, anImage.image.Height );
pb_canvas.Invalidate();
}
}
参考TaW的代码,我实现了通过鼠标拖动在图片框内移动图片。请查看图片框鼠标和绘画的事件。
// Global Variables
private int _xPos;
private int _yPos;
private bool _dragging;
Image _img;
Rectangle _imgRect;
private void pictureBox1_MouseUp(
object sender, MouseEventArgs e) {
var c = sender as PictureBox;
if (null == c) return;
_dragging = false;
}
private void pictureBox1_MouseDown(
object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) return;
_dragging = true;
_xPos = e.X;
_yPos = e.Y;
}
private void pictureBox1_MouseMove(
object sender, MouseEventArgs e) {
if (!_dragging || _img == null ) return;
if (e.Button == MouseButtons.Left) {
_imgRect = new Rectangle(-(_xPos-e.X), -(_yPos - e.Y), _img.Width, _img.Height);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(
object sender, PaintEventArgs e) {
if (_img != null) {
e.Graphics.DrawImage(_img, _imgRect);
}
}