Sat 19 Jul 2008
Image Resize With CSharp
Posted by admin under Graphics |
Often it is necessary to resize an image that has been uploaded. To do this you will need the following using classes:-
using System.Drawing; using System.Drawing.Imaging;
Then here are some examples on functions with the resize code:-
public static string ResizeImageAndSave(int Width, int Height, string imageUrl,string destPath) { System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(imageUrl)); double widthRatio = (double)fullSizeImg.Width / (double)Width; double heightRatio = (double)fullSizeImg.Height / (double)Height; double ratio = Math.Max(widthRatio, heightRatio); int newWidth = (int)(fullSizeImg.Width / ratio); int newHeight = (int)(fullSizeImg.Height / ratio); System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight,dummyCallBack, IntPtr.Zero); DateTime MyDate = DateTime.Now; String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png"; thumbNailImg.Save(HttpContext.Current.Server.MapPath(destPath) + MyString, ImageFormat.Png); thumbNailImg.Dispose(); return MyString; } public static bool ThumbnailCallback() { return false; }
No Responses to “ Image Resize With CSharp ”
Comments:
Leave a Reply
You must be logged in to post a comment.