2011-06-14 120 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)。

相关问题