如何合并两个图像并在图像控件中显示?

时间:2013-06-28 01:01:09

标签: c# windows-phone-8 windows-phone

我的应用程序中有一个全尺寸的框架和一个尺寸较小的内部图片。我希望内部的一个在框架内部和顶部。所以这就是我所做的:

var resourceStream = Application.GetResourceStream(new Uri("Assets/Frame.png", UriKind.Relative));
var frame = new BitmapImage();
frame.SetSource(resourceStream.Stream);

WriteableBitmap finalImage = new WriteableBitmap(frame);

using (MemoryStream memoryStream = new MemoryStream())
{
    BitmapImage innerImage = new BitmapImage(new Uri(string.Format("Assets/Pages/{0}.png", page), UriKind.Relative));
    Image image = new Image();
    image.Height = innerImage.PixelHeight;
    image.Width = innerImage.PixelWidth;
    image.Source = innerImage;

    TranslateTransform translateTransform = new TranslateTransform();
    translateTransform.X = 52;
    translateTransform.Y = 60;

    finalImage.Render(image, translateTransform);
    finalImage.Invalidate();

    finalImage.SaveJpeg(memoryStream, frame.PixelWidth, frame.PixelHeight, 0, 100);
    memoryStream.Seek(0, System.IO.SeekOrigin.Begin);

    PagePicture.Source = finalImage;
}

但我只看到没有内部图像的框架..我想这可能在框架后面。 我该如何解决这个问题?

或许我可以在没有合并的情况下一个接一个地控制两个控件?让我试试

1 个答案:

答案 0 :(得分:1)

希望此链接能够指导您。请visit here。 有关详细说明,请浏览MSDN SAmple。两者都相同,但你可以在这里得到描述。

现在试试这个

    using (MemoryStream mem = new MemoryStream())
        {
            //Inner Image
            BitmapImage innerImage = new BitmapImage();
            innerImage.SetSource(System.Windows.Application.GetResourceStream(new Uri("test.jpg", UriKind.Relative)).Stream);
            WriteableBitmap wb = new WriteableBitmap(innerImage);

            //Frame Images
            BitmapImage finalImage = new BitmapImage();
            finalImage.SetSource(System.Windows.Application.GetResourceStream(new Uri("White.jpg", UriKind.Relative)).Stream);
            WriteableBitmap wbFinal = new WriteableBitmap(finalImage);

            Image image = new Image();
            image.Height = innerImage.PixelHeight;
            image.Width = innerImage.PixelWidth;
            image.Source = innerImage;

            // TranslateTransform                      
            TranslateTransform tf = new TranslateTransform();
            tf.X = 52;
            tf.Y = 60;
            wbFinal.Render(image, tf);

            wbFinal.Invalidate();
            wbFinal.SaveJpeg(mem, wb.PixelWidth, wb.PixelHeight, 0, 100);
            mem.Seek(0, System.IO.SeekOrigin.Begin);

            // Show image.               
            PagePicture.Source = wbFinal;
        }