如何使用C#正确创建缩略图?

时间:2019-02-06 16:21:56

标签: c# image image-processing stream memorystream

我在Core .NET 2.2框架的顶部有一个使用NavDrawerActivity编写的控制台应用程序。

我想创建异步任务,该任务会将完整大小的图像写入存储。此外,该过程将需要创建缩略图并将其写入默认存储。

跟随是处理逻辑的方法。我记录了每一行以解释我相信正在发生

C#

如果需要,这里是我的// This method accepts FileObject and returns a task // The generated task will write the file as is to the default storage // Then it'll create a thumbnail of that images and store it to the default storage public async Task ProcessImage(FileObject file, int thumbnailWidth = 250) { // The name of the full-size image string filename = string.Format("{0}{1}", file.ObjectId, file.Extension); // The name along with the exact path to the full-size image string path = Path.Combine(file.ContentId, filename); // Write the full-size image to the storage await Storage.CreateAsync(file.Content, path) .ContinueWith(task => { // Reset the stream to the beginning since this will be the second time the stream is read file.Content.Seek(0, SeekOrigin.Begin); // Create original Image Image image = Image.FromStream(file.Content); // Calulate the height of the new thumbnail int height = (thumbnailWidth * image.Height) / image.Width; // Create the new thumbnail Image thumb = image.GetThumbnailImage(thumbnailWidth, height, null, IntPtr.Zero); using (MemoryStream thumbnailStream = new MemoryStream()) { // Save the thumbnail to the memory stream thumb.Save(thumbnailStream, image.RawFormat); // The name of the new thumbnail string thumbnailFilename = string.Format("thumbnail_{0}", filename); // The name along with the exact path to the thumbnail string thumbnailPath = Path.Combine(file.ContentId, thumbnailFilename); // Write the thumbnail to storage Storage.CreateAsync(thumbnailStream, thumbnailPath); } // Dispose the file object to ensure the Stream is disposed file.Dispose(); image.Dispose(); thumb.Dispose(); }); }

FileObject

上面的代码将正确的全尺寸图像写入存储驱动器。它还会将缩略图写入存储。但是,缩略图始终会损坏。换句话说,生成的缩略图文件始终以0字节写入。

将同一流写入存储后,如何从public class FileObject : IDisposable { public string ContentId { get; set; } public string ObjectId { get; set; } public ContentType ContentType { get; set; } public string Extension { get; set; } public Stream Content { get; set; } private bool IsDisposed; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (IsDisposed) return; if (disposing && Content != null) { Content.Close(); Content.Dispose(); } IsDisposed = true; } } 流正确创建缩略图?

1 个答案:

答案 0 :(得分:0)

我找出了问题的原因。出于某些原因,行thumb.Save(thumbnailStream, image.RawFormat);thumbnailStream放置在末尾,并且在写入存储时什么也没写

解决该问题的原因是,在像这样写入流后重置搜索位置

    using (MemoryStream thumbnailStream = new MemoryStream())
    {
        // Save the thumbnail to the memory stream
        thumb.Save(thumbnailStream, image.RawFormat);

        // Reset the seek position to the begining
        thumbnailStream.Seek(0, SeekOrigin.Begin);

        // The name of the new thumbnail
        string thumbnailFilename = string.Format("thumbnail_{0}", filename);

        // The name along with the exact path to the thumbnail
        string thumbnailPath = Path.Combine(file.ContentId, thumbnailFilename);

        // Write the thumbnail to storage
        Storage.CreateAsync(thumbnailStream, thumbnailPath);
    }

我不确定thumb.Save(...)在复制到新流中后不将位置重置为0会得到什么好处!我只是觉得应该这样做,因为它总是会写一个新的流而不附加到现有的流上。