2011-06-14 167 views
2

我想繪製一個白色的矩形(100 x 200),並在中間放置一個較小的圖像(50 x 75),以便它看起來類似於紙牌的背面。在另一個頂部覆蓋圖像

使用下面的代碼,我得到的只是周圍有邊框的圖塊,但沒有圖像。

 //generate temporary control to render image 
     Image temporaryImage = new Image { Source = emptyTileWatermark, Width = destWidth, Height = destHeight }; 

     //create writeableBitmap 
     WriteableBitmap wb = new WriteableBitmap(outputWidth, outputHeight); 
     wb.Clear(Colors.White); 

     // Closed green polyline with P1(0, 0), P2(outputWidth, 0), P3(outputWidth, outputHeight) and P4(0, outputHeight) 
     var outline = new int[] { 0, 0, outputWidth, 0, outputWidth, outputHeight, 0, outputHeight, 0, 0}; 
     wb.DrawPolyline(outline, Colors.Black); 
     wb.Invalidate(); 
     wb.Render(temporaryImage, new TranslateTransform { X = destX, Y = destY }); 
     wb.Invalidate(); 

我應該指出要做.Clear(),我正在使用WriteableBitmapEx項目。

任何想法???

回答

2

您的temporaryImage尚未執行其佈局,所以在渲染時它仍然是空白的。爲了彌補這一點,你應該稱之爲度量和排列,這並不總是可靠的,但將其包裹在邊界中並測量和安排似乎可行的方法。

所有的說法,因爲你已經使用WriteableBitmapEx你可以使用它的Blit方法。

WriteableBitmap emptyTile = new WriteableBitmap(emptyTileWatermark); 
//create writeableBitmap 
WriteableBitmap wb = new WriteableBitmap(outputWidth, outputHeight); 
wb.Clear(Colors.White); 

// Closed green polyline with P1(0, 0), P2(outputWidth, 0), P3(outputWidth, outputHeight) and P4(0, outputHeight) 
var outline = new int[] { 0, 0, outputWidth, 0, outputWidth, outputHeight, 0, outputHeight, 0, 0}; 
wb.DrawPolyline(outline, Colors.Black); 
wb.Blit(new Rect(destX,destY,destWidth,destHeight),emptyTile,new Rect(0,0,emptyTileWatermark.PixelWidth,emptyTileWatermark.PixelHeight)); 
+0

完美地工作!謝謝! – 2011-06-14 05:14:48

0

我不是一個圖像處理專家,但它似乎Blit函數將在這種情況下有用。而不是試圖呈現temporaryImage,請創建一個新的WriteableBitmap與您的emptyTileWatermark作爲其來源。使用此WriteableBItmap作爲Source參數,使用wb作爲Dest參數,並將兩個WriteableBitmaps blit。 (Blit功能附帶WriteableBitmapEx)。

相關問題