How can I resize an image when downloading?
asp.net, c#, download, image, resize
Solution
Put this code after the try / finally block -
This will resize the image to 1/4 its original size.
using (System.Drawing.Image original = System.Drawing.Image.FromFile(fullPath))
{
int newHeight = original.Height / 4;
int newWidth = original.Width / 4;
using (System.Drawing.Bitmap newPic = new System.Drawing.Bitmap(newWidth, newHeight))
{
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(newPic))
{
gr.DrawImage(original, 0, 0, (newWidth), (newHeight));
string newFilename = ""; /* Put new file path here */
newPic.Save(newFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
You can of course change it to any size you want, by changing the newHeight and newWidth variables
UPDATE: modified code to use using() {} instead of dispose, as per comment below.
Problem
I want to resize my image when I am downloading it, or after download. This is my code. Quality is not important. ``` public void downloadPicture(string fileName, string url,string path) { string fullPath = string.Empty; fullPath = path + @"\" + fileName + ".jpg"; //imagePath byte[] content; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); using (BinaryReader br = new BinaryReader(stream)) { content = br.ReadBytes(500000); br.Close(); } response.Close(); FileStream fs = new FileStream(fullPath, FileMode.Create); // Starting create BinaryWriter bw = new BinaryWriter(fs); try { bw.Write(content); // Created } finally { fs.Close(); bw.Close(); } } ``` So how can I do it?