I have a webform contains a file upload control and a button, when uploading the image I want to resize it and decrease its physical size (in KB).
I use a method that Do the resizing but the image size still big, i tried to decrease the resolution using
Bitmap.SetResolution( , )
but it didn't affect the size of the image in KB
Here's my aspx code:
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
and Here's the code behind:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
int Image_Width = 830;
int Image_Height = 430;
string ImageName = "f_" + DateTime.Now.Millisecond + DateTime.Now.DayOfYear + DateTime.Now.Minute + DateTime.Now.Hour;
HttpPostedFile pf = FileUpload1.PostedFile;
ImageName += Path.GetExtension(pf.FileName);
System.Drawing.Image bm_Large = System.Drawing.Image.FromStream(pf.InputStream);
bm_Large = ResizeBitmap((Bitmap)bm_Large, Image_Width, Image_Height);
string pathToSave_Large = Server.MapPath("~/images/Test Ajax/" + ImageName);
bm_Large.Save(pathToSave_Large);
FileUpload1.PostedFile.InputStream.Close();
}
}
public static 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;
}