Solved

DrawImage without interpolation

Posted on 2014-02-15
5
375 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 62

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 62

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 11 23
Get number of Files in Directory and Sub Directories 2 39
No Data for DropDown List 2 23
How come this regular expression allows "e"? 14 35
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

776 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