Solved

C# image scaled

Posted on 2010-08-12
7
482 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 74

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

20 Experts available now in Live!

Get 1:1 Help Now