开发快递图片编辑裁剪问题
本文关键字:裁剪 问题 编辑 快递 开发 | 更新日期: 2023-09-27 18:32:10
我有一个比图片编辑框大的图像,问题是当用鼠标创建矩形时,图像上的正确点没有被选中。似乎它正在选择图片编辑中的点,而不是它自己的图像。 我正在尝试裁剪图像并将裁剪后的图像放置在新的图片编辑中。下面的示例代码。
private void originalPictureEdit_MouseDown(object sender, MouseEventArgs e)
{
//Point TextStartLocation = e.Location;
Cursor = Cursors.IBeam;
if (_drawStarted == false)
{
_drawStarted = true;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
_cropX = e.X;
_cropY = e.Y;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
_cropPen = new Pen(Color.Black, 1);
_cropPen.DashStyle = DashStyle.DashDotDot;
}
}
originalPictureEdit.Refresh();
}
private void originalPictureEdit_MouseMove(object sender, MouseEventArgs e)
{
if (originalPictureEdit.Image == null)
return;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
originalPictureEdit.Refresh();
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;
originalPictureEdit.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _selection.Width, _selection.Height);
}
}
private void originalPictureEdit_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size())
{
_croppedImage = (_displayedImage as Bitmap).Clone(_selection, _displayedImage.PixelFormat);
modifiedPictureEdit.Image = _croppedImage;
modifiedPictureEdit.Width = _croppedImage.Width;
modifiedPictureEdit.Height = _croppedImage.Height;
}
Cursor = Cursors.Default;
_drawStarted = false;
}
我需要找到滚动条的位置。以下是MouseUp的工作代码:
private void pictureEdit1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size())
{
PictureEditViewInfo viewInfo = originalPictureEdit.GetViewInfo() as PictureEditViewInfo;
PropertyInfo pr = viewInfo.GetType().GetProperty("HScrollBarPosition", BindingFlags.Instance | BindingFlags.NonPublic);
int fHScrollBarPosition = (int)pr.GetValue(viewInfo, null);
pr = viewInfo.GetType().GetProperty("VScrollBarPosition", BindingFlags.Instance | BindingFlags.NonPublic);
int fVScrollBarPosition = (int)pr.GetValue(viewInfo, null);
_selection.X += fHScrollBarPosition;
_selection.Y += fVScrollBarPosition;
Crop();
}
Cursor = Cursors.Default;
_drawStarted = false;
}
PictureEdit 控件可以显示具有偏移或缩放的图像,因此应在计算中接受这些因素。我相信以下方法可能对您有所帮助:
PictureEditViewInfo viewInfo = GetViewInfo(originalPictureEdit);
Rectangle cropRect = new Rectangle(
_selection.X - viewInfo.PictureStartX,
_selection.Y - viewInfo.PictureStartY,
_selection.Width,
_selection.Height);
_croppedImage = (originalPictureEdit.Image as Bitmap).Clone(cropRect,
originalPictureEdit.Image.PixelFormat);
//...
static PictureEditViewInfo GetViewInfo(PictureEdit edit) {
PropertyInfo pInfo = typeof(BaseEdit).GetProperty("ViewInfo",
BindingFlags.Instance | BindingFlags.NonPublic);
return (PictureEditViewInfo)pInfo.GetValue(edit, new object[] { });
}
另外,我建议您用以下内容替换图片编辑图形上的直接绘图:
//...
Point frameLocation = originalPictureEdit.PointToScreen(_selection.Location);
ControlPaint.DrawReversibleFrame(
new Rectangle(frameLocation, _selection.Size), Color.Black, FrameStyle.Dashed);