Solved

DrawImage without interpolation

Posted on 2014-02-15
5
377 Views
Last Modified: 2014-03-03
So, I'm working on a sort-of image manipulation program in .NET, and I'm trying to draw an image to the screen, and when you mouse wheel you zoom into the image to view individual pixels (similar to microsoft paint, etc).

I'm using the code

pe.Graphics.DrawImage(m_Bitmap, draw_rect)

in the form OnPaint method to do this.

However, I noticed I was getting smoothing when I zoomed in, and the pixels were looking blurry.  I found on another topic that I could set the "InterpolationMode" and adjust the smoothing method to get the "blocky" pixels I wanted, the other article said "NearestNeighbor" would do it.

However this isn't quite right... it is shifting the first pixel for some reason.  See the attached image.  Also see the original bitmap in microsoft paint,  Each row is supposed to be two pixels wide, and the "Nearestneighbor" interpolation seems to halve the first row and column of pixels.

Is there a way to turn interpolation off altogether?   I'm trying to find a way to not iterate through each pixel and draw the individual squares...

Thanks for any thoughts.
shifted.png
paint.png
0
Comment
Question by:RonMexico
  • 3
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39862247
Hi RonMexico;

Using NearestNeighbor Will produce an image of lower quality. To get the best image quality use HighQualityBicubic. InterpolationMode must be set to a value. Be advised that the higher the quality, InterpolationMode, the longer it takes to process the image.
0
 

Author Comment

by:RonMexico
ID: 39862266
Sorry, probably wasn't clear:

I actually don't want smoothing/interpolation at all.  I want to zoom in and examine pixels and have them look like perfect squares that can be clicked on.  (I'm creating an image manipulation program.)

When I use NearestNeighbor my first row and columns are halved, as shown in the first attached picture.  What I want is behavior like in MS Paint, such as in the second attached picture.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 39862751
Hi RonMexico;

The InterpolationMode enumeration specifies the algorithm that is used when images are scaled or rotated.. It must be set to one of the enumeration values. The only enumeration that may not do any interpolation may be the Default setting, it does not say specifically what it does.

There is another property that handles smoothing and that is SmoothingMode. If you do not what smoothing set the SmoothingMode property to Drawing2D.SmoothingMode.None
0
 

Accepted Solution

by:
RonMexico earned 0 total points
ID: 39863040
Default performs interpolation, unfortunately.

And the SmoothingMode doesn't seem to affect DrawImage (it seems to only affect GDI drawing commands such as DrawLine).

This is starting to appear like a bug, or at least an oversight in .NET.
0
 

Author Closing Comment

by:RonMexico
ID: 39900012
Seems to me at this point that this is design behavior, although it *ought* to behave like I suggest.  Awarding Fernando the points for the insight but the answer ought to lead to my comment I think.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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