Bushuev
Bushuev

Reputation: 577

How to show an image in PictureBox if the picture can be from 10x10 to 500x500

Now I used next code to download picture from desctop to PictureBox

        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "BMP|*.bmp";
        if (openFileDialog.ShowDialog() != DialogResult.OK)
            return;

        pictureBox.Load(openFileDialog.FileName);
        pictureBox.SizeMode=PictureBoxSizeMode.StretchImage;
        pictureBox.BorderStyle = BorderStyle.Fixed3D;

If I used a normal picture(100x100) it looks nice(unfuzzy).

enter image description here

If I used a smal picture(15x15) it looks fuzzy.

This picture has 15x15 size

I want to know how to make them unfuzzy( they can look like bricks, but they need to be unfuzzy);

Waiting result for small picture need to look like this

enter image description here

Waiting result for normal picture need to look like this

enter image description here

Upvotes: 1

Views: 733

Answers (2)

Gediminas Masaitis
Gediminas Masaitis

Reputation: 3212

The fuzziness comes from image interpolation. To have your desired blocky look, you need to use nearest neighbour interpolation.

Thankfully, .NET has this build into. You can set to the interpolation mode to the GDI Graphics object which renders your PictureBox.

To do so I'd recommend exteding the PictureBox user control and adding an InterpolationMode parameter. Then, you can override the OnPaint method, and use your interpolation mode.

public class InterpolatingPictureBox : PictureBox
{
    public InterpolationMode InterpolationMode { get; set; }

    protected override void OnPaint(PaintEventArgs eventArgs)
    {
        eventArgs.Graphics.InterpolationMode = InterpolationMode;
        base.OnPaint(eventArgs);
    }
}

NearestNeighbour, Bilinear and HighQualityBilinear interpolations, upscaling a 16x16 image to 256x256

Here is a 16x16 image: the original 16x16 image, upscaled to 256x256 using

  • NearestNeighbour (left)
  • Bilinear (which is default, middle)
  • HighQualityBilinear (right)

All are rendered on the InterpolatingPictureBox control.

Upvotes: 4

TomTom
TomTom

Reputation: 62157

You have a SERIOUS problem. There is no easy solution.

I want to know how to make them unfuzzy

Impossible. Sharpening (which is what you want) of images to larger resolution is possible within reason, but this takes seriously advanced programs that run for quite some time. Not talking as programmer here - but this is a standard step in printing fine art photography. Photos have a relatively low resolution (mine come at up to 43 megapixel) and printers a much higher one.

There are some AA approaches that are easy, but 10x10 sized up t0 100x100 is going to challenge even the most advanced programs - 10x10 is nothing to work with. You talk of 10x starting with no data on the beginning.

You CAN turn off anti aliasing, resize the image in code (easy to do) with no anti aliasing - but this will turn real pictures looking UGLY. .NET has no real high level sharpening functionality and again, 10x10 is not enough to start working with.

The best you can do is not load them into a picture box. Load them into a bitmap, then draw that onto a new bitmap of optimal size (100x100). This at least gives you full control over the resizing.

The code for this you can find at Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio

while

Image resizing in .Net with Antialiasing

tells you how to select anti aliasing.

But again: expect quite little. Those anti aliasing modes are purely technical and not anything that you use for photos. Plus selecting the right one depends on the input image - so you can use a default or give the user a choice. I am probably overcomplicating things, but outside programming I actually DO deal with images that need to be scaled up with as little image loss as possible for large printing.

Upvotes: 0

Related Questions