Click here to Skip to main content
13,000,324 members (74,951 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In the following code:
void f10(Graphics g)
	var img = new Bitmap(1, 1);
	img.SetPixel(0, 0, Color.Black);
	var destRect = pictureBox1.ClientRectangle;
	destRect.Inflate(-5, -5);
	g.DrawRectangle(new Pen(Color.Blue), destRect);
	var srcRect = new RectangleF(-.5f, -.5f, img.Width, img.Height);
	g.InterpolationMode = InterpolationMode.NearestNeighbor;
	g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
private void pictureBox1_Paint(object sender, PaintEventArgs e)
private void pictureBox1_Resize(object sender, EventArgs e)
	textBox1.Text = pictureBox1.ClientSize.Width + "x" + pictureBox1.ClientSize.Height;

I get the following result:


The form contains a picture box that fits the whole inside content of the form and a text box that shows the dimensions of the picture box. I expect the right side of the black box to fit the whole inside of the blue rectangle. Why it doesn't?!

Unexpected gap

By changing the values of dimensions (resizing the picture box) the gap changes.
Posted 13-Apr-12 22:49pm
Updated 13-Apr-12 22:51pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That's a tricky one but I believe it's in the interpolation mode you are using.
On the form I enable DoubleBuffering andf commented out the Interpolation mode and it seems to have fixed the problem.
ilostmyid2 14-Apr-12 9:48am
thanks for your answer.
indeed setting the interpolation to NearestNeighbor causes the problem to get exposed better. i've a project in which the interpolation is not set (the default is used). since this gap effect happened there, i decided to write a test code and found that it appears in both cases of default interpolation mode and non-default interpolation mode.
now, is it a bug? how can i resolve it? it seems not to be reasonable.
for more discussion, please come here:
i've described more about it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I believe this[^] may be the reason for the problem.
Also you need to dispose of the image after you are done with it;

One more thing why are you using a PictureBox? why not output directly to the form it would be faster and less prone to problems.
ilostmyid2 15-Apr-12 4:21am
indeed, i want to use it in web and it needs to be on a control for panning.
i believe that the problem won't be resolved if the picturebox gets omitted.
i read the link, thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 14 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100