ASP MVC Input stream is not a supported format

1.4k Views Asked by At

I use ImageProccessor to apply some changes on uploaded image and I want to check if width of image has been uploaded less than 150px change it to 200px. For this purpose I make instance of Image class like this:

Image image = Image.FromStream(inStream); 

When I add this I get this error:

Input stream is not a supported format

Here is my code:

    public ActionResult SaveFileToTemporaryFodler()
    {

        HttpPostedFileBase file = Request.Files[0];
        string directoryPath = ImageService.GetImageTempDirectoryRelativePath();

        byte[] photoBytes = file.InputStream.StreamToByteArray();
        int quality = 70;


        using (var inStream = new MemoryStream(photoBytes))
        {
            Image image = Image.FromStream(inStream);

            using (var outStream = new MemoryStream())
            {
                Size size = new Size(500, 500);
                using (var imageFactory = new ImageFactory(preserveExifData: true))
                {    
                     if (image.Width < 150)
                    {
                        size = new Size(200, 200);
                    }
                    // I get error here :
                    imageFactory.Load(inStream)

                        .RoundedCorners(new RoundedCornerLayer(190, true, true, true, true))
                        .Watermark(new TextLayer()
                        {
                            DropShadow = true,
                            Text = "Watermark",
                            Style = FontStyle.Bold,
                        })
                        .Resize(size)     
                        .Quality(quality)
                        .Save(Server.MapPath(directoryPath + fileName));    
                }
            }
        }



        return Json(new { message = Request.Files.Count, picName = fileName, fileExtention = fileName });
    }

How can I fix this issue or is there any way to check image dimensions?

UPDATE:

I upload image with Ajax and here is StreamToByteArray function:

 public static byte[] StreamToByteArray(this Stream input)
    {
        input.Position = 0;
        using (var ms = new MemoryStream())
        {
            int length = System.Convert.ToInt32(input.Length);
            input.CopyTo(ms, length);
            return ms.ToArray();
        }
    }
0

There are 0 best solutions below