Answered by:
Image Resize problem, Size is too much

Question
-
User366826142 posted
I have picture size problem in Resizing , the code resize the picture correct but the problem is that, orignial file size is 277 kb but new Resize picture is almost 1 Mb. is there any one please help me. code is following.
int width=800;
int width=600;
string srcImgPath="c:\file1.jpg";
System.Drawing.Image oImg = System.Drawing.Image.FromFile(srcImgPath);
System.Drawing.Image oThumbNail = new Bitmap(width, height,PixelFormat.Format24bppRgb);Graphics oGraphic = Graphics.FromImage(oThumbNail);
Rectangle oRectangle = new Rectangle(0,0,width,height);
oGraphic.DrawImage(oImg, oRectangle);
oThumbNail.Save(d:\file2.jpg);
oGraphic.Dispose();
oImg.Dispose();
Sunday, March 29, 2009 10:01 AM
Answers
-
User187056398 posted
I don't know why that code isn't working correctly. If you just want something that works, you can try this:
Bitmap OriginalBM = new Bitmap(MapPath(@"~\images\april.jpg")); Size newSize = new Size(OriginalBM.Width / 2, OriginalBM.Height / 2); Bitmap ResizedBM = new Bitmap(OriginalBM, newSize); ResizedBM.Save(MapPath(@"~\images\april2.jpg"), ImageFormat.Jpeg);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, March 29, 2009 2:10 PM
All replies
-
User187056398 posted
The code you posted does not compile.
Sunday, March 29, 2009 10:11 AM -
User366826142 posted
here is complete successful code,& please make a file c:/file.jpg before testing.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int width=800;
int height=600;
System.Drawing.Image oImg = System.Drawing.Image.FromFile("c:/file.jpg");
System.Drawing.Image oThumbNail = new Bitmap(width, height,PixelFormat.Format24bppRgb);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
Rectangle oRectangle = new Rectangle(0,0,width,height);
oGraphic.DrawImage(oImg, oRectangle);
oThumbNail.Save("c:/resizefile.jpg");
oGraphic.Dispose();
oImg.Dispose();
}
}
Sunday, March 29, 2009 1:21 PM -
User187056398 posted
I don't know why that code isn't working correctly. If you just want something that works, you can try this:
Bitmap OriginalBM = new Bitmap(MapPath(@"~\images\april.jpg")); Size newSize = new Size(OriginalBM.Width / 2, OriginalBM.Height / 2); Bitmap ResizedBM = new Bitmap(OriginalBM, newSize); ResizedBM.Save(MapPath(@"~\images\april2.jpg"), ImageFormat.Jpeg);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, March 29, 2009 2:10 PM -
User366826142 posted
Thankyou very much for your code, i learnt 1 thing from your code, now my code is also correct,i was missing Imageformat Type.
oThumbNail.Save("c:/resizefile.jpg");
oThumbNail.Save("c:/resizefile.jpg",ImageFormat.Jpeg);
Monday, March 30, 2009 4:00 AM