using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ImageCompressor { /// /// 计算保存Zoom的数据 /// class ZoomFactor { static double zoomPercent = 0.1; /// /// [0.1 - 1] /// 对原图像的最短一边的像素 * value,作为新方形图像的一边像素值。 /// public static double ZoomPercent { get { return zoomPercent; } set { if (value < 0.01) zoomPercent = 0.01; else if (value > 1) zoomPercent = 1; else zoomPercent = value; } } /// /// 新图像宽度的百分比 /// double widthLocationPercent; /// /// 新图像高度的百分比 /// double heightLocationPercent; public Rectangle GetSourceImage(Image image) { int rawImageHeight= image.Height; int rawImageWidth= image.Width; int NewImageX = (int)(rawImageWidth * widthLocationPercent); int NewImageY = (int)(rawImageHeight * heightLocationPercent); int NewImageSize = (int)(Math.Min(rawImageHeight, rawImageWidth) * zoomPercent); return new Rectangle(NewImageX - NewImageSize/2, NewImageY - NewImageSize / 2, NewImageSize, NewImageSize); } /// /// 获取当前缩放数据 /// /// /// /// /// public ZoomFactor(Rectangle previewBoxRectangle, Point location, int imageWidth, int imageHeight) { double X = location.X; // - previewBoxRectangle.X; double Y = location.Y;// - previewBoxRectangle.Y; if ((double)imageWidth / (double)imageHeight > (double)previewBoxRectangle.Width / (double)previewBoxRectangle.Height) { double truePreviewBoxHeight = previewBoxRectangle.Width * ((double)imageHeight / (double)imageWidth); double offsetY = (previewBoxRectangle.Height - truePreviewBoxHeight) * 0.5; //图像比预览框更宽 widthLocationPercent = X / (double)previewBoxRectangle.Width; heightLocationPercent = (Y - offsetY) / truePreviewBoxHeight; } else { double truePreivewBoxWidth = previewBoxRectangle.Height * ((double)imageWidth / (double)imageHeight); double offsetX = (previewBoxRectangle.Width - truePreivewBoxWidth) * 0.5; widthLocationPercent = (X - offsetX) / truePreivewBoxWidth; heightLocationPercent = Y / (double)previewBoxRectangle.Height; } //Debug.WriteLine("On Move percent w = {0}, h = {1}", widthLocationPercent, heightLocationPercent); } } }