c# - 在以下代码中获取 'Stream does not support writing.' 异常

标签 c# stream .net

我正在尝试将图像上传到 Amazon S3,但在此之前我正在调整图像的大小。为了调整大小,我必须传递流对象,并且在某一时刻(注释为//Error 的行)我得到“Stream does not support writing”。异常(exception)。请帮忙。

 public ActionResult AddPost(AddPost post)
    {
        Guid guid = new Guid();
        AccountController ac=new AccountController();

        string randomId = guid.ToString();

        PutAttributesRequest putAttributesAction = new PutAttributesRequest().WithDomainName("ThisIsMyEXDomainPosts").WithItemName(randomId);

        List<ReplaceableAttribute> attrib = putAttributesAction.Attribute;

        System.IO.Stream stream;

        System.IO.StreamReader sr = new System.IO.StreamReader(post.imageFileAddress.ToString());


        sr.ReadToEnd();

        stream = sr.BaseStream;

        Amazon.S3.Model.PutObjectRequest putObjectRequest = new Amazon.S3.Model.PutObjectRequest();

        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);

        System.Drawing.Image imgResized = ResizeImage(img, 640, 800);

        System.IO.MemoryStream mstream = new System.IO.MemoryStream();

        imgResized.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);

        mstream.WriteTo(stream);//Error

        putObjectRequest.WithBucketName("TIMEXImages");
        putObjectRequest.CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead;
        putObjectRequest.Key = randomId + "_0.jpg";
        putObjectRequest.InputStream = stream;

        Amazon.S3.Model.S3Response s3Response = as3c.PutObject(putObjectRequest);
        s3Response.Dispose();

        //Uploadig the Thumb

        System.Drawing.Image imgThumb = ResizeImage(img, 80, 100);

        imgThumb.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);

        mstream.WriteTo(stream);

        putObjectRequest.WithBucketName("MyProjectImages");
        putObjectRequest.CannedACL = Amazon.S3.Model.S3CannedACL.PublicRead;
        putObjectRequest.Key = randomId + ".jpg";
        putObjectRequest.InputStream = stream;

        Amazon.S3.Model.S3Response s3Response2 = as3c.PutObject(putObjectRequest);
        s3Response2.Dispose();


        //Closing all opened streams
        sr.Close();

        stream.Close();

        mstream.Close();



        //Adding to SimpleDB
        attrib.Add(new ReplaceableAttribute().WithName("category").WithValue(post.category));
        attrib.Add(new ReplaceableAttribute().WithName("description").WithValue(post.description));
        attrib.Add(new ReplaceableAttribute().WithName("favoriteCount").WithValue("0"));
        attrib.Add(new ReplaceableAttribute().WithName("imageThug").WithValue(randomId));
        attrib.Add(new ReplaceableAttribute().WithName("title").WithValue(post.title));
        attrib.Add(new ReplaceableAttribute().WithName("userId").WithValue(ac.GetLoggedInUserId()));

        sdb.PutAttributes(putAttributesAction);




        return View();
    }

最佳答案

似乎StreamReaderBaseStream只读 - 这是有道理的。为什么你首先需要重新使用这个流?直接使用内存流即可:

mstream.Position = 0;
putObjectRequest.InputStream = mstream;

关于c# - 在以下代码中获取 'Stream does not support writing.' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7238018/

相关文章:

c# - 用于 UWP 应用程序的 Azure 服务总线

c# - 在 C# 中将值从程序集传递到应用程序

scala - 与 Scala 中的简单正则表达式匹配的字符串流

ffmpeg - 如何在不消耗所有 CPU 的情况下从多台摄像机录制

c# - WCF 动态端点和配置依赖?

c# - 非阻塞任务。在 .NET 4.5 中等待

c# - 如何使用 Stream Writer 写入文件开头?

asp.net - ASP.NET 中的 UWP API

c# - 为什么嵌套循环中的方法仅在第一个循环的迭代中被调用!

c# - 查明一个类型是否实现了一个泛型接口(interface)