C# Asp.Net:动态图像大小调整
我想为我的Asp.Net网站项目(如timthumb)创建一个图像大小调整机制。我想这样做:C# Asp.Net:动态图像大小调整,c#,asp.net,image-resizing,timthumb,C#,Asp.net,Image Resizing,Timthumb,我想为我的Asp.Net网站项目(如timthumb)创建一个图像大小调整机制。我想这样做: 上传图像 定一个尺寸 如果我需要这个图像的另一个大小,我可以给“只”的大小,而无需再次上传 我怎么做,你有什么建议吗 以下是我的调整大小功能: public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message) { try { Bitmap n
- 上传图像
- 定一个尺寸
- 如果我需要这个图像的另一个大小,我可以给“只”的大小,而无需再次上传
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>();
}
公共位图调整大小(位图图像、int-newWidth、int-newHeight、字符串消息)
{
尝试
{
位图newImage=新位图(newWidth,计算(image.Width,image.Height,newWidth));
使用(Graphics gr=Graphics.FromImage(newImage))
{
gr.SmoothingMode=SmoothingMode.AntiAlias;
gr.InterpolationMode=InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode=PixelOffsetMode.HighQuality;
gr.DrawImage(图像,新矩形(0,0,newImage.Width,newImage.Height));
var myBrush=新的SolidBrush(Color.FromArgb(64205205205));
双对角线=Math.Sqrt(newImage.Width*newImage.Width+newImage.Height*newImage.Height);
var containerBox=新矩形();
containerBox.X=(int)(对角线/10);
var messageLength=(float)(对角线/消息长度*1);
containerBox.Y=-(int)(messageLength/1.6);
var stringFont=新字体(“verdana”,messageLength);
var sf=新的StringFormat();
var slope=(float)(Math.Atan2(newImage.Height,newImage.Width)*180/Math.PI);
gr.旋转变换(坡度);
gr.DrawString(消息、stringFont、myBrush、containerBox、sf);
返回新图像;
}
}
捕获(异常exc)
{
抛出exc;
}
}
公共整数计算(十进制orjWidth、十进制orjHeight、整数newWidth)
{
十进制高度=0;
十进制比率=0;
if(新宽度<或JWidth)
{
比率=orjWidth/newWidth;
高度=或高度/比率;
返回高度.To();
}
如果(或)你正在寻找
如果您使用NuGet,您可以安装包ImageResizer.MvcWebConfig
,这是通过jQuery脚本完成的
通过使用一些可用的jQuery插件,您可以实现相同的背景图像动态调整大小效果。例如:
此外,也可以通过使用普通CSS3来实现:
致以最诚挚的问候,templateMonster附属团队!如果您不想再次上传图像,您需要将其保存在某个位置,可能是磁盘上。一般代码清洁说明:将大小调整和文本绘制位放在同一个函数中可能不是一个好主意。