从流调整C#图像大小

时间:2019-09-08 01:43:08

标签: c# asp.net asp.net-core

我正在尝试调整流中图像的大小。得到错误

  

值不能为空。参数名称:编码器

在线

  

System.Drawing.Bitmap fullSizeBitmap =新   System.Drawing.Bitmap(fullsizeImage,新的System.Drawing.Size(width,   image_height));

如何在此处添加编码器?我需要它来自原始图像

   public static FileProperty UploadImage(IFormFile file, string folderPath, string fileName, FileNote note, int image_height)
    {

        FileProperty property = new FileProperty();

        if (file.Length > 0)
        {

            MemoryStream ms = new MemoryStream();
            file.CopyTo(ms);
            var fileBytes = ms.ToArray();

            MemoryStream inputMemoryStream = new MemoryStream(fileBytes);
            System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(inputMemoryStream);

            int width = (image_height / fullsizeImage.Height) * fullsizeImage.Width;

            System.Drawing.Bitmap fullSizeBitmap = new System.Drawing.Bitmap(fullsizeImage, new System.Drawing.Size(width, image_height));

            using (var stream = new MemoryStream())
            {
                fullSizeBitmap.Save(stream, fullSizeBitmap.RawFormat);

                using (MemoryStream memoryStream = new MemoryStream(stream.ToArray()))
                {
                    UploadFromStreamAsync(memoryStream);
                }
            }

            property.FileName = fileName;
            property.FileExtention = Path.GetExtension(fileName);
            property.FileSize = file.Length;
            property.FileType = file.ContentType;
            property.FileNote = note.ToString();
        }

        return property;
    }

1 个答案:

答案 0 :(得分:0)

只需使用file.OpenReadStream()来读取文件并调整其大小,然后将调整后的位图保存到MemoryStream并上传。

除了使用bitmap.RawFormat之外,您还可以使用以下方法获取上传的图像格式:

if (file.Length > 0)
{
    using(var stream = file.OpenReadStream())
    {
        var image = Image.FromStream(stream);

        int width = (image_height / fullsizeImage.Height) * fullsizeImage.Width;        

        Bitmap fullSizeBitmap = new Bitmap(fullsizeImage, new Size(width, image_height));

        var imgFormat = GetImageFormat(file.FileName);

        using(var ms = new MemoryStream())
        {
            fullSizeBitmap.Save(ms, imgFormat);
            UploadFromStreamAsync(ms);
        }
    }
}

通过文件扩展名获取图像格式的方法:

public ImageFormat GetImageFormat(string fileName)
{
    var dotIndex = fileName.LastIndexOf('.');
    var ext = fileName.Substring(dotIndex, fileName.Length - dotIndex).ToLower();

    switch (ext)
    {
        case ".bmp": return ImageFormat.Bmp;
        case ".emf": return ImageFormat.Emf;
        case ".exif": return ImageFormat.Exif;
        case ".gif": return ImageFormat.Gif;
        case ".icon": return ImageFormat.Icon;
        case ".Jpg": return ImageFormat.Jpeg;
        case ".Jpeg": return ImageFormat.Jpeg;
        case ".png": return ImageFormat.Png;
        case ".tiff": return ImageFormat.Tiff;
        case ".Wmf": return ImageFormat.Wmf;
        default: throw new InvalidDataException("Format not supported");
    }
}

如果您仍然需要获取上传的图像编码器信息,请使用以下方法:

// ext: image extension
using System.Drawing.Imaging;

public static ImageCodecInfo GetEncoderInfo(string ext)
{
    return ImageCodecInfo.GetImageEncoders().SingleOrDefault(x => x.FilenameExtension.ToLower().Contains(ext));
}