2013-04-21 7 views
1

xna에서 texture2d를 자르고 자합니다. 오른쪽과 오른쪽에있는 이미지를자를 다음 코드를 찾았습니다. 코드를 가지고 놀았으며 특정 간격으로 모든면을 잘라낼 방법을 찾지 못했습니다. 아래 코드는 수정하려고 시도한 코드입니다.xna의 모든면에 texture2d 자르기 #

어떤 도움이나 아이디어라도 감사하겠습니다.

Rectangle area = new Rectangle(0, 0, 580, 480); 

     Texture2D cropped = new Texture2D(heightMap1.GraphicsDevice, area.Width, area.Height); 
     Color[] data = new Color[heightMap1.Width * heightMap1.Height]; 
     Color[] cropData = new Color[cropped.Width * cropped.Height]; 

     heightMap1.GetData(data); 

     int index = 0; 


     for (int y = 0; y < area.Y + area.Height; y++) // for each row 
     { 

       for (int x = 0; x < area.X + area.Width; x++) // for each column 
       { 
        cropData[index] = data[x + (y * heightMap1.Width)]; 
        index++; 
       } 

     } 

    cropped.SetData(cropData); 
+0

무슨 잘못? 문제의 원인은 무엇입니까? – Cyral

+0

이미지의 모든면에서 20 픽셀 자르기가 필요합니다. 위쪽과 오른쪽에서 20 자루 밖에 자르지 않습니다. ( –

+0

텍스처를 잘라내어 다운로드 나 다운로드로 사용자에게 출력물을 다시 제공해야합니까? 당신은 텍스처를 자른 것을 그려야 만합니까? 화면에 더 큰 텍스처의 일부분을 그려 넣으려고하면 –

답변

2

다음은 텍스처를 자르기위한 코드입니다. GetData 메서드는 이미 이미지의 직사각형 하위 섹션을 선택할 수 있으므로 수동으로자를 필요가 없습니다.

물론
// Get your texture 
Texture2D texture = Content.Load<Texture2D>("myTexture"); 

// Calculate the cropped boundary 
Rectangle newBounds = texture.Bounds; 
const int resizeBy = 20; 
newBounds.X += resizeBy; 
newBounds.Y += resizeBy; 
newBounds.Width -= resizeBy * 2; 
newBounds.Height -= resizeBy * 2; 

// Create a new texture of the desired size 
Texture2D croppedTexture = new Texture2D(GraphicsDevice, newBounds.Width, newBounds.Height); 

// Copy the data from the cropped region into a buffer, then into the new texture 
Color[] data = new Color[newBounds.Width * newBounds.Height]; 
texture.GetData(0, newBounds, data, 0, newBounds.Width * newBounds.Height); 
croppedTexture.SetData(data); 

, 당신도 주위의 모든 텍스처 데이터를 복사 할 필요가되지 않을 수도 있습니다 SpriteBatch.Draw가하는 sourceRectangle 매개 변수를 취할 수 있음을 명심! 원래 텍스처의 하위 섹션 만 사용하십시오. 예를 들어 :

spriteBatch.Draw(texture, Vector2.Zero, newBounds, Color.White); 

(어디 newBounds 첫 번째 코드 목록에 같은 방식으로 계산됩니다.) 그것과

+0

건배 앤드류 내가 정확히 무엇을 필요로합니까 :) 고맙습니다. –