减少来自HttpPostedFileBase的图像大小(物理尺寸),然后转换为base64 [英] reduce image size (physical and dimensions) coming from HttpPostedFileBase then convert to base64

查看:73
本文介绍了减少来自HttpPostedFileBase的图像大小(物理尺寸),然后转换为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人将HttpPostedFileBase的图像文件转换为缩小的大小,然后再将图像转换为base64的好例子?我已经花了几个小时没有运气.这是我的代码的开始.其中一些是硬编码的(图像尺寸).

Does anyone have a good example of converting an image file coming from a HttpPostedFileBase to a reduced size and then converting the image to base64? I've spent hours on this with out any luck. Here is the start of my code. Some of which is hardcoded (image size).

当我将base64放在图像标签中并在浏览器中查看时,这给了我黑色图像.

This is giving me a black image when I place the base64 in an image tag and view it in a browser.

    public ActionResult Upload(HttpPostedFileBase file, decimal? id, decimal? id2)
    {                        
            Image img = Image.FromStream(file.InputStream, true, true);

            var bitmap = new Bitmap(img.Width - 100, img.Height - 100);

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = stream.ToArray();
            string base64String = Convert.ToBase64String(imageBytes);
            InsertImage(base64String);
     }

我在问如何更改图像,然后将其转换为base64.这比称为重复的问题更具体.

I'm asking how to change the image then convert it to base64. This is more specific than the question called it duplicate.

推荐答案

我自己从未使用过HttpPostedFileBase.因此,我稍微简化了这个问题,实际上这是您应该尝试解决的未来问题.您应该尝试尽可能缩小焦点.也就是说,这是一种减小流表示的图像尺寸并将新图像作为字节数组返回的方法.

I have never used the HttpPostedFileBase myself. So, I simplified the problem a bit, which is actually what you should try to do with future questions. You should try to narrow the focus as much as possible. That said, here is a method that reduces the dimensions of an image represented by a stream and returns the new image as an array of bytes.

    private static byte[] ReduceSize(FileStream stream, int maxWidth, int maxHeight)
    {
        Image source = Image.FromStream(stream);
        double widthRatio = ((double)maxWidth) / source.Width;
        double heightRatio = ((double)maxHeight) / source.Height;
        double ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;
        Image thumbnail = source.GetThumbnailImage((int)(source.Width * ratio), (int)(source.Height * ratio), AbortCallback, IntPtr.Zero);
        using (var memory = new MemoryStream())
        {
            thumbnail.Save(memory, source.RawFormat);
            return memory.ToArray();
        }
    }

您可能会这样调用此方法:

You can probably call this method like this:

public ActionResult Upload(HttpPostedFileBase file, decimal? id, decimal? id2)
{
    byte[] imageBytes = ReduceSize(file.InputStream, 100, 100);
    string base64String = Convert.ToBase64String(imageBytes);
    InsertImage(base64String);
}

我的ReduceSize()方法保持宽高比.您可能不需要,也可能想要更改参数,以便更改指定尺寸调整方式的方式.试一试,让我知道它是怎么做的.

My ReduceSize() method maintains aspect ratio. You may not need that, and you may also want to change the arguments so you can change how you specify how it is resized. Give that a shot and let me know how it does.

这篇关于减少来自HttpPostedFileBase的图像大小(物理尺寸),然后转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆