When I zoom in on an image, GDI+ automatically smoothens the edges
between the pixels. I am looking for a way to see the individual pixels
as squares in the enlarged image, like in MSPaint. I searched in vain in
the enumerations System.Drawing.Drawing2D.SmoothingMode and
System.Drawing.Drawing2D.InterpolationMode to find the constant that
does just that. How?
Some code to illustrate the problem:
using System.Drawing;
using System.Windows.Forms;
class MyForm:Form
{
override protected void OnPaint(PaintEventArgs a)
{
Bitmap bitmap=new Bitmap(2,2);
Graphics graphics=Graphics.FromImage(bitmap);
graphics.Clear(Color.Green);
graphics.DrawLine(Pens.Red,1,1,2,2);
graphics.Dispose();
a.Graphics.DrawImage
(
bitmap,
new Rectangle(0,0,200,200),
new Rectangle(0,0,2,2),
GraphicsUnit.Pixel
);
}
[System.STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
Nicholas Paldino [.NET/C# MVP] - 17 Sep 2007 17:23 GMT
Martijn,
You should be able to set the SmoothingMode and InterpolationMode
properties on the Graphics instance to prevent GDI+ from trying to smooth
things out.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> When I zoom in on an image, GDI+ automatically smoothens the edges between
> the pixels. I am looking for a way to see the individual pixels as squares
[quoted text clipped - 30 lines]
> }
> }
Ben Voigt [C++ MVP] - 17 Sep 2007 18:21 GMT
> When I zoom in on an image, GDI+ automatically smoothens the edges between
> the pixels. I am looking for a way to see the individual pixels as squares
> in the enlarged image, like in MSPaint. I searched in vain in the
> enumerations System.Drawing.Drawing2D.SmoothingMode and
> System.Drawing.Drawing2D.InterpolationMode to find the constant that does
> just that. How?
I would think that NearestNeighbor would be the one that matches the old
StretchBlt functionality.
> Some code to illustrate the problem:
>
[quoted text clipped - 23 lines]
> }
> }
Martijn Mulder - 17 Sep 2007 18:54 GMT
>> When I zoom in on an image, GDI+ automatically smoothens the edges between
>> the pixels. I am looking for a way to see the individual pixels as squares
>> in the enlarged image, like in MSPaint. I searched in vain in the
>> enumerations System.Drawing.Drawing2D.SmoothingMode and
>> System.Drawing.Drawing2D.InterpolationMode to find the constant that does
>> just that. How?
> I would think that NearestNeighbor would be the one that matches the old
> StretchBlt functionality.
System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor it is. It was
'to close', so to say ;)
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class MyForm:Form
{
override protected void OnPaint(PaintEventArgs a)
{
Bitmap bitmap=new Bitmap(2,2);
Graphics graphics=Graphics.FromImage(bitmap);
graphics.Clear(Color.Green);
graphics.DrawLine(Pens.Red,1,1,2,2);
graphics.Dispose();
a.Graphics.InterpolationMode=
InterpolationMode.NearestNeighbor;
a.Graphics.DrawImage
(
bitmap,
new Rectangle(0,0,200,200),
new Rectangle(0,0,1,1),
GraphicsUnit.Pixel
);
}
[System.STAThread]
static void Main()
{
Application.Run(new MyForm());
}
}
Bob Powell [MVP] - 17 Sep 2007 18:47 GMT
This depends on both the interpolation mode and the pixel offset mode. I
have modified your code below.
Bitmap bitmap=new Bitmap(2,2);
Graphics graphics=Graphics.FromImage(bitmap);
graphics.Clear(Color.Green);
graphics.DrawLine(Pens.Red,1,1,2,2);
graphics.Dispose();
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
e.Graphics.DrawImage
(
bitmap,
new Rectangle(0,0,200,200),
new Rectangle(0,0,2,2),
GraphicsUnit.Pixel
);

Signature
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> When I zoom in on an image, GDI+ automatically smoothens the edges between
> the pixels. I am looking for a way to see the individual pixels as squares
[quoted text clipped - 30 lines]
> }
> }