Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+
Hi,
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)
{
	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 13-Apr-12 23:49pm
Edited 13-Apr-12 23:51pm
v2
Rate this: bad
good
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.
  Permalink  
Comments
ilostmyid2 at 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
Rate this: bad
good
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;
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.
  Permalink  
Comments
ilostmyid2 at 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
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 14 Apr 2012
Copyright © CodeProject, 1999-2014
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