使用 ASP.NET 调整图像大小并保存到数据库

发布于 2024-08-25 07:08:32 字数 253 浏览 5 评论 0原文

我需要拍摄上传的图像,调整其大小,然后将其保存到数据库中。很简单,只是我无权将任何临时文件保存到服务器。我正在拍摄图像,将其大小调整为位图,并需要将其作为原始图像类型(例如 JPG)保存到数据库字段中。我怎样才能像这样获得 FileBytes() ,以便将其保存到数据库中?

在我使用 ImageUpload.FileBytes() 之前,但现在我正在调整大小,我正在处理图像和位图而不是 FileUploads,并且似乎找不到任何可以给我字节的东西。

谢谢!

I need to take an uploaded image, resize it, and save it to the database. Simple enough, except I don't have access to save any temp files to the server. I'm taking the image, resizing it as a Bitmap, and need to save it to a database field as the original image type (JPG for example). How can I get the FileBytes() like this, so I can save it to the database?

Before I was using ImageUpload.FileBytes() but now that I'm resizing I'm dealing with Images and Bitmaps instead of FileUploads and can't seem find anything that will give me the bytes.

Thanks!

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

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

发布评论

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

评论(3

心房敞 2024-09-01 07:08:32

实际上没那么简单...有 28 个不明显的陷阱您应该注意调整图像大小时。最好使用我的免费开源库来处理所有编码问题并避免 GDI 错误。

以下是如何获取调整大小、裁剪并转换为 Jpeg 格式后,每个上传文件的编码 byte[] 数组。

using ImageResizer;
using ImageResizer.Encoding;


//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys) {
    HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];

    //You can specify any of 30 commands.. See http://imageresizing.net
    ResizeSettings resizeCropSettings = 
        new ResizeSettings("width=200&height=200&format=jpg&crop=auto");

    using (MemoryStream ms = new MemoryStream()) {
        //Resize the image
        ImageBuilder.Current.Build(file, ms, resizeCropSettings);

        //Upload the byte array to SQL: ms.ToArray();
    }
}

使用 MS SQL 存储图像也是一个坏主意。请参阅我的 斯科特·汉塞尔曼的播客了解更多信息。

It's actually not so simple... there are 28 non-obvious pitfalls you should watch out for when doing image resizing. It's best to use my free, open-source library to handle all the encoding issues and avoid the GDI bugs.

Here's how to get an encoded byte[] array for each uploaded file, after resizing, cropping, and converting to Jpeg format.

using ImageResizer;
using ImageResizer.Encoding;


//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys) {
    HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];

    //You can specify any of 30 commands.. See http://imageresizing.net
    ResizeSettings resizeCropSettings = 
        new ResizeSettings("width=200&height=200&format=jpg&crop=auto");

    using (MemoryStream ms = new MemoryStream()) {
        //Resize the image
        ImageBuilder.Current.Build(file, ms, resizeCropSettings);

        //Upload the byte array to SQL: ms.ToArray();
    }
}

It's also a bad idea to use MS SQL for storing images. See my podcast with Scott Hanselman for more info.

赠我空喜 2024-09-01 07:08:32

请参阅在不损失任何质量的情况下调整图像大小,然后您可以编写图像(Bitmap.SaveToStream) 到 MemoryStream 并调用 ToArray 来获取字节。

See Resizing an Image without losing any quality You can then write your image (Bitmap.SaveToStream) to a MemoryStream and call ToArray to get the bytes.

一个人的旅程 2024-09-01 07:08:32

这就是我调整图像大小所做的事情。

    private byte[] toBytes(Image image)
    {            
        Bitmap resized = new Bitmap(image, yourWidth, yourHeight);            
        System.IO.MemoryStream ms = new System.IO.MemoryStream();            
        resized.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        resized.Dispose();
        return ms.ToArray();            
    }

This is what I've done to resize images.

    private byte[] toBytes(Image image)
    {            
        Bitmap resized = new Bitmap(image, yourWidth, yourHeight);            
        System.IO.MemoryStream ms = new System.IO.MemoryStream();            
        resized.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        resized.Dispose();
        return ms.ToArray();            
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文