嘿。 我有一個480×800的圖像,並希望將其放置在我的地圖上。我試圖將圖像分割成網格(6 x 10),併爲每個圖塊分配圖像的特定部分。本質上,由於每個圖塊都附加了圖像的相關部分,所以該圖塊看起來像一個大圖像。這樣做的最好方法是什麼? 我已經嘗試瞭解每個tile並將其繪製到WriteableBitmap,但所有圖像都是相同的。將圖像分解爲圖塊
WriteableBitmap wb = new WriteableBitmap(80,80);
Rect src= new Rect(x*Width,y*Height, 80, 80);
Rect dest= new Rect(0, 0, 80, 80);
wb.Blit(dest, mainWb, src);
tile.SetImage(wb);
(x和y)是剛通過tilemap的迭代時所使用的索引,80是瓦片的高度和寬度和mainWb
是大圖像欲分割。謝謝你的幫助。
編輯:完全循環代碼:
var mainImage = Application.GetResourceStream(new Uri("MainImage.jpg", UriKind.Relative)).Stream;
WriteableBitmap mainWb = new WriteableBitmap(480, 800);
mainWb.SetSource(mainImage);
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 12; y++)
{
Tile tile = new Tile();
WriteableBitmap wb = new WriteableBitmap(80,80);
Rect src = new Rect(x*Width,y*Height, 80, 80);
Rect dest = new Rect(0, 0, 80, 80);
wb.Blit(dest, mainWb, src);
tile.SetImage(wb);
Canvas.SetLeft(tile, x * WIDTH);
Canvas.SetTop(tile, y * HEIGHT);
LayoutRoot.Children.Add(tile);
ls.Add(tile);
}
}
的Tile
類是與圖像控制簡單的80×80畫布稱爲img
。上述SetImage
方法是這樣的:
public void SetImage(WriteableBitmap wb)
{
img.Source = wb;
}
該代碼似乎是正確的。你能發佈整個循環嗎? – 2011-05-19 12:19:03
@ Olivier Payen - 我已經添加了完整的循環。 – Skoder 2011-05-19 14:36:19