Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martijn Mulder - 17 Sep 2007 16:01 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?

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]
>  }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.