Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In the following code:
C#
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)
{
	f10(e.Graphics);
}

private void pictureBox1_Resize(object sender, EventArgs e)
{
	textBox1.Text = pictureBox1.ClientSize.Width + "x" + pictureBox1.ClientSize.Height;
	pictureBox1.Invalidate();
}

I get the following result:

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
Updated 13-Apr-12 22:51pm
v2

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.
 
Share this answer
 
Comments
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:
http://stackoverflow.com/questions/10152225/another-unexpected-result-from-graphics-drawimage
i've described more about it.
thanks
I believe this[^] may be the reason for the problem.
Also you need to dispose of the image after you are done with it;
img.Dispose();

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900