簡體   English   中英

如何使用C#正確創建縮略圖?

[英]How to correctly create a thumbnail using C#?

我在Core .NET 2.2框架的頂部有一個使用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

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流正確創建縮略圖?

我找出問題的原因。 由於某種原因,行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會有什么好處! 我只是覺得應該這樣做,因為它將始終編寫一個新的流,而不是將其追加到現有的流之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM