Solved

C# image scaled

Posted on 2010-08-12
7
491 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now