Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:39:54+00:00 2026-05-24T13:39:54+00:00

This is my first question ever on StackOverflow, hurray! I can honestly say I

  • 0

This is my first question ever on StackOverflow, hurray! I can honestly say I use StackOverflow on daily basis for both my work and personal programming mysteries. 99,9% of the time I actually find the answer I need on here too, which is great!

My current problem actually stumped me a little as I can’t seem to find anything which actually works. I’ve already read several posts on GameDev.net and found other resources around the net but can’t sort it out.

I am in the process of porting a small 2D engine I wrote for XNA to SlimDX (just DirectX9 at the moment), which has been a good move as I learned more about inner-workings of DirectX in just a few days than I did in six months of working with XNA. I got most of my basic rendering features done and actually managed to recreate the XNA SpriteBatch with a ton of additional features (which I really missed in XNA).

One of the last things I’m trying to get to work is to extract a source-rectangle from a given texture and use it for tiling. Why: When not tiling you can just mess around with the UV to get the source you want to display (eg: 0.3;0.3 to 0.5;0.5), but when tiling you need the UV to tile (0;0 to 2;2 means tile images twice) and therefore need a cutout texture.

To make a long story short, I try to use the following:

DataRectangle dataRectangle = sprite.Texture.LockRectangle(0, LockFlags.None);
Format format = sprite.Texture.GetLevelDescription(0).Format;

byte[] buffer = new byte[4];
dataRectangle.Data.Read(buffer, ([y] * dataRectangle.Pitch) + ([x] * 4), buffer.Length);
texture.UnlockRectangle(0);

I tried different pixels but all seem to give bogus data. For example, I actually tried using my current avatar to see if the buffer I got from the DataRectangle matches the actual pixel in the image, but no luck (even checked if the Format was correct, which it is).

What am I doing wrong? Is there a better way to do it? Or is my UV story wrong and can it be solved
much simpler than cutting out a source-rectangle before tiling it?

Thank you for your time,

Lennard Fonteijn

Update #1

I actually managed to export the pixel data to a Bitmap using the following conversion from a byte array:

int pixel = (buffer[0] & 0xFF) | ((buffer[1] & 0xFF) << 8) | ((buffer[2] & 0xFF) << 16) | ((255 - buffer[3] & 0xFF) << 24);

So the data doesn’t seem so bogus as I thought it was. My next problem, however, is grabbing the pixels specified in the source rectangle and copy them to a new texture. The image I’m trying to cut is 150×150, but for some reason it is stretched to a 256×256 image (power of two), but when actually trying to access pixels beyond 150×150, it throws an OutOfBounds exception. Also, when I actually try to create a second blank texture of size 256×256, no matter what I put into it, it turns out completely black.

Here’s my current code:

//Texture texture = 150x150
DataRectangle dataRectangle = texture.LockRectangle(0, LockFlags.None);
SurfaceDescription surface = texture.GetLevelDescription(0);

Texture texture2 = new Texture(_graphicsDevice, surface.Width, surface.Height, 0, surface.Usage, surface.Format, surface.Pool);
DataRectangle dataRectangle2 = texture2.LockRectangle(0, LockFlags.None);

for (int k = sourceX; k < sourceHeight; k++)
{
    for (int l = sourceY; l < sourceWidth; l++)
    {
        byte[] buffer = new byte[4];
        dataRectangle.Data.Seek((k * dataRectangle.Pitch) + (l* 4), SeekOrigin.Begin);
        dataRectangle.Data.Read(buffer, 0, 4);

        dataRectangle2.Data.Seek(((k - sourceY) * dataRectangle2.Pitch) + ((l - sourceX) * 4), SeekOrigin.Begin);
        dataRectangle2.Data.Write(buffer, 0, 4);
    }
}

sprite.Texture.UnlockRectangle(0);
texture2.UnlockRectangle(0);

_graphicsDevice.SetTexture(0, texture2);

So my new (additional) questions are: How can I move over pixels from one texture to another smaller texture, including the Alpha channel? Any why does the SurfaceDescription report 256×256 when my original texture is 150×150?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:39:54+00:00Added an answer on May 24, 2026 at 1:39 pm

    It’s kind of awkward to answer my own question, but after some more digging and by simple trial and error, I found the solution.

    At first, I had to change the way I loaded my texture. To prevent it from internally resizing to a Power-of-Two size, I had to use the following method:

    Texture texture = Texture.FromFile(_graphicsDevice, [filePath], D3DX.DefaultNonPowerOf2, D3DX.DefaultNonPowerOf2, 1, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, 0);
    

    Note how I specifically specified that the size is Non-Power-of-Two.

    Next, there was a mistake in my new texture definition. Instead of specifying 0 levels (and making it auto-generate a MipMap), I had to specify 1 level, like this:

    Texture texture2 = new Texture(_graphicsDevice, [sourceWidth], [sourceHeight], 1, surface.Usage, surface.Format, surface.Pool);
    

    After having done that, the for-loop I have in my actual question, works fine:

    DataRectangle dataRectangle = texture.LockRectangle(0, LockFlags.None);
    SurfaceDescription surface = texture.GetLevelDescription(0);
    
    DataRectangle dataRectangle2 = texture2.LockRectangle(0, LockFlags.None);
    
    for (int y = [sourceX]; y < [sourceHeight]; k++)
    {
        for (int x = [sourceY]; x < [sourceWidth]; l++)
        {
            byte[] buffer = new byte[4];
            dataRectangle.Data.Seek((y * dataRectangle.Pitch) + (x * 4), SeekOrigin.Begin);
            dataRectangle.Data.Read(buffer, 0, 4);
    
            dataRectangle2.Data.Seek(((y - [sourceY]) * dataRectangle2.Pitch) + ((x - [sourceX]) * 4), SeekOrigin.Begin);
            dataRectangle2.Data.Write(buffer, 0, 4);
        }
    }
    
    texture.UnlockRectangle(0);
    texture2.UnlockRectangle(0);
    
    _graphicsDevice.SetTexture(0, texture2);
    

    Everything in brackets is considered a variable from outside this snippet. I suppose _graphicsDevice is clear enough. I am aware the .Seek can be simplified, but I think it works fine for example purposes. Note that I do not recommended doing this kind of operation every frame, as it drains your FPS quite fast when used wrong.

    It took me quite some time to figure it out, but the result is satisfying. I would like to thank everyone who glimpsed over the question and has tried to help me.

    Lennard Fonteijn

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question to stackoverflow so here it goes... I use cruise
this is my first question here so I hope I can articulate it well
Greetings to all! This is my first question here on stackoverflow. I have a
This is my first question so please be patient :) Background: I'm implementing an
This is my very first question so I am a bit nervous about it
This is a really basic question but this is the first time I've used
I know this question might sound a little cheesy but this is the first
Okay this is a fairly broad question. This is my first App and I'm
This question might not seem programming related at first, but let me explain. I'm
So this question will get technical – eventually – but first check out Hanselminutes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.