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 / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

The Pixels are blurred

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
A. Suresh Kumar - 18 Mar 2008 07:06 GMT
Hi,
 Iam Facing some problem in the gradient of pixels while invoking DrawIamge
of System.Graphics. My objective is to position a set of pixels in a bitmap
and enlarge it based on the dimension specified for the container.I choose
DrawImage method of Graphics object which Draws the specified portion of the
specified Image at the specified location and with the specified size.
While experimenting, I could see that the pixels drawn were enlarged and
stretched. But the rectangles of the pixels formed are blurred and forms a
gradient from the middle to the edges of each pixel rectangle. But what I
really want is something which is of equal gradient in the stretched pixel.
Please find the code snippet below,

private void button1_Click(object sender, EventArgs e)
       {
           Graphics g = this.CreateGraphics();
       Bitmap bg = new Bitmap(9, 8);// CREATING A BITMAP OBJECT WHICH SIZE
9X8, ADDING SIX PIXELS TO THE BITMAP
           bg.SetPixel(1, 1, Color.Red);
           bg.SetPixel(1, 3, Color.Blue);
           bg.SetPixel(1, 5, Color.Green);
           bg.SetPixel(3, 1, Color.Orange);
           bg.SetPixel(3, 3, Color.Violet);
           bg.SetPixel(3, 5, Color.Pink);
           g.DrawImage(bg, 50, 50, 200, 200); //ENLARGING AND DRAWING THE
BITMAP TO SIZE 200 X 200
                        //HERE IS WHERE THE PIXELS FORM RECTANGLES WITH GRADIENT FROM MIDDLE
TO EDGES
       }

My requirement is, the stretched pixel should be a rectangle with solid
color and constant gradient. How do I acheive this ?

Any help will be really appreciated.

Regards,
suresh

Signature

suresh Kumar A

Morten Wennevik [C# MVP] - 18 Mar 2008 10:56 GMT
Hi Suresh,

This is the default behaviour of drawing.  If you don't want the pixels to
be interpolated when the image is resized specify so in the graphics object.

           g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

By the way, any graphics object you create yourself (or any disposable
object, but especially window objects), should be disposed as soon as
possible. And to make this easier to remember, enclose your code in a using
block

Not to mention, you rarely ever should draw directly to a Control's Graphics
object, instead, change the Bitmap in the button event, then call
Invalidate() and have the Paint event draw the bitmap.  This way you won't
lose the drawing when you move something over the control.

Bitmap bg = new Bitmap(9, 8);
private void button1_Click(object sender, EventArgs e)
{
   bg.SetPixel(1, 1, Color.Red);
   bg.SetPixel(1, 3, Color.Blue);
   bg.SetPixel(1, 5, Color.Green);
   bg.SetPixel(3, 1, Color.Orange);
   bg.SetPixel(3, 3, Color.Violet);
   bg.SetPixel(3, 5, Color.Pink);

   Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);

   e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
   e.Graphics.DrawImage(bg, 50, 50, 200, 200);
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> Hi,
>   Iam Facing some problem in the gradient of pixels while invoking DrawIamge
[quoted text clipped - 32 lines]
> Regards,
> suresh

Rate this thread:







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.