ASP.net C# 调整图像大小 SQL 上传

发布于 2024-10-22 00:56:46 字数 304 浏览 0 评论 0原文

我一直在寻找一种在将图像上传到数据库之前调整图像大小的方法。现在文件刚刚上传,如果它们的大小不正确,那么我的页面看起来会一团糟。在将图像上传到数据库之前,如何调整图像大小,我想上传原始大小的图像和正确的大小。这可以通过 ASP.net 实现吗?我看过一些有关图像调整大小的教程,但没有一个有帮助,如果有人可以提供帮助那就太好了。我开始查看 this 教程,但无法在我的 SQL 中实现它上传。

谢谢

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-10-29 00:56:46

像这样,我使用 MVC,因此使用 HttpPostedFileBase。然而,这是获取文件输入类型的输入并返回字节数组,非常适合上传到数据库。

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();
            }
        }
    }
}

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.

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();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文