How do you convert HttpPostedFileBase to image?

I am using ASP.NET MVC and I have an action that loads a file. File uploads properly. But I want the width and height of the image. I think I need to convert the HttpPostedFileBase to Image , and then continue. How to do it?

And please let me know if there is another best way to get the width and height of the image.

+42
Jul 23 '09 at 13:24
source share
2 answers

I am using Image.FromStream as follows:

 Image.FromStream(httpPostedFileBase.InputStream, true, true) 

Note that the returned Image is IDisposable .

To do this, you will need a link to System.Drawing.dll , and Image in the System.Drawing namespace.

Resize Image

I'm not sure what you are trying to do, but if you are doing sketches or something like that, you might be interested in doing something like ...

 try { var bitmap = new Bitmap(newWidth,newHeight); using (Graphics g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(oldImage, new Rectangle(0,0,newWidth,newHeight), clipRectangle, GraphicsUnit.Pixel); }//done with drawing on "g" return bitmap;//transfer IDisposable ownership } catch { //error before IDisposable ownership transfer if (bitmap != null) bitmap.Dispose(); throw; } 

where clipRectangle is the rectangle of the original image that you want to scale in the new bitmap (you will need to manually process the aspect ratio). The catch block is the typical use of IDisposable inside the constructor; you retain ownership of the new IDisposable until it is returned (you may want a doc with code comments).

Saving as Jpeg

Unfortunately, the "save as jpeg" encoder does not provide any quality controls by default and selects a terribly low quality by default.

You can also manually select the encoder, and then you can pass arbitrary parameters:

 ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders() .Where(codecInfo => codecInfo.MimeType == "image/jpeg").First(); using (EncoderParameters encParams = new EncoderParameters(1)) { encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality); //quality should be in the range [0..100] image.Save(outputStream, jpgInfo, encParams); } 
+88
Jul 23 '09 at 13:28
source

If you are sure that the image source does not need editing, you can do it easily, as described here

 [HttpPost] public void Index(HttpPostedFileBase file) { if (file.ContentLength > 0) { var filename = Path.GetFileName(file.FileName); System.Drawing.Image sourceimage = System.Drawing.Image.FromStream(file.InputStream); } } 

To protect the file - this image, add JavaScript validation to view by adding the accept attribute with the MIME type to enter the tag

 <input type="file" accept="image/*"> 

and jQuery script validation

 $.validator.addMethod('accept', function () { return true; }); 

All solution can be found here.

+5
Nov 20 '13 at 11:58
source



All Articles