我想为我的 Asp.Net 网站项目(如 timthumb)创建图像调整大小机制。我想做这个:
- 上传图片
- 设置大小
- 如果我需要此图像的其他尺寸,我可以提供“仅”尺寸而无需再次上传。
我该怎么做,你有什么建议吗?
这是我的调整大小功能:
public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
{
try
{
Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));
var myBrush = new SolidBrush(Color.FromArgb(64, 205, 205, 205));
double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);
var containerBox = new Rectangle();
containerBox.X = (int)(diagonal / 10);
var messageLength = (float)(diagonal / message.Length * 1);
containerBox.Y = -(int)(messageLength / 1.6);
var stringFont = new Font("verdana", messageLength);
var sf = new StringFormat();
var slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);
gr.RotateTransform(slope);
gr.DrawString(message, stringFont, myBrush, containerBox, sf);
return newImage;
}
}
catch (Exception exc)
{
throw exc;
}
}
public int Calculations(decimal orjWidth, decimal orjHeight, int newWidth)
{
decimal height = 0;
decimal ratio = 0;
if (newWidth < orjWidth)
{
ratio = orjWidth / newWidth;
height = orjHeight / ratio;
return height.To<int>();
}
if (orjWidth <= newWidth)
{
ratio = newWidth / orjWidth;
height = orjHeight * ratio;
return height.To<int>();
}
return height.To<int>();
}