Very Strange Drawing Error

Cags

Well-known member
Joined
Feb 19, 2004
Messages
690
Location
Melton Mowbray, England
Now maybe im being stupid, but this is driving me really nuts...

Insert the following code onto a normal form and let me know if you see anything strange when you load an image. For the smart-arses amonst you, feel free to tell me whats wrong by just looking at the code :). Basically the images are drawing all wrong. Im wondering if it might be a problem with using GraphicsUnit.Pixel.
C#:
public Bitmap SourceImage;

protected override void OnPaint(PaintEventArgs e)
{
	e.Graphics.Clear(Color.Black);
	
	if(SourceImage != null)
	{
		for(int i = 0; i < 4; i++)
		{
			Rectangle bounds = new Rectangle(i * 60, 0, 60, 240);
			e.Graphics.DrawImage(SourceImage, bounds.X, bounds.Y, bounds, GraphicsUnit.Pixel);
		}

		e.Graphics.DrawImage(SourceImage, 240, 0, new Rectangle(0, 0, 240, 240), GraphicsUnit.Pixel);
		e.Graphics.DrawString("Drawn in strips", new Font("Tahoma", 12), new SolidBrush(Color.White), 5, 5);
		e.Graphics.DrawString("Drawn as one", new Font("Tahoma", 12), new SolidBrush(Color.White), 245, 5);
	}

}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
	// avoid flicker
}

protected override void OnLoad(EventArgs e)
{
	OpenFileDialog openDialog = new OpenFileDialog();
	
	// load an image for demonstration purposes
	if(openDialog.ShowDialog() == DialogResult.OK)
	{
		SourceImage = new Bitmap(openDialog.FileName);
	}

	this.ClientSize = new Size(480, 240);
}
 
Well Ive worked out the problem goes away if I specify the targets width and height (by using the overload that excepts the targetRect rather than x and y). This seems odd to me as if the source is that size I assumed it would make the target that size. Perhaps its down to DPI if anyone can explain it please post.
 

Similar threads

A
Replies
0
Views
72
ahmeddc
A
D
Replies
0
Views
64
Draw textures from txt-file (monogame)
D
Back
Top