2013-02-15 5 views
0

이미지를 가져 와서 타일로 분할하는 C#으로 프로그램을 만듭니다. 나는 큰 이미지를 가져 와서 다른 타일로 잘라서 각 타일을 저장하고 싶습니다. 내가 가지고있는 문제는 첫 번째 타일에 대해서는 작동하지만 다른 모든 타일은 비어 있으며 이유는 확실하지 않다는 것입니다. 여기 내가 자르기를하고있는 코드가있다.C#에서 TextureBrush를 반복적으로 사용하여 이미지 샘플링하기

g.FillRectangle(textureBrush, new Rectangle(currX, currY, myTileWidth, myTileHeight)); 

전화가 경계 외부의 좌표를 입력하려고 : 그것은 당신의 경우

Graphics g; 
Image tempTile; 
TextureBrush textureBrush; 
int currRow = 1; 
int currCol = 1; 
int currX = 0; //Used for splitting. Initialized to origin x. 
int currY = 0; //Used for splitting. Initialized to origin y. 

//Sample our new image 
textureBrush = new TextureBrush(myChopImage); 

while (currY < myChopImage.Height) 
{ 
    while (currX < myChopImage.Width) 
    { 
     //Create a single tile 
     tempTile = new Bitmap(myTileWidth, myTileHeight); 
     g = Graphics.FromImage(tempTile); 

     //Fill our single tile with a portion of the chop image 
     g.FillRectangle(textureBrush, new Rectangle(currX, currY, myTileWidth, myTileHeight)); 

     tempTile.Save("tile_" + currCol + "_" + currRow + ".bmp"); 

     currCol++; 
     currX += myTileWidth; 

     g.Dispose(); 
    } 

    //Reset the current column to start over on the next row. 
    currCol = 1; 
    currX = 0; 

    currRow++; 
    currY += myTileHeight; 
} 

답변

1

이유는이 라인입니다. 루프를 처음 반복 한 후에는이 값이 타일 범위 밖에 있습니다.

더 나은 방법은 다음과 같이 보일 수 Bitmap.Clone

while (currY < myChopImage.Height) 
{ 
    while (currX < myChopImage.Width) 
    { 
     tempTile = crop(myChopImage, new Rectangle(currX, currY, myTileWidth, myTileHeight)); 
     tempTile.Save("tile_" + currCol + "_" + currRow + ".bmp"); 

     currCol++; 
     currX += myTileWidth; 
    } 

    //Reset the current column to start over on the next row. 
    currCol = 1; 
    currX = 0; 

    currRow++; 
    currY += myTileHeight; 
} 

자르기 방법을 사용하여 이미지를 자르기 시도 할 수 있습니다 :

private Bitmap crop(Bitmap bmp, Rectangle cropArea) 
{ 
    Bitmap bmpCrop = bmp.Clone(cropArea, bmp.PixelFormat); 
    return bmpCrop; 
} 
+0

완벽하게 작동합니다. 문제에 대한 해결책을 실제로 주셔서 대단히 감사합니다. – Katianie

0

인가?

예를 들어, 타일은 10x10입니다. 첫 번째 호출은 입니다 .FillRectangle (textureBrush, new Rectangle (0, 0, 10, 10));

와 두 번째 통화에

, 당신은 effeetively

tempTile의 경계를 벗어나는
g.FillRectangle(textureBrush, new Rectangle(10, 0, 10, 10)); 

거야?

fillRectangle 호출이 항상 0,0,myTileWidth,myTileHeight해야한다고, 당신이 변경하려는 textureBrush에서 소스 위치입니다. 정확히 어떻게 할 지 확신하지 못했을 것입니다. 아마도 변환 변환을 사용하여 반대 방향으로 변환 할 수 있습니까?

g.FillRectangle(textureBrush, new Rectangle(currX, currY, myTileWidth, myTileHeight));

좌표가 currX, currY 타일에 그리기 시작하는 지정 : 당신이 빈 타일을 왜

+0

나는 그것의 경계의 외출 beleve니까. 죄송합니다 귀하의 답변을 dosent relrelly 도와주세요. – Katianie

+0

당신은 믿지 않습니까? 테스트에서 currx/2, curry/2를 호출하고 저장된 이미지에 타일의 1/4이 그려지는지 확인하십시오. –

관련 문제