C# 使用流与字节[]传输文件内容的混淆

C# 使用流与字节[]传输文件内容的混淆,c#,file-io,stream,memorystream,C#,File Io,Stream,Memorystream,我想要实现的是从磁盘读取JPEG图像,调整其大小(降低分辨率),并将生成的图像返回到另一个模块,该模块将把图像保存到AWS S3存储桶中。。。我不知道是应该以byte[]还是MemoryStream的形式返回结果图像 我已经看到并编写了以下函数,该函数读取图像,调整图像大小,并以字节[]返回结果图像 public byte[] GetResizedImage(string folderPath, string fileName) { // read the original image

我想要实现的是从磁盘读取JPEG图像,调整其大小(降低分辨率),并将生成的图像返回到另一个模块,该模块将把图像保存到AWS S3存储桶中。。。我不知道是应该以
byte[]
还是
MemoryStream
的形式返回结果图像

我已经看到并编写了以下函数,该函数读取图像,调整图像大小,并以
字节[]
返回结果图像

public byte[] GetResizedImage(string folderPath, string fileName)
{
    // read the original image from disk
    FileInfo originalImage = ReadFileFromDisk(folderPath, fileName);

    // resize the image, set width to 640px and respect original aspect ratio 
    using (var image = Image.FromStream(originalImage.OpenRead()))
    {
        int newWidth = image.Width;
        int newHeight = image.Height;
        float aspectRatio = image.Width / image.Height;

        if (image.Width > 640)
        {
            newWidth = 640;
            newHeight = Convert.ToInt32(GlobalConstants.NoOfPixelsForImageResizing / aspectRatio);
        }

        // resize image           
        Image thumbnail = image.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);

        using (var thumbnailStream = new MemoryStream())
        {
            thumbnail.Save(thumbnailStream, ImageFormat.Jpeg);
            return thumbnailStream.ToArray(); // <-- return image in byte[]
        }
    }
}
这样,我将无法处理
内存流
。。。不确定这是否是一种不好的做法

这个问题的原因是,我的另一个模块将缩小后的图像保存到S3存储桶中,并将文件输入作为流接受:

SavetoS3bucket(Stream image, string name)
{
    // save image to S3 bucket
}

因此,我不清楚是否最好将
字节[]
传递给上述方法

如果返回MemoryStream,则可以在对GetResizedImage的调用周围添加using语句。那么这取决于调用代码。请看。希望它能帮助您做出最好的决定。@user1672994通常是创建流的代码应该进行处理it@user
使用(var stream=MethodThatReturnsStream())
是我可能会使用这种方法的方式。不,我是在评论一个概念,通常你应该有一个创建一次性对象的方法,使用它们并最终销毁/处置它们。在我看来,在代码的其他部分销毁一次性对象是危险的。在您的情况下,这显然是不可能的,因此您有一个返回一次性对象的方法,并且需要包含在using语句中。别忘了。
SavetoS3bucket(Stream image, string name)
{
    // save image to S3 bucket
}