88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ImageCompressor
|
||
{
|
||
|
||
|
||
/// <summary>
|
||
/// 计算保存Zoom的数据
|
||
/// </summary>
|
||
class ZoomFactor
|
||
{
|
||
static double zoomPercent = 0.1;
|
||
/// <summary>
|
||
/// [0.1 - 1]
|
||
/// 对原图像的最短一边的像素 * value,作为新方形图像的一边像素值。
|
||
/// </summary>
|
||
public static double ZoomPercent
|
||
{
|
||
get
|
||
{
|
||
return zoomPercent;
|
||
}
|
||
set
|
||
{
|
||
if (value < 0.01) zoomPercent = 0.01;
|
||
else if (value > 1) zoomPercent = 1;
|
||
else zoomPercent = value;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 新图像宽度的百分比
|
||
/// </summary>
|
||
double widthLocationPercent;
|
||
/// <summary>
|
||
/// 新图像高度的百分比
|
||
/// </summary>
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前缩放数据
|
||
/// </summary>
|
||
/// <param name="previewBoxRectangle"></param>
|
||
/// <param name="location"></param>
|
||
/// <param name="imageWidth"></param>
|
||
/// <param name="imageHeight"></param>
|
||
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);
|
||
}
|
||
}
|
||
|
||
}
|