Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从S3读取图像时,参数无效_C#_.net_Amazon Web Services_Amazon S3_Memorystream - Fatal编程技术网

C# 从S3读取图像时,参数无效

C# 从S3读取图像时,参数无效,c#,.net,amazon-web-services,amazon-s3,memorystream,C#,.net,Amazon Web Services,Amazon S3,Memorystream,尝试将流转换为图像时,我得到的参数无效错误 在我的C#ASP.NET应用程序中,我将一个图像上传到AmazonS3并再次下载以进行操作 通过在S3上在线查看,我已经检查了上传的图像是否正常 我正在使用以下代码下载图像: using (AmazonS3 client = new AmazonS3Client(accessKey, secretKey)) { GetObjectRequest request = new GetObjectRequest { BucketNa

尝试将流转换为图像时,我得到的
参数无效
错误

在我的C#ASP.NET应用程序中,我将一个图像上传到AmazonS3并再次下载以进行操作

通过在S3上在线查看,我已经检查了上传的图像是否正常

我正在使用以下代码下载图像:

 using (AmazonS3 client = new AmazonS3Client(accessKey, secretKey))
{
   GetObjectRequest request = new GetObjectRequest
   {
      BucketName = "mybucket",
      Key = "temp/" + sp.FileGuid + Path.GetExtension(sp.FileName)
   };

    using (GetObjectResponse response = client.GetObject(request)) // S3Response
   {

     using (MemoryStream memStream = new MemoryStream())
      {
            int file_size_in_bytes = Convert.ToInt32(response.Headers.GetValues("Content-Length")[0]);
            byte[] buffer = new Byte[file_size_in_bytes];
            int numBytesToRead = file_size_in_bytes;

           int read;
           while ((read = response.ResponseStream.Read(buffer, 0, buffer.Length)) > 0)
           {
             memStream.Write(buffer, 0, read);
            }

   response.ResponseStream.Close();

    ...
我将图像的响应流转换为字节数组,以便将其传递给WCF服务,以便稍后进行操作

在WCF中,我执行(部分代码):

sp.ImageFile
保存将响应流转换为字节数组

我将字节数组读入一个流中,并再次从该流中创建图像。在上面的代码中,您可以看到我从何处获得
参数无效错误

我假设在某些地方,将图像的流数据转换为字节数组或流会聚集图像数据,因此会创建损坏的图像数据,但我不确定

花一整天的时间试图解决这个问题,但没有成功。它工作的唯一方式是直接将上传的文件流传递到System.Drawing.FromStream,但这不是我想要做的


真的需要你的帮助。

这可能会有所帮助。如果流未定位在起始位置,则可能无法加载图像

  using (Stream filestream = new MemoryStream(sp.ImageFile)
  {
     var checkSeek = filestream.Seek(0, SeekOrigin.Begin);

     **// GET THE ERROR IN THIS LINE!**
     System.Drawing.Image image = System.Drawing.Image.FromStream(filestream);
  }   

嗨,是吗,同样的例外
  using (Stream filestream = new MemoryStream(sp.ImageFile)
  {
     var checkSeek = filestream.Seek(0, SeekOrigin.Begin);

     **// GET THE ERROR IN THIS LINE!**
     System.Drawing.Image image = System.Drawing.Image.FromStream(filestream);
  }