How to show individual pixels? (Was: Show Me The Pixel)

M

Martijn Mulder

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());
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

Ben Voigt [C++ MVP]

Martijn Mulder said:
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:

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());
}
}
 
B

Bob Powell [MVP]

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

);


--
--
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.
 
M

Martijn Mulder

When I zoom in on an image, GDI+ automatically smoothens the edges between
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());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Show Me The Pixel 2
The Mighty BitBlt 6
The Pixels are blurred 1

Top