ASP.net c#调整图像SQL上传

时间:2021-07-29 03:44:47

I have been looking all over for a way to resize an image before it is uploaded to my database. Right now the files are just upload and if they are not the correct size then my pages look like a mess. How would I resize the image before I upload it to my database, I would like to upload an original sized image, and correct size. Is this possible with ASP.net. I have seen some tutorials on image resizing but none of them were helpful, if anyone can help that would be great. I started looking at this tutorial but wasnt able to implement it in my SQL upload.

在将图像上传到我的数据库之前,我一直在寻找一种调整图像大小的方法。现在文件只是上传,如果它们的大小不正确,那么我的页面看起来就像一团糟。在将图像上传到我的数据库之前,我将如何调整图像大小,我想上传原始尺寸的图像,并且尺寸正确。这可能与ASP.net。我已经看过一些关于图像大小调整的教程,但没有一个是有帮助的,如果有人可以提供帮助那就太棒了。我开始查看本教程,但无法在我的SQL上传中实现它。

Thanks

1 个解决方案

#1


2  

Something like this, I am using MVC thus the HttpPostedFileBase. However this is taking the input of a file input type and returning a byte array, perfect for uploading to DB.

像这样,我使用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();
            }
        }
    }
}

#1


2  

Something like this, I am using MVC thus the HttpPostedFileBase. However this is taking the input of a file input type and returning a byte array, perfect for uploading to DB.

像这样,我使用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();
            }
        }
    }
}