ASP.net c#调整图像大小SQL上载
在上传到数据库之前,我一直在寻找调整图像大小的方法。现在这些文件只是上传,如果它们的大小不正确,那么我的页面看起来就像一团糟。如何调整图片大小在上传到数据库之前,我想上传一张原始大小的图片,并且大小正确。ASP.net是否可以实现这一点。我看过一些关于图像大小调整的教程,但没有一个是有帮助的,如果有人能帮忙的话,那就太好了。我开始看教程,但无法在我的SQL上传中实现它ASP.net c#调整图像大小SQL上载,c#,asp.net,sql,image-uploading,C#,Asp.net,Sql,Image Uploading,在上传到数据库之前,我一直在寻找调整图像大小的方法。现在这些文件只是上传,如果它们的大小不正确,那么我的页面看起来就像一团糟。如何调整图片大小在上传到数据库之前,我想上传一张原始大小的图片,并且大小正确。ASP.net是否可以实现这一点。我看过一些关于图像大小调整的教程,但没有一个是有帮助的,如果有人能帮忙的话,那就太好了。我开始看教程,但无法在我的SQL上传中实现它 谢谢诸如此类的东西,我使用的是MVC,因此是HttpPostedFileBase。但是,这将获取文件的输入输入类型并返回一个字节
谢谢诸如此类的东西,我使用的是MVC,因此是
HttpPostedFileBase
。但是,这将获取文件的输入
输入类型并返回一个字节数组,非常适合上传到DB
using System.Drawing;
using System.Drawing.Drawing2D;
private static byte[] PrepImageForUpload(HttpPostedFileBase FileData)
{
using (Bitmap origImage = new Bitmap(FileData.InputStream))
{
int maxWidth = 165;
int newWidth = origImage.Width;
int newHeight = origImage.Height;
if (origImage.Width < newWidth) //Force to max width
{
newWidth = maxWidth;
newHeight = origImage.Height * maxWidth / origImage.Width;
}
using (Bitmap newImage = new Bitmap(newWidth, newHeight))
{
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(origImage, new Rectangle(0, 0, newWidth, newHeight));
MemoryStream ms = new MemoryStream();
newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
}
}
使用系统图;
使用System.Drawing.Drawing2D;
私有静态字节[]PrepImageForUpload(HttpPostedFileBase文件数据)
{
使用(位图origImage=新位图(FileData.InputStream))
{
int maxWidth=165;
int newWidth=origImage.Width;
int newHeight=origImage.Height;
if(origImage.Width
您能分享您迄今为止尝试的代码吗?使用该库可获得最佳图像质量和正确的内存管理。使用(MemoryStream ms=new MemoryStream()){ImageBuilder.Current.Build(postedFile,ms,new ResizeSettings(“maxwidth=800&maxheight=600&format=jpg”);返回ms.ToArray();}