0

I'm working on a pixel art editor and I stuck in an issue with zooming. I created a simple zooming function which is working properly except one thing... I can't get a pixel perfect result after zooming. I think it is self-explaining and don't need any further explanation what I'm thinking. I tried the different versions of InterpolatinMode but the picture still "smooth".

private Image ZoomPicture(Image img, Size size)
    {
        Bitmap temp_btm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
        Graphics gpu = Graphics.FromImage(temp_btm);
        gpu.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

        return temp_btm;
    }

Check the pictures:

enter image description here

enter image description here

DerStarkeBaer
  • 669
  • 8
  • 28
BushWookie
  • 51
  • 1
  • 1
  • 8
  • 1
    "*I think it is self-explaining and don't need any further explanation*" never assume this. However `NearestNeighbor` is the lowest-quality mode and `HighQualityBicubic` is the highest-quality mode (in general and without getting bogged down into the algorithms and what they do), you also might want to consider setting `SmoothingMode.HighQuality` and `PixelOffsetMode.HighQuality` – TheGeneral Jun 26 '20 at 09:47
  • 1
    Quality is in the eyes of the beholder. You want `NearestNeighbor` for its 'lower' quality See here [image getting blurred when enlarging picture box](https://stackoverflow.com/questions/49554126/image-getting-blurred-when-enlarging-picture-box/49556218?r=SearchResults&s=1|43.1814#49556218) - Also you are leaking the Graphics object! – TaW Jun 26 '20 at 11:12
  • 1
    Your entire scaled up image is created on the first line. The other two lines don't do anything -- it doesn't do any good to set the interpolation mode *after* the scaling is done. Create a blank bitmap, graphics, set the interpolation mode, and then DrawImage the old image onto it – Matt Timmermans Jun 26 '20 at 11:42
  • 1
    Thanks for the helpful answers! I could fix the issue by TaW's link... @TaW Can you explain how did you mean this "leaking the Graphics object" thing? – BushWookie Jun 27 '20 at 23:38
  • Most GDI objects are `IDisposable` i.e.need to be disposed of when no longer needed. Best by using a `using` clause: `Using (Graphics g = Graphics.fromImage(..)) {do stuff..}; return ..` as in the link. If you don't do that you may run out of GDI handles.. – TaW Jun 28 '20 at 01:05

0 Answers0