How do you convert a HttpPostedFileBase to an Image? -


I am using ASP.NET MVC and I have any action to upload the file. I will upload the file properly is being done. But I want the width and height of the image. I think I need to convert from HttpPostedFileBase to image and then proceed how do I do this?

And please tell me if there is a better way of achieving the width and height of the image.

Post text "itemprop =" text ">

I use something like this:

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

Note that return image is IDisposable .

to you System.Drawing.dll Reference will be required to do this and the image is in the System.Drawing namespace.

Resizing the image

< P> I'm not sure what you are trying to do, but if you make thumbnails or something similar, then you do something Can be interested ...

  try {var bitmap = new bitmap (newWidth), newHeight;) (Graphics G = Graphics .framesize (bitmap)) {g.SmoothingMode = SmoothingMode.HighQuality; G.PixelOffsetMode = PixelOffsetMode.HighQuality; G.CompositingQuality = CompositingQuality.HighQuality; G. Interpolation Mode = InterpolationMode.Hyquive Bibbic; G.DrawImage (Old Image, New Rectangle (0,0, NewWind, NewHead), Cliprextengle, graphicsout Pixel); } // "G" Return is done with drawing on bitmap, // Transport IDSpace qualified ownership} Hold {ID} IDIPCB if before the ownership transfer (bitmap! = Null) bitmap Dishes (); throw; }  

Where clipRectangle is the rectangle of the original image that you want to scale in the new bitmap (you will need to manually deal with aspect ratio). Catch-block specific IDisposable is used within a manufacturer; You will not retain ownership of the new IDisposable object until it is returned (you can make a doctor with code-comments).

Save as JPEG

Unfortunately, the default "save as a JPG" encoder does not expose any quality control, and selects very little default quality .

You can also choose the encoder manually, however, and then you arbitrary parameter:

  ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders (). Where (codecInfo = & gt; codecInfo.MimeType == "image / JPEG"). first (); (Encoder parameter encapsum = new encoder parameter (1)) {encaparm.param [0] = new encoder parameter (encoder quality, (long) quality); // should be in the quality range [0..100] image. Save (Outputstream, JPGInfo, Encaparm); }  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -