裁剪代码向右偏移,而不是鼠标位置
Posted
技术标签:
【中文标题】裁剪代码向右偏移,而不是鼠标位置【英文标题】:Crop code is offset to the right and not the on the mouse position 【发布时间】:2020-11-14 15:39:21 【问题描述】:我一直在为我的数据库管理系统制作裁剪表格。但是有一个烦人的警告,当我按下鼠标左键时,绘制的矩形向右偏移并且没有从鼠标位置开始。
这是我的代码。如果你能告诉我我在哪里搞砸了,我将不胜感激。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace Student_File
public partial class fUpload : Form
public fUpload()
InitializeComponent();
private Bitmap OriginalImage;
// True when we're selecting a rectangle.
private bool IsSelecting = false;
// The area we are selecting.
private int X0, Y0, X1, Y1;
private void BtnUpload_Click(object sender, EventArgs e)
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
if (openFileDialog1.ShowDialog() == DialogResult.OK)
lblFileName.Text = Path.GetFileName(openFileDialog1.FileName);
pictureBoxOriginal.Image = Image.FromFile(openFileDialog1.FileName);
OriginalImage = (Bitmap)pictureBoxOriginal.Image;
private void btnExit_Click(object sender, EventArgs e)
Application.Exit();
private void btnSave_Click(object sender, EventArgs e)
pictureBoxCropped.Image.Save(@""+ Environment.CurrentDirectory +"Temp/Temp.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
private void pb1_MouseUp(object sender, MouseEventArgs e)
// Do nothing it we're not selecting an area.
if (!IsSelecting) return;
IsSelecting = false;
// Display the original image.
pictureBoxOriginal.Image = OriginalImage;
// Copy the selected part of the image.
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(OriginalImage, dest_rectangle,
source_rectangle, GraphicsUnit.Pixel);
// Display the result.
pictureBoxCropped.Image = area;
private void pb1_MouseDown(object sender, MouseEventArgs e)
IsSelecting = true;
// Save the start point.
X0 = e.X;
Y0 = e.Y;
private void pb1_MouseMove(object sender, MouseEventArgs e)
// Do nothing it we're not selecting an area.
if (!IsSelecting) return;
// Save the new point.
X1 = e.X;
Y1 = e.Y;
// Make a Bitmap to display the selection rectangle.
Bitmap bmp = new Bitmap(OriginalImage);
// Draw the rectangle.
using (Graphics gr = Graphics.FromImage(bmp))
gr.DrawRectangle(Pens.Red,
Math.Min(X0, X1), Math.Min(Y0, Y1),
Math.Abs(X0 - X1), Math.Abs(Y0 - Y1));
// Display the temporary bitmap.
pictureBoxOriginal.Image = bmp;
【问题讨论】:
PictureBox.SizeMode 属性很重要。 ***.com/questions/52278722/… 【参考方案1】:你需要从表单中获取偏移量
PropertyInfo imageRectProperty = typeof(PictureBox).GetProperty("ImageRectangle", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance);
var value = imageRectProperty.GetValue(pictureBoxOriginal, null);
if (value == null)
return;
Rectangle rectangle = (Rectangle)value;
if (rectangle.Contains(e.Location))
// this is your coordinate on image, not on form
var y = e.Y - rectangle.Y;
var x = e.X - rectangle.X;
【讨论】:
感谢您的快速回答,但版主可能删掉了我是 c# 新手的事实,您能否解释一下如何抵消表格,因为我无所适从。跨度> 我添加了一张带说明的图片。以上是关于裁剪代码向右偏移,而不是鼠标位置的主要内容,如果未能解决你的问题,请参考以下文章