The Pixels are blurred

A

A. Suresh Kumar

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
 
M

Morten Wennevik [C# MVP]

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

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

Top