gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:47:53 27 4
gpt4 key购买 nike

我正在尝试将图像上传到 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com