• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

C# image scaled

If you select to strech a bitmap to fit a image of some size that is greater, the bitmap pixels are blended using some kind of algorithm that 'blends' (lack of better word) the pixel from one to the next. What I want is the scale the image but still have crisp pixel boundaries. I can't a setting on the picture box to get this result.

Anybody know?

Thanks,
Matthew
0
MatthewOsosky
Asked:
MatthewOsosky
2 Solutions
 
käµfm³d 👽Commented:
The reason this happens is because you are most likely using a "lossy" format (e.g. jpeg). "Lossy" means that during compression some of the image data is lost for the sake of compression ratio. This is the reason you get blotchy images when you scale an image upward--the computer has to "guess" at what data should go where the "missing" data is. Some "guessing" algorithms are better than others (and forgive me, I'll have to defer to someone else to tell you which are the better ones), but TMK you will never a perfect recreation. You may get close, but I'm hesitant to say that.
0
 
declanmcdCommented:
Have you tried changing the image format to something like a GIF?
0
 
b_levittCommented:
I don't think you can control the interpolation mode of a picturebox...

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/b647603b-a6bc-4468-a06b-048691a9086d

But this guy rolled his own that allows you to select the interpolation:
http://www.xtremedotnettalk.com/showthread.php?t=97904

In the end, the simplest way is to start with an image that is larger than you need.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
MatthewOsoskyAuthor Commented:
The image is a bitmap, it's not a problem of compression.

I would think it's pretty lame on Microsofts part to provide this functionality. The implementation of bi linear interpolation is far more complex then snapping pixel colors to the nearest scaled colors. I cannot start with a larger image because I am specifically looking at the per pixel properties on the bitmap. With the interpolation on it's much more difficult to see whats going on.

Yes I thought I could roll my own. It would be easy enought to do the calcualations to scale the bitmap pixels as sqaures onto a larger bitmap using GDI+, but I thought it should be as easy as a setting in the picturebox class.

I'll wait a few more days to se if somebody may know.

0
 
MrHorizontalCommented:
If you need code to resize the image in C#, see this article on CodeProject: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

You'll notice there's an enum being used called 'InterpolationMode.HighQualityBicubic', which is the best quality resizing, however, if you look at MSDN for this enum (http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx), you'll find there are different interpolation modes between the pixels, which are in increasing levels of quality:

NearestNeighbor
Bilinear
HighQualityBilinear
Bicubic
HighQualityBicubic

I 'think' what you're looking for is no smoothing (ie no 'blending'), which means you should probably try InterpolationMode.NearestNeighbor
0
 
MatthewOsoskyAuthor Commented:
That's close. probably as close as this is getting. Those settings are for the graphics object, which i could use to copy and redraw the bitmap. But I had already easily implented my code. I will post code below.
0
 
MatthewOsoskyAuthor Commented:

public static Bitmap ScaleUpBitmap(Bitmap original, int scale)
        {
            int h = original.Height * scale;
            int w = original.Width * scale;

            Bitmap b = new Bitmap(w, h);

            Graphics g = Graphics.FromImage(b);

            for (int y = 0; y < original.Height; y++)
            {
                for (int x = 0; x < original.Width; x++)
                {
                    Color c = original.GetPixel(x, y);

                    Brush br = new SolidBrush(c);

                    Point p = new Point(x * scale, y * scale);

                    Rectangle r = new Rectangle(p.X, p.Y, scale, scale);
                    g.FillRectangle(br, r);

                }
            }




            return b;

        }

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now