我一直在寻找一种在将图像上传到我的数据库之前调整图像大小的方法。现在文件刚刚上传,如果它们的大小不正确,那么我的页面看起来就像一团糟。在将图像上传到数据库之前如何调整图像大小,我想上传原始大小的图像和正确的大小。这可能与 ASP.net。我看过一些关于图像大小调整的教程,但没有一个有帮助,如果有人能提供帮助,那就太好了。我开始查看本教程,但无法在我的 SQL 上传中实现它。
谢谢
我一直在寻找一种在将图像上传到我的数据库之前调整图像大小的方法。现在文件刚刚上传,如果它们的大小不正确,那么我的页面看起来就像一团糟。在将图像上传到数据库之前如何调整图像大小,我想上传原始大小的图像和正确的大小。这可能与 ASP.net。我看过一些关于图像大小调整的教程,但没有一个有帮助,如果有人能提供帮助,那就太好了。我开始查看本教程,但无法在我的 SQL 上传中实现它。
谢谢
像这样的东西,我正在使用 MVC,因此HttpPostedFileBase
. 然而,这是获取file
输入类型的输入并返回一个字节数组,非常适合上传到数据库。
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();
}
}
}
}