By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

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

P: n/a
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());
}
}
Sep 17 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Martijn Mulder" <i@mwrote in message
news:46***********************@news.wanadoo.nl...
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());
}
}

Sep 17 '07 #2

P: n/a

"Martijn Mulder" <i@mwrote in message
news:46***********************@news.wanadoo.nl...
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());
}
}

Sep 17 '07 #3

P: n/a
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.
"Martijn Mulder" <i@mwrote in message
news:46***********************@news.wanadoo.nl...
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());
}
}
Sep 17 '07 #4

P: n/a
>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.Nearest Neighbor 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());
}
}
>
Sep 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.