简体   繁体   English

从 stream 在 c# 中创建缩略图

[英]Create thumbnail in c# from stream

After 3 hours of debugging and searching I have this.经过 3 个小时的调试和搜索,我有了这个。

        public Stream ResizeImageToThumbnail(Stream imageStream, int width)
        {
            var image = Image.FromStream(imageStream);

            var height = (width * image.Height) / image.Width;
            var thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero);

            using var thumbnailStream = new MemoryStream();
            thumbnail.Save(thumbnailStream, ImageFormat.Jpeg);
            return thumbnailStream;
        }

Problem is, it returns exception问题是,它返回异常

ArgumentException: Parameter is not valid. ArgumentException:参数无效。

at

var image = Image.FromStream(imageStream);

Parameter is file.OpenFileStream();参数为file.OpenFileStream(); where file is IFormFile .其中fileIFormFile

I'm out of ideas.我没主意了。

Edit编辑

Requested code:要求的代码:


foreach (var item in model.UploadedImages) //item = IFormFile, image only allowed from HTML's end
{
   using var ms = item.OpenReadStream();
   _service.AttachImage(newId, ms, item.FileName);
   ms.Position = 0;
   _service.AttachThumb(newId, ms, item.FileName); //cannot access a closed stream exception
}

Edit 2编辑 2

AttachThumb is using closed stream, returns exception (check comment). AttachThumb 使用封闭的 stream,返回异常(检查注释)。 It appears that ResizeImageToThumbnail returns closed stream. ResizeImageToThumbnail似乎返回关闭的 stream。

public void AttachThumb(Guid id, Stream imageStream, string imageName)
        {
            Post post = GetPost(id);

            ObjectId imageId = _gridFS.UploadFromStream(imageName, ResizeImageToThumbnail(imageStream, 640)); //cannot use closed stream

            post.ImagesThumbs.Add(imageId.ToString());

            var filter = Builders<Post>.Filter.Eq(x => x.Id, id);
            var update = Builders<Post>.Update.Set("ImagesThumbs", post.ImagesThumbs);
            _posts.UpdateOne(filter, update);
        }

        public Stream ResizeImageToThumbnail(Stream imageStream, int width)
        {
            var image = Image.FromStream(imageStream);

            var height = (width * image.Height) / image.Width;
            var thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero);

            using var thumbnailStream = new MemoryStream();
            thumbnail.Save(thumbnailStream, ImageFormat.Jpeg);
            return thumbnailStream;
        }

First copy the content of the file into a stream, then use that stream.首先将文件内容复制到 stream 中,然后使用该 stream。 Rewind the stream in between.在两者之间倒带 stream。 Note that the two Attach* methods must not access the stream asynchronously.请注意,两个 Attach* 方法不得异步访问 stream。 Otherwise you would have to create a second in-memory copy first.否则,您必须先创建第二个内存副本。

foreach (var item in model.UploadedImages)
{
    // Copy content in to stream
    using (MemoryStream stream = new MemoryStream())
    {
        item.CopyTo(stream);

        // Rewind and use
        stream.Position = 0;
        _service.AttachImage(newId, stream, item.FileName);
        
        // Rewind and use
        stream.Position = 0;
        _service.AttachThumb(newId, stream, item.FileName);
    }
}

As noted by user Jimi, you have to remove the using clause from the ResizeImageToThumbnail method.正如用户 Jimi 所指出的,您必须从ResizeImageToThumbnail方法中删除using子句。 Otherwise the method returns an already disposed MemoryStream :否则,该方法返回一个已经释放的MemoryStream

public Stream ResizeImageToThumbnail(Stream imageStream, int width)
{
    var image = Image.FromStream(imageStream);

    var height = (width * image.Height) / image.Width;
    var thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero);

    // Bad: using var thumbnailStream = new MemoryStream();
    // Remove using
    var thumbnailStream = new MemoryStream(); 
    thumbnail.Save(thumbnailStream, ImageFormat.Jpeg);
    return thumbnailStream;
}

Then dispose the stream outside:然后将 stream 放在外面:

public void AttachThumb(Guid id, Stream imageStream, string imageName)
{
    Post post = GetPost(id);

    using var thumbnailStream = ResizeImageToThumbnail(imageStream, 640);
    // rewind stream
    thumbnailStream.position = 0;
    ObjectId imageId = _gridFS.UploadFromStream(imageName, thumbnailStream); 

    ...

You could improve the design of this by not having the ResizeImageToThumbnail method create the stream but instead create the stream outside and pass it to ResizeImageToThumbnail as a parameter.您可以通过不让ResizeImageToThumbnail方法创建 stream 而是在外部创建 stream 并将其作为参数传递给ResizeImageToThumbnail来改进此设计。 So the caller of ResizeImageToThumbnail is then responsible for creating and disposing the stream.所以ResizeImageToThumbnail的调用者然后负责创建和处置 stream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM