将图像叠加在另一个上面

时间:2011-06-14 01:11:57

标签: windows-phone-7

我正在尝试绘制一个白色矩形(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 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

我不是图像处理专家,但似乎Blit函数在这种情况下很有用。不要尝试呈现temporaryImage,而是以WriteableBitmap为源,创建一个新的emptyTileWatermark。将此WriteableBItmap用作Source参数,将wb用作Dest参数,然后将两个WriteableBitmaps blit。 (Blit函数附带WriteableBitmapEx)。