روشهای زیادی برای تغییر اندازه تصویر موقع ارسال (upload) کردن وجود که این هم یک روش ساده و سریع می باشد.
همچنین در این روش می توان چندین تصویر را به صورت همزمان تغییر اندازه داد.
توضیحات در داخل کدها نوشته شده است
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; ////// Summary description for clsImageUpload : Credits : http://jaberi.eu /// public class clsImageUpload { string fileName; public clsImageUpload() { // // TODO: Add constructor logic here // } //This function is called from aspnet page, it takes directory name as parameter public string HandleUploadedFile(string directory) { // To get the root of the web site string root = HttpContext.Current.Server.MapPath("~/"); // clean up the path if (!root.EndsWith(@"\")) root += @"\"; // make a folder to store the images in string fileDirectory = root + @"\" + directory + "\\"; // create the folder if it does not exist // make a link to the new file // loop through the file in the request for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { // get the file instance HttpPostedFile fi = HttpContext.Current.Request.Files.Get(i); // create a byte array to store the file bytes byte[] fileBytes = new byte[fi.ContentLength]; // fill the byte array using (System.IO.Stream stream = fi.InputStream) { stream.Read(fileBytes, 0, fi.ContentLength); } // create a random file name fileName = Guid.NewGuid().ToString(); // write the resized file to the file system File.WriteAllBytes(fileDirectory + fileName + "_thumb.jpg", ResizeImageFile(fileBytes, 75)); fileBytes = null; } return (fileName + "_thumb.jpg"); } public void HandleUploadedFileUseExistingName(string directory, string fname) { // get the root of the web site string root = HttpContext.Current.Server.MapPath("~/"); // clean up the path if (!root.EndsWith(@"\")) root += @"\"; // make a folder to store the images in string fileDirectory = root + @"\" + directory + "\\"; // loop through the file in the request for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { // get the file instance HttpPostedFile fi = HttpContext.Current.Request.Files.Get(i); // create a byte array to store the file bytes byte[] fileBytes = new byte[fi.ContentLength]; // fill the byte array using (System.IO.Stream stream = fi.InputStream) { stream.Read(fileBytes, 0, fi.ContentLength); } // create a random file name fileName = fname; // write the resized file to the file system File.WriteAllBytes(fileDirectory + fileName, ResizeImageFile(fileBytes, 75)); fileBytes = null; } } /// This fuction returns a Byte array containing the resized file private static byte[] ResizeImageFile(byte[] imageFile, int targetSize) { using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile))) { //If you want to maintain the propotion use following code //Size newSize = CalculateDimensions(oldImage.Size, targetSize); //If you want to use a fixed size use following one Size newSize = GetDimension(); using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.SmoothingMode = SmoothingMode.AntiAlias; canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize)); MemoryStream m = new MemoryStream(); newImage.Save(m, ImageFormat.Jpeg); return m.GetBuffer(); } } } } /// This function Calculates the new size of the image based on the target size private static Size CalculateDimensions(Size oldSize, int targetSize) { Size newSize = new Size(); if (oldSize.Height > oldSize.Width) { newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height)); newSize.Height = targetSize; } else { newSize.Width = targetSize; newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width)); } return newSize; } //Dimension of the images can be set here private static Size GetDimension() { Size newSize = new Size(); newSize.Width = 100; newSize.Height = 100; return newSize; } }
Thank God! Someone with brains speaks!
God, I feel like I suhold be takin notes! Great work
Begun, the great inretent education has.
I didn’t know where to find this info then kobaom it was here.
Didn’t know the forum rules alolewd such brilliant posts.
YMMD with that asnewr! TX
Kick the tires and light the fires, problem officially sloevd!
There is a crtiical shortage of informative articles like this.
We need a lot more insitghs like this!
A piece of erudition uiknle any other!
Always refreshing to hear a raitoanl answer.
Good entry thanks, like your blog design too. Is it wordpress?
hi, yes, i used wordpress in this personal site.
That insight’s perecft for what I need. Thanks!
Could you write about Phycsis so I can pass Science class?
This artlcie achieved exactly what I wanted it to achieve.
Dead indited written content , Really enjoyed reading .
Okay I’m convinced. Let’s put it to atcoin.
thank you
This forum needed sahikng up and you’ve just done that. Great post!
بازتاب: My Homepage
hello admin, your internet site page’s pattern is simple and clean and i like it. Your articles are remarkable. Remember to maintain up the very good function. Greets.. 565035
Articles like this rellay grease the shafts of knowledge.
Perecft answer! That really gets to the heart of it!
KXX3mR rmtjdussqebx
Your pstoing is absolutely on the point!
I just hope whoever witres these keeps writing more!
I simply want to mention I am just very new to weblog and absolutely enjoyed you’re web site. More than likely I’m likely to bookmark your blog . You actually have outstanding well written articles. With thanks for sharing your website.
You have noted very interesting details! ps nice internet site.
Play informative for me, Mr. inrtenet writer.
I’m so glad that the internet allwos free info like this!
Aritcels like these put the consumer in the driver seat-very important.
Wow! That’s a really neat awnesr!
You put the lime in the coconut and drink the airtcle up.
I adore looking at and I believe this website got some genuinely utilitarian stuff on it! .
Boy that ralely helps me the heck out.
Always a good job right here. Keep rollnig on through.
Fell out of bed fenelig down. This has brightened my day!
Hello there, I found your web site by way of Google even as searching for a comparable matter, your website came up, it appears to be like great. I have bookmarked it in my google bookmarks.
Thikning like that shows an expert’s touch
It’s about time soeomne wrote about this.