当您使用控件作为图像的容器并且图像被缩放以适应容器的边界(例如,将PictureBox.SizeMode 设置为PictureBoxSizeMode.Zoom)以便图像可以使用预定义的度量显示在 UI 中时,当您需要选择图像的一部分时,您需要计算比例因子。也就是说,确定容器大小与Image真实大小的比值。
使用较小的容器作为参考可能会更好,因此您可以将相对度量乘以比例而不是除以比例:
private float GetImageScaledRatio(RectangleF canvas, SizeF imageSize)
{
return Math.Max(canvas.Width, canvas.Height) /
Math.Max(imageSize.Width, imageSize.Height);
}
镜头在容器内的位置 - 如果您希望镜头跟随鼠标指针的位置 - 由指针坐标减去镜头尺寸的一半给出:
private PointF GetLensPosition(PointF centerPosition, RectangleF lens)
{
return new PointF(centerPosition.X - (lens.Width / 2),
centerPosition.Y - (lens.Height / 2));
}
要确定镜头(选择)尺寸相对于位图实际尺寸的实际尺寸,当需要绘制或剪裁位图的一部分时,必须缩放镜头尺寸:
private SizeF GetScaledLensSize(RectangleF canvas, SizeF imageSize, SizeF lensSize)
{
float scaleRatio = GetImageScaledRatio(canvas, imageSize);
return new SizeF(lensSize.Width * scaleRatio, lensSize.Width * scaleRatio);
}
另外,在显示当前由 Lens 表示的选择的预览时,选择需要缩放到用于预览 Lens 选择的 Container 的大小:
private RectangleF CanvasToImageRect(RectangleF canvas, SizeF imageSize, RectangleF rect)
{
float scaleRatio = GetImageScaledRatio(canvas, imageSize);
return new RectangleF(new PointF(rect.X / scaleRatio, rect.Y / scaleRatio),
new SizeF(rect.Width / scaleRatio, rect.Height / scaleRatio));
}
这些简单的方法允许计算与所考虑的图像相关的选择的实际大小以及用于预览的控件的大小。
使用镜头选区绘制预览时,最好使用常用方法来绘制图像部分:该方法也可用于在新位图中绘制选区,然后可以保存光盘或以其他方式存储。
这里,pctLens 是用于预览的 PictureBox,RectangleF section 是重新调整为 pctLens 大小(用于预览)的镜头尺寸,以及当然sourceImage是原图:
private void pctLens_Paint(object sender, PaintEventArgs e)
{
RectangleF section = CanvasToImageRect(pctOriginal.ClientRectangle, sourceImage.Size, imageLens);
DrawImageSelection(e.Graphics, pctLens.ClientRectangle, section, sourceImage);
}
private void DrawImageSelection(Graphics g, RectangleF canvas, RectangleF imageSection, Image image)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, canvas, imageSection, GraphicsUnit.Pixel);
switch (lensType)
{
case LensType.Circular:
using (var path = new GraphicsPath())
{
path.AddEllipse(canvas);
g.SetClip(path, CombineMode.Exclude);
using (var brush = new SolidBrush(Color.FromArgb(160, Color.Black)))
{
g.FillRectangle(brush, canvas);
g.ResetClip();
using (var pen = new Pen(brush, 1f))
g.DrawEllipse(pen, canvas);
}
}
break;
case LensType.Rectangular:
// NOP
break;
}
}
视觉效果(图片:1200x675,PictureBox:300x175,SizeMode: Zoom)
完整的源代码重现动画中显示的内容:
Bitmap sourceImage 是原始位图,必须设置为现有对象。
RectangleF imageLens 是用于定义相对镜头尺寸的形状.
Size lensPixelSize 是 imageLens 的像素大小,相对于 UI 表示。
pctOriginal 是原始图像所在的 PictureBox
pctLens 是绘制镜头部分预览的 PictureBox。
Bitmap sourceImage = null;
RectangleF imageLens = RectangleF.Empty;
Size lensPixelSize = new Size(100, 100);
LensType lensType = LensType.Circular;
bool lensUseRelativeSize = false;
bool drawLens = false;
private enum LensType
{
Circular,
Rectangular
}
private void pctOriginal_MouseMove(object sender, MouseEventArgs e)
{
imageLens.Location = GetLensPosition(e.Location, imageLens);
imageLens.Size = lensUseRelativeSize
? GetScaledLensSize(pctOriginal.ClientRectangle, sourceImage.Size, lensPixelSize)
: lensPixelSize;
pctOriginal.Invalidate();
pctLens.Invalidate();
}
private PointF GetLensPosition(PointF centerPosition, RectangleF rect)
{
return new PointF(centerPosition.X - (rect.Width / 2),
centerPosition.Y - (rect.Height / 2));
}
private SizeF GetScaledLensSize(RectangleF canvas, SizeF imageSize, SizeF lensSize)
{
float scaleRatio = GetImageScaledRatio(canvas, imageSize);
return new SizeF(lensSize.Width * scaleRatio, lensSize.Width * scaleRatio);
}
private float GetImageScaledRatio(RectangleF canvas, SizeF imageSize)
{
return Math.Max(canvas.Width, canvas.Height) /
Math.Max(imageSize.Width, imageSize.Height);
}
private RectangleF CanvasToImageRect(RectangleF canvas, SizeF imageSize, RectangleF rect)
{
float scaleRatio = GetImageScaledRatio(canvas, imageSize);
return new RectangleF(new PointF(rect.X / scaleRatio, rect.Y / scaleRatio),
new SizeF(rect.Width / scaleRatio, rect.Height / scaleRatio));
}
private void pctOriginal_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2.0f))
{
pen.DashStyle = DashStyle.Dash;
switch (lensType)
{
case LensType.Circular:
e.Graphics.DrawEllipse(pen, Rectangle.Round(imageLens));
break;
case LensType.Rectangular:
e.Graphics.DrawRectangle(pen, Rectangle.Round(imageLens));
break;
}
}
}
private void pctLens_Paint(object sender, PaintEventArgs e)
{
if (!drawLens) return;
RectangleF section = CanvasToImageRect(pctOriginal.ClientRectangle, sourceImage.Size, imageLens);
DrawImageSelection(e.Graphics, pctLens.ClientRectangle, section, sourceImage);
}
private void DrawImageSelection(Graphics g, RectangleF canvas, RectangleF imageSection, Image image)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, canvas, imageSection, GraphicsUnit.Pixel);
switch (lensType)
{
case LensType.Circular:
using (var path = new GraphicsPath())
{
path.AddEllipse(canvas);
g.SetClip(path, CombineMode.Exclude);
using (var brush = new SolidBrush(Color.FromArgb(160, Color.Black)))
{
g.FillRectangle(brush, canvas);
g.ResetClip();
using (var pen = new Pen(brush, 1f))
g.DrawEllipse(pen, canvas);
}
}
break;
case LensType.Rectangular:
// NOP
break;
}
}
private void chkSizeRelative_CheckedChanged(object sender, EventArgs e)
=> lensUseRelativeSize = chkSizeRelative.Checked;
private void radLensType_CheckedChanged(object sender, EventArgs e)
=> lensType = (LensType)(int.Parse((sender as Control).Tag.ToString()));
private void pctOriginal_MouseEnter(object sender, EventArgs e)
=> drawLens = true;
private void pctOriginal_MouseLeave(object sender, EventArgs e)
{
drawLens = false;
pctLens.Invalidate();
}