0

我想编辑 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”通过 0 个八位字节图像擦除 FTP 服务器上的图像。FTP 服务器上的图像是 .jpg。我已经将“client.UploadFile”与 FileStream 一起使用,它工作正常。但是在这种情况下,我不想将文件保存在我的 IIS 服务器上,对其进行修改然后将其上传到 FTP 服务器......知道吗?

4

2 回答 2

1
        img.Save(stream, ImageFormat.Jpeg);

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

        client.UploadFile(stream, fileName);
于 2016-06-08T13:43:47.260 回答
0

正如我上面所说,感谢 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);
            }
        }
    }
}
于 2016-06-08T14:01:41.817 回答