How to restrict the size of image at the time of upload
-
Hi I need to restrict the size of image at the time of uploading. So please tell me the suggestion. Thanking u, Naren
please help me
-
Hi I need to restrict the size of image at the time of uploading. So please tell me the suggestion. Thanking u, Naren
please help me
You could find you answers at Chris Khoo post http://www.codeproject.com/aspnet/netimageupload.asp Based in that post i made these two functions for asp.net C#. you must to invoice the public function. The parameters are: MaxWidth = max Width pixels MaxHeight = max height pixels UrlAndFileName = the From address image UrlAndNewFileName = the save address and new file name Example: (600, 600, "C:\Documents and Settings\Barriosj.FMMB\Mis documentos\Mis imágenes\imagenes\73237_4611.jpg", "c:/myRedimImage.jpg") The image is restricted by the smallest width or heigth value. public void Redim_And_SaveImg(int MaxWidth, int MaxHeight, string UrlAndFileName, string UrlAndnewFileName) { // load up the image, figure out a "best fit" // resize, and then save that new image Bitmap OriginalBmp = (System.Drawing.Bitmap)Image.FromFile(UrlAndFileName).Clone(); Size ResizedDimensions = ObtainDim(MaxWidth, MaxHeight, ref OriginalBmp); Bitmap NewBmp = new Bitmap(OriginalBmp, ResizedDimensions); NewBmp.Save(UrlAndnewFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } private static Size ObtainDim(int MaxWidth, int MaxHeight, ref Bitmap Bmp) { int Width; int Height; float Multiplier; Height = Bmp.Height; Width = Bmp.Width; // this means you want to shrink // an image that is already shrunken! if (Height <= MaxHeight && Width <= MaxWidth) return new Size(Width, Height); // check to see if we can shrink it width first Multiplier = (float)((float)MaxWidth / (float)Width); if ((Height * Multiplier) <= MaxHeight) { Height = (int)(Height * Multiplier); return new Size(MaxWidth, Height); } // if we can't get our max width, then use the max height Multiplier = (float)MaxHeight / (float)Height; Width = (int)(Width * Multiplier); return new Size(Width, MaxHeight); } :)
keep Learning and you never will be out of date...