تغییر اندازه تصویر موقع ارسال به سرور (Upload)

روشهای زیادی برای تغییر اندازه تصویر موقع ارسال (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;
}
}

Vote This Post DownVote This Post Up (+3 votes)
Loading...



این نوشته در ASP.NET ارسال شده است. افزودن پیوند یکتا به علاقه‌مندی‌ها.

41 Responses to تغییر اندازه تصویر موقع ارسال به سرور (Upload)

  1. Takeo می‌گوید:

    Thank God! Someone with brains speaks!

  2. Claudie Tada می‌گوید:

    Good entry thanks, like your blog design too. Is it wordpress?

  3. Andre Fratello می‌گوید:

    Dead indited written content , Really enjoyed reading .

  4. بازتاب: My Homepage

  5. 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

  6. Joaquin Proffit می‌گوید:

    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.

  7. Mona Bivens می‌گوید:

    You have noted very interesting details! ps nice internet site.

  8. pain in lower right abdomen می‌گوید:

    I adore looking at and I believe this website got some genuinely utilitarian stuff on it! .

  9. Amina Perney می‌گوید:

    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.

  10. Sakhiwo می‌گوید:

    It’s about time soeomne wrote about this.

پاسخ دهید