Solved

C# image scaled

Posted on 2010-08-12
7
517 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selenium and Xpath 4 67
Set arraylist as datatable 2 29
Open telerik datetime picker programmatically 13 42
How does GeoJson relates to Leaflet and Mapbox? 4 48
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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