0

所以我在 C# 中创建了一个程序,它获取图像并将其拆分为图块。我正处于想要拍摄大图像并将其切成不同的图块并保存每个图块的地步。我遇到的问题是它适用于第一个图块,但所有其他图块都是空白的,我不知道为什么。这是我正在砍的代码。

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;
}
4

2 回答 2

1

你有空白瓷砖的原因是这一行:

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

坐标currX, currY指定开始在图块上绘制的位置。在循环的第一次迭代之后,这些值超出了图块的范围。

更好的方法可能是尝试使用裁剪图像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;
}
于 2013-02-15T19:14:54.407 回答
0

你的情况是这样吗:

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

call 试图填写其范围之外的坐标?

比如tile是10x10,首先调用:g.FillRectangle(textureBrush, new Rectangle(0, 0, 10, 10));

在第二次通话中,您正在有效地做

    g.FillRectangle(textureBrush, new Rectangle(10, 0, 10, 10));

哪个超出了 tempTile 的范围?

fillRectangle调用应始终为0,0,myTileWidth,myTileHeight,它是您要更改的源位置textureBrush不知道你会如何做到这一点,也许使用翻译变换将它翻译成相反的方向?

于 2013-02-15T19:04:06.950 回答