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);
Eamon Nerbonne Jul 23 '09 at 13:28 2009-07-23 13:28
source share