所以我在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;
}
答案 0 :(得分:1)
你有空白区块的原因是这一行:
g.FillRectangle(textureBrush, new Rectangle(currX, currY, myTileWidth, myTileHeight));
坐标currX, currY
指定在拼贴上开始绘制的位置。在循环的第一次迭代之后,这些值超出了tile的范围。
更好的方法可能是尝试使用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;
}
答案 1 :(得分:0)
是你的情况:
g.FillRectangle(textureBrush, new Rectangle(currX, currY, myTileWidth, myTileHeight));
电话试图填写超出界限的坐标?
例如,磁贴是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
中的源位置。不知道你究竟是怎么做的,也许使用翻译变换将其翻译成相反的方向?