📄 impscaletransform.cs
字号:
namespace Imps.Utils
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
public class ImpScaleTransform : IImageTransform
{
protected int _height;
protected bool _keepRatio;
protected int _width;
public ImpScaleTransform(int width, int height) : this(width, height, false)
{
}
public ImpScaleTransform(int width, int height, bool keepRatio)
{
this._width = width;
this._height = height;
this._keepRatio = keepRatio;
}
public virtual IImage Transform(IImage image)
{
IImage image5;
if (image == null)
{
throw new ArgumentNullException("image");
}
int width = this.Width;
int height = this.Height;
int x = 0;
int y = 0;
IImage image2 = null;
try
{
image2 = (IImage) image.Clone();
using (Image image3 = image.Image)
{
using (Image image4 = new Bitmap(this.Width, this.Height, image3.PixelFormat))
{
using (Graphics graphics = Graphics.FromImage(image4))
{
float num5 = image.Width;
float num6 = image.Height;
float num7 = num5 / num6;
float num8 = ((float) this.Width) / ((float) this.Height);
if (this._keepRatio && (num5 != num6))
{
float num9;
if (num7 < num8)
{
num9 = ((float) this.Height) / num6;
}
else
{
num9 = ((float) this.Width) / num5;
}
height = Convert.ToInt32((float) (num6 * num9));
width = Convert.ToInt32((float) (num5 * num9));
x = (this.Width - width) / 2;
y = (this.Height - height) / 2;
}
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image3, new Rectangle(x, y, width, height), new Rectangle(0, 0, (int) num5, (int) num6), GraphicsUnit.Pixel);
}
image2.Image = image4;
}
}
image5 = image2;
}
catch (Exception exception)
{
if (image2 != null)
{
image2.Dispose();
}
throw exception;
}
return image5;
}
public int Height
{
get
{
return this._height;
}
set
{
this._height = value;
}
}
public bool KeepRatio
{
get
{
return this._keepRatio;
}
set
{
this._keepRatio = value;
}
}
public int Width
{
get
{
return this._width;
}
set
{
this._width = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -