通过 MemoryStream 编辑图像(来自 FTP)

Edit Image via MemoryStream (from FTP)

我想编辑 FTP 服务器上的图像。 我使用 SSH.net,这是我的代码:

using (var client = new SftpClient(ftpUploadPath, ftpPort, ftpUser, ftpPassword))
{
    client.Connect();
    using (var stream = new MemoryStream())
    {
        client.DownloadFile(fileName, stream);
        using (var img = Image.FromStream(stream, true))
        {
            img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            img.Save(stream, ImageFormat.Jpeg);
            client.UploadFile(stream, fileName);
        }
    }
}

一切都是正确的,直到 "client.UploadFile" 将 FTP 服务器上的图像擦除为 0 个八位字节图像。 FTP 服务器上的图像是 .jpg。 我已经将 "client.UploadFile" 与 FileStream 一起使用并且工作正常。但在这种情况下,我不想将文件保存在我的 IIS 服务器上,修改它然后将其上传到 FTP 服务器... 有什么想法吗?

        img.Save(stream, ImageFormat.Jpeg);

        stream.Position = 0; // Reset the stream to the beginning before switching to reading it

        client.UploadFile(stream, fileName);

正如我上面所说,感谢 noelicus 和 Thorsten,这是解决方案:

using (var client = new SftpClient(ftpUploadPath, ftpPort, ftpUser, ftpPassword))
{
    client.Connect();
    using (var stream = new MemoryStream())
    {
        client.DownloadFile(fileName, stream);
        using (var img = Image.FromStream(stream, true))
        {
            img.RotateFlip(RotateFlipType.Rotate90FlipNone);

            using (var newStream = new MemoryStream())
            {
                img.Save(newStream, ImageFormat.Jpeg);
                newStream.Position = 0;
                client.UploadFile(newStream, item);
            }
        }
    }
}