2013-06-28 92 views
0

在我的應用程序中有一個全尺寸的框架和一個尺寸較小的內部圖片。我希望內部的一個在框架內部和頂部。所以這是我做過什麼:如何合併兩個圖像並顯示在圖像控件中?

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

希望這個鏈接將引導你。請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; 
     } 
+0

謝謝@Amit。你知道,我的代碼也是基於這個例子,我讀過它。但沒有爲我的案件工作 – user2490629

+0

哦,但示例工作正常。你檢查了所有的行嗎? –

相關問題