如何正确地创建一个使用C#的缩略图?

问题描述 投票:2回答:1

我使用C#核心.NET 2.2框架的顶部写一个控制台应用程序。

我想创建异步任务,将一个全尺寸的图像写入存储。此外,这一过程将需要创建一个缩略图,并将其写入到默认存储。

关注是处理逻辑的方法。我记录的每一行解释,我认为正在发生的事情

// 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如果需要的话

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;
    }
}

上面的代码正确的全尺寸的图像写入到存储驱动器。它也写缩略图存储。然而,缩略图总是损坏。换句话说,所生成的缩略图文件总是被写入0字节。

我怎样才能正确地写相同的流存储后创建file.Content流我的缩略图?

c# image image-processing stream memorystream
1个回答
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复制到一个新的流之后所得到的利益!我只是觉得应该这样做,因为它总是会写一个新的流不追加到一个现有的。

© www.soinside.com 2019 - 2024. All rights reserved.