spriteBatch.Draw(Texture2D, Rectangle, Color) does not work correctly

I want to draw texture 1x1 pixels ob blue color on rectangle 34x8 pixels.
So my code is:

Texture2D pixel = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
pixel.SetData(new[] { Color.White });
Rectangle rect = new Rectangle((int)point.X, (int)point.Y, (int)length, (int)thickness);
spriteBatch.Draw(pixel, rect, Color.Blue);

And I get bad result

You can see that top pixels row goes out of bounds (inside red circle)

Are you drawing with SamplerState.PointClamp?

Looks to me like integer maths issues.

(int)point.Y could be causing your problem.

Look at the value of the Y coordinate and see if it is being rounded down.

Then look at length and see if it is rounded up

hard to say with such a blurry image, but looks to me like the spritebatch has no problem but he’s rendering the "X"square after the blue square. The ‘apendix’ portion is rectangular (more than 1 pixel width) and this is probably impossible to produce with a clamping issue or to be a point/linear sampler issue.

2 Likes

I don’t understand what you say about

I think you don’t understand youself what you write… Look at Rectangle constructor, X,Y,Width, Height. There all right

Image is blurry because it is zoomed. I’ve would that you notice better 1 pixel row.
Using my code can you try repeat this thing?

Please show more of your drawing code, including your SpriteBatch.Begin call(s). SamplerState is one of the states that influences how things are rendered. PointClamp is typically used for pixel-perfect rendering.

It’s also not recommended to create and use a new Texture2D like this, or you’ll quickly run into memory issues since it’s not reclaimed by the GC. Create it once and cache it, or dispose it once you’re done with it.

Oops, I’m sorry. But I understoot that my mistake - there was overlap two textures. Therefore it was happened such effect.

Exactly, I think you are right. The white rectangle is quite clearly drawn on top of the blue one.