把这看作是对
Reza Aghaei answer
.
一个专门的类,它提供一些帮助工具来确定选择的比例因子,并将选择坐标转换为比例
Bitmap
协调。
这个
版本
仅用于缩放图像。
这个
ZoomFactor
类提供了这些方法
:
PointF TranslateZoomPosition(PointF Coordinates, SizeF ContainerSize, SizeF ImageSize)
:
返回
PointF
将容器内点位置的坐标转换为位图内的点位置,并放大容器。
RectangleF TranslateZoomSelection(RectangleF Selection, SizeF ContainerSize, SizeF ImageSize)
:
返回A
RectangleF
表示在容器内创建的选定内容,并转换为位图坐标。
RectangleF TranslateSelectionToZoomedSel(RectangleF SelectionRect, SizeF ContainerSize, SizeF ImageSize)
:
返回A
矩形F
表示转换为容器内缩放选择图像的原始位图的预选区域。
PointF GetImageScaledOrigin(SizeF ContainerSize, SizeF ImageSize)
以下内容:
返回
波因特
容器内缩放图像原点坐标的引用。
SizeF GetImageScaledSize(SizeF ContainerSize, SizeF ImageSize)
以下内容:
返回
SizeF
在容器内缩放时的图像引用。
示例用法,显示如何使用容器控件内创建的选择矩形裁剪位图。这个
TranslateZoomSelection
方法返回与选择区域对应的位图部分:
ZoomFactor ZoomHelper = new ZoomFactor()
Bitmap originalBitmap;
RectangleF currentSelection = [Current Selection Rectangle];
RectangleF bitmapRect = ZoomHelper.TranslateZoomSelection(currentSelection, [Container].Size, originalBitmap.Size);
using (Bitmap croppedBitmap = new Bitmap((int)bitmapRect.Width, (int)bitmapRect.Height, originalBitmap.PixelFormat))
using (Graphics g = Graphics.FromImage(croppedBitmap))
{
g.DrawImage(originalBitmap, new Rectangle(Point.Empty, Size.Round(bitmapRect.Size)),
bitmapRect, GraphicsUnit.Pixel);
[Container].Image = (Bitmap)croppedBitmap.Clone();
}
上述行为示例
:
注释
以下内容:
在本例中,纵向图像的预选择反转
Width
和
Height
这个
动物因子
班
:
public class ZoomFactor
{
public ZoomFactor() { }
public PointF TranslateZoomPosition(PointF Coordinates, SizeF ContainerSize, SizeF ImageSize)
{
PointF imageOrigin = TranslateCoordinatesOrigin(Coordinates, ContainerSize, ImageSize);
float scaleFactor = GetScaleFactor(ContainerSize, ImageSize);
return new PointF(imageOrigin.X / scaleFactor, imageOrigin.Y / scaleFactor);
}
public RectangleF TranslateZoomSelection(RectangleF SelectionRect, SizeF ContainerSize, SizeF ImageSize)
{
PointF selectionTrueOrigin = TranslateZoomPosition(SelectionRect.Location, ContainerSize, ImageSize);
float scaleFactor = GetScaleFactor(ContainerSize, ImageSize);
SizeF selectionTrueSize = new SizeF(SelectionRect.Width / scaleFactor, SelectionRect.Height / scaleFactor);
return new RectangleF(selectionTrueOrigin, selectionTrueSize);
}
public RectangleF TranslateSelectionToZoomedSel(RectangleF SelectionRect, SizeF ContainerSize, SizeF ImageSize)
{
float scaleFactor = GetScaleFactor(ContainerSize, ImageSize);
RectangleF zoomedSelectionRect = new
RectangleF(SelectionRect.X * scaleFactor, SelectionRect.Y * scaleFactor,
SelectionRect.Width * scaleFactor, SelectionRect.Height * scaleFactor);
PointF imageScaledOrigin = GetImageScaledOrigin(ContainerSize, ImageSize);
zoomedSelectionRect.Location = new PointF(zoomedSelectionRect.Location.X + imageScaledOrigin.X,
zoomedSelectionRect.Location.Y + imageScaledOrigin.Y);
return zoomedSelectionRect;
}
public PointF TranslateCoordinatesOrigin(PointF Coordinates, SizeF ContainerSize, SizeF ImageSize)
{
PointF imageOrigin = GetImageScaledOrigin(ContainerSize, ImageSize);
return new PointF(Coordinates.X - imageOrigin.X, Coordinates.Y - imageOrigin.Y);
}
public PointF GetImageScaledOrigin(SizeF ContainerSize, SizeF ImageSize)
{
SizeF imageScaleSize = GetImageScaledSize(ContainerSize, ImageSize);
return new PointF((ContainerSize.Width - imageScaleSize.Width) / 2,
(ContainerSize.Height - imageScaleSize.Height) / 2);
}
public SizeF GetImageScaledSize(SizeF ContainerSize, SizeF ImageSize)
{
float scaleFactor = GetScaleFactor(ContainerSize, ImageSize);
return new SizeF(ImageSize.Width * scaleFactor, ImageSize.Height * scaleFactor);
}
internal float GetScaleFactor(SizeF Scaled, SizeF Original)
{
return (Original.Width > Original.Height) ? (Scaled.Width / Original.Width)
: (Scaled.Height / Original.Height);
}
}