Solved

C# image scaled

Posted on 2010-08-12
7
520 Views
Last Modified: 2012-05-10
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
Comment
Question by:MatthewOsosky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33426798
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
 
LVL 3

Expert Comment

by:declanmcd
ID: 33427831
Have you tried changing the image format to something like a GIF?
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 250 total points
ID: 33429211
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
Stressed Out?

Watch some penguins on the livecam!

 
LVL 1

Author Comment

by:MatthewOsosky
ID: 33429848
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
 
LVL 3

Assisted Solution

by:MrHorizontal
MrHorizontal earned 250 total points
ID: 33440162
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
 
LVL 1

Author Closing Comment

by:MatthewOsosky
ID: 33440975
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
 
LVL 1

Author Comment

by:MatthewOsosky
ID: 33440976

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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question