0

I am try to convert inputstream to Image

I have tried using the below code.

var img = Image.FromStream(imagePathErrorMessage.StreamObject);

I need to pass the img to mvc view

 @foreach (var img in imageSet)
                                    {
                                        <div class="thumbnail thumbnail-inline">
                                            <label class="checkbox-inline">
                                                <input type="checkbox" onchange="DisplayedImageOnchange(this)" value="@img.Id" />
                                                <img src="@img.Url" class="img-thumbnail" />
                                            </label>
                                            <div class="btn-block">
                                                <input type="file" class="replace file-loading" />
                                                <button class="btn btn-xs btn-danger delete">delete</button>
                                            </div>
                                        </div>
                                    }

 public JsonResult Upload(object model)
        { 
            var img = Image.FromStream(imagePathErrorMessage.StreamObject);

            return Json(new AdCreative { Id = 100, Url = Url.Content("data:image;base64,@System.Convert.ToBase64String('" + img + "')") }, JsonRequestBehavior.AllowGet);
        }

it throws the following error :

enter image description here

the above inputstream from amazon s3 response.

enter image description here

Kapil
  • 1,823
  • 6
  • 25
  • 47

1 Answers1

0

Firstly, you may find more information by clicking on 'View Detail' in the exception window, and expanding any InnerException properties.

Have you checked that imagePathErrorMessage.StreamObject is non-null and provides an image in one of the accepted formats?

These are JPG, BMP, TIF, GIF and PNG. If your image contains transparency data, you should convert it to a PNG first. You may not use RAW data, for that I think you'd want BitmapImage instead of Image

If the stream is valid, it will also need to be at position 0 before you start the read, for example:

imagePathErrorMessage.StreamObject.Position = 0;
var img = Image.FromStream(imagePathErrorMessage.StreamObject);

Edit for OP Edit:

Do you need to set the ResponseStream back to position zero after it is converted but before it is copied to destination?

(if at the breakpoint shown in your screenshot, you hovered over destination would it show a stream with 61153 length and CurrentPosition 0?)

Second Edit:

This answered post Loading an image from a stream without keeping the stream open describes why when using Image.FromStream then the stream needs to remain open for the lifetime of the image.

Community
  • 1
  • 1
paul
  • 21,653
  • 1
  • 53
  • 54
  • yes I have set the StreamObject.Position= 0 before copying. – Kapil Jul 21 '16 at 10:36
  • Well, if you have certified at the point of `Image.FromStream(imagePathErrorMessage.StreamObject);` that your stream object is valid, with a non zero length, and at position zero, then the only other thing I can suggest is that the data in that stream is not convertable to one of the supported image types. – paul Jul 21 '16 at 10:40
  • One final suggestion, it appears that the stream object must remain open during the lifetime of the `Image` – paul Jul 21 '16 at 10:53