0

I have image and while re-sizing it gets Poor Quality

enter image description here

My code:

string sFile = "D:\\Testing\\" + txtnewname.Text;
Bitmap newImage = new Bitmap(Convert.ToInt32(txtwidth.Text), Convert.ToInt32(txtHeight.Text));
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (Graphics gr = Graphics.FromImage(newImage))
{
    gr.SmoothingMode = SmoothingMode.HighQuality;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(image, new Rectangle(0, 0, (Convert.ToInt32(txtwidth.Text)), Convert.ToInt32(txtHeight.Text)));
}
newImage.Save(sFile + ".jpeg", ImageFormat.Jpeg);
Aravind
  • 1,521
  • 2
  • 12
  • 23

1 Answers1

0

Problem is not with the saved quality, IMO its quite fine. The problem is different aspect ratio that you are saving new image to. Thats why it looks of different quality than original image.

You must make sure that the new image persists the original image's aspect ratio by enforcing it. Whatever height or width you provide as input, one of them must be automatically set to retain original aspect ratio.

Bitmap originalImage = Bitmap.FromFile(oFile) as Bitmap;
double ar = (double)originalImage.Height/originalImage.Width;
int newHeight = 100;
int newWidth = (int)(newHeight/ar );
Bitmap newImage = new Bitmap(newWidth, newHeight);
//and then do what you want to do
Irfan
  • 2,713
  • 3
  • 29
  • 43