public static bool IsImage(HttpPostedFile postedFile) { //------------------------------------------- // Check the image mime types //------------------------------------------- if (postedFile.ContentType.ToLower() != "image/jpg" && postedFile.ContentType.ToLower() != "image/jpeg" && postedFile.ContentType.ToLower() != "image/pjpeg" && postedFile.ContentType.ToLower() != "image/gif" && postedFile.ContentType.ToLower() != "image/x-png" && postedFile.ContentType.ToLower() != "image/png") { return false; } //------------------------------------------- // Check the image extension //------------------------------------------- if (Path.GetExtension(postedFile.FileName).ToLower() != ".jpg" && Path.GetExtension(postedFile.FileName).ToLower() != ".png" && Path.GetExtension(postedFile.FileName).ToLower() != ".gif" && Path.GetExtension(postedFile.FileName).ToLower() != ".jpeg") { return false; } return true; } if(IsImage(imgUpload.PostedFile)) { string imagePath = imgUpload.PostedFile.FileName; byte[] buffer = getResizedImage(imagePath, 99, 99); } protected byte[] getResizedImage(String path, int width, int height) { System.IO.MemoryStream outStream = new System.IO.MemoryStream(); try { Bitmap imgIn = new Bitmap(path); double y = imgIn.Height; double x = imgIn.Width; double factor = 1; if (width > 0) { factor = width / x; } else if (height > 0) { factor = height / y; } Bitmap imgOut = new Bitmap((int)(x * factor), (int)(y * factor)); // Set DPI of image (xDpi, yDpi) imgOut.SetResolution(72, 72); Graphics g = Graphics.FromImage(imgOut); g.Clear(Color.White); g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)), new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel); imgOut.Save(outStream, getImageFormat(path)); } catch (Exception ex) { Common.ExceptionLogging("MNWeb", "getResizedImage", ex.ToString()); } return outStream.ToArray(); } protected string getContentType(String path) { switch (Path.GetExtension(path)) { case ".bmp": return "Image/bmp"; case ".gif": return "Image/gif"; case ".jpg": return "Image/jpeg"; case ".png": return "Image/png"; default: break; } return ""; } protected ImageFormat getImageFormat(String path) { switch (Path.GetExtension(path)) { case ".bmp": return ImageFormat.Bmp; case ".gif": return ImageFormat.Gif; case ".jpg": return ImageFormat.Jpeg; case ".png": return ImageFormat.Png; default: break; } return ImageFormat.Jpeg; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)