0

I have this code that resizes an image to 300x300. But after resizing it, the image still loses its quality. i've tried other solutions posted on other websites that have the same code implementation to this code but it doesn't work for me. please help me with this.

Stream ream = FileUploadPic.FileContent;
            System.Drawing.Image i = new Bitmap(ream);

            Bitmap Orig_Photo = new Bitmap(i);
            Bitmap resize_Photo = new Bitmap(300,300);
            Graphics thumbnailGraph = Graphics.FromImage(resize_Photo);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.AntiAlias;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;

            var imageRectangle = new Rectangle(0, 0, 300, 300);
            thumbnailGraph.DrawImage(i, imageRectangle);
            System.Drawing.Image ii = new Bitmap(resize_Photo);
nathan gonzalez
  • 11,817
  • 4
  • 41
  • 57
  • What are the dimensions of the input image? – Matt Ball Apr 12 '13 at 03:59
  • what I'm using in my testing has a size of 2709pxx2709px...But even though i try smaller size images the output is still the same. – user2272856 Apr 12 '13 at 04:06
  • 1
    Is there any way you could share a sample? Not sure what "loses quality means". If you have straight lines or text, Bicubic interpolation just blurs each pixel with it's neighbors. – LouD Apr 12 '13 at 04:13
  • @Loud- Please visit this link... This has the same image output as I do...I've tried that author's solution but nothing happened.http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx – user2272856 Apr 12 '13 at 05:22

1 Answers1

0

I have used this code once,so you can give a try to this and this is c# version

  Bitmap img = new Bitmap(newW, newH);
        using (Graphics g = Graphics.FromImage(img))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH));
        }

here newW and newH are new height and width in which you want to re size image .

You also need to include using System.Drawing; name space also .

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • @rahularyansharma-thank you so much...It's now working...My other question now is why is it whenever i'm converting it to byte array, it becomes low quality again?I hope you can also help me with this..thanks. – user2272856 Apr 12 '13 at 06:13
  • Your welcome, if its works for you, you should accept this as answer . Unless you're doing vector graphics, there's no way to resize an image without potentially losing some image quality. – rahularyansharma Apr 12 '13 at 06:20