I would like to draw a texture in a rectangle that is smaller than the texture, normally the texture will be scaled to fill the rectangle, which results in a squished image. I want to draw it to fit.

For example, if the green stuff is the Texture, I would like to draw only the stuff in the red rectangle.

enter image description here

I already tried to get the color data from the base texture and create a new one, filled with the part from the base.

Color[] colorData = new Color[progressRectangle.Width * progressRectangle.Height];
Color[] sourceColorData = new Color[t2dBack.Width * t2dBack.Height];
for (int x = 0; x < progressRectangle.Width; x++)
    for (int y = 0; y < progressRectangle.Height; y++)
        colorData[x + progressRectangle.Width * y] = sourceColorData[x + t2dBack.Width * y];


but that just results in gibberish.

Is there an easy way to accomplish this?

kind regards


You can easily draw part of a texture with a simple Spritebatch.Draw command. Consider the following:

// width and height of your red rectangle
int width = 100;
int height = 100;

// start x and y position, in texture coordinates
int x = 120;
int y = 0;

// rectangles for our draw call
Rectangle destinationRectangle = new Rectangle(0, 0, width, height);
Rectangle sourceRectangle = new Rectangle(x, y, width, height);

spriteBatch.Draw(mainTexture, destinationRectangle, sourceRectangle, Color.White);

This draw call, will draw the sourceRectangle portion of the texture onto the screen at the desired location.

See more here:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.