Reputation: 4119
I have a page which allows the user to upload pictures. The problem is that my old users can't resize the image themselves. I want to allow them to upload any size of image and then when the server gets it, it will create a small copy of this picture.
Upvotes: 4
Views: 25360
Reputation: 7701
This is quite a recent solution. Hope it helps
http://forums.asp.net/t/1657138.aspx/1?C+Resize+Profile+Image
protected void UploadButton_Click(object sender, EventArgs e)
{
if (File1.HasFile)
{
string fileName = Server.HtmlEncode(File1.FileName);
string extension = System.IO.Path.GetExtension(fileName);
System.Drawing.Image image_file = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
int image_height = image_file.Height;
int image_width = image_file.Width;
int max_height = 100;
int max_width = 100;
image_height = (image_height * max_width) / image_width;
image_width = max_width;
if (image_height > max_height)
{
image_width = (image_width * max_height) / image_height;
image_height = max_height;
}
Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
byte[] data = new byte[stream.Length + 1];
stream.Read(data, 0, data.Length);
Profile.Picture = data;
Profile.PictureType = "image/png";
}
}
Upvotes: 1
Reputation: 10448
There are so many approaches to resize images, but i like this one
System.Drawing.Image image = System.Drawing.Image.FromFile("FilePath");
int newwidthimg = 160;
float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
int newHeight = Convert.ToInt32(newwidthimg / AspectRatio);
Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight);
Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save("FilePath", ImageFormat.Jpeg);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
I have fix the width because i want all my images having width 160 and height according to the Aspect ratio
Upvotes: 15
Reputation: 8359
You may use some kind of resizeMethod.
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile pf = FileUpload1.PostedFile;
System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream);
bm = ResizeBitmap((Bitmap) bm, 100, 100); /// new width, height
bm.Save(Path.Combine(YOURUPLOADPATH, pf.FileName);
}
private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
return result;
}
On the other hand, uploading a eg 3 MB file and afterwards *resizing* it to a 20kb pic - does not sound very good to me. Perhaps you could consider limiting upload file size.
Upvotes: 9