2012-10-18 191 views
3

我深化發展一個愚蠢的應用程序: - 打開從媒體庫, 一個圖片 - 放在一個網格單元所選擇的圖像(其中包含TextBlok元素) - 保存「保存的圖片」相冊中的圖片。WP7如何適應網格尺寸圖像尺寸

我的XAML代碼:

<controls:PanoramaItem Header="Image Selection" Height="652"> 
<Grid Name="markedImage" Margin="0,0,4,89"> 
    <Image x:Name="img" Stretch="Fill" Margin="0,0,0,10"></Image> 
    <TextBlock x:Name="text" Text="Hello!"> 
     <i:Interaction.Behaviors> 
      <el:MouseDragElementBehavior ConstrainToParentBounds="True"/> 
     </i:Interaction.Behaviors> 
    </TextBlock> 
</Grid> 

和代碼來打開和保存所選擇的圖片:

private void photoChooserTask_Completed(object sender, PhotoResult e) 
    { 
     try 
     { 
      BitmapImage image = new BitmapImage(); 
      image.SetSource(e.ChosenPhoto); 
      WriteableBitmap wbp = new WriteableBitmap(image); 
      this.img.Source = image; 
      height = image.PixelHeight; 
      width = image.PixelWidth; 
      MessageBox.Show("H: " + height + "\t" + "W: " + width); 
     } 
     catch 
     { 
      MessageBox.Show("Disconnect your device from Zune"); 
     } 
    } 
private void save_Click(object sender, System.EventArgs e) 
    { 

     WriteableBitmap marked = new WriteableBitmap(this.markedImage, null); 
     ThreadPool.QueueUserWorkItem(callback => 
     { 
      MemoryStream ms = new MemoryStream(); 
      marked.SaveJpeg(ms, (width * 2), (height * 2), 0, 100); 
      using (MediaLibrary lib = new MediaLibrary()) 
       lib.SavePicture("Test", ms.ToArray()); 
     }); 
     MessageBox.Show("H: " + marked.PixelHeight + "\t" + "W: " + marked.PixelWidth); 

     // wbm.SaveToMediaLibrary("SavedPicture.jpg", true); 

     MessageBox.Show("Picture saved successfully"); 
    } 

,因爲我是我不能發佈圖片新用戶,無論如何圖片(orignal和保存的圖片)具有相同的高度和寬度,但他們看起來不同 我認爲問題是網格尺寸和Stretch屬性。我嘗試了不同的組合,但結果並不好。 一些建議?

編輯:我賺了點聲望值 原始圖片是 original pics

保存的圖片被 saved pics

,如果你在另一個窗口同時打開,就可以看到區別

回答

1

的問題是相當簡單易懂:基本上,您正在對網格進行「截圖」。因此,生成的圖片的大小與網格的大小相同。因此下采樣的圖片。

修復,但可能會很棘手。我可以建議兩種方式:只執行前

  1. 重新創建編程網格,它在內容代碼隱藏,然後從這個新的網格

  2. 創建WriteableBitmap的將其從母公司控制電網的圖像生成代碼(那麼,網格就可以根據需要使用盡可能多的空間),並將其添加回算賬:

    <Grid x:Name="ParentGrid" Grid.Row="1" Width="200" Height="150"> 
        <Grid Name="markedImage" Margin="0,0,4,89"> 
         <Image x:Name="img" Source="Images/Test.jpg" Stretch="Fill" Margin="0,0,0,10"></Image> 
         <TextBlock x:Name="text" Text="Hello!"> 
          <i:Interaction.Behaviors> 
           <el:MouseDragElementBehavior ConstrainToParentBounds="True"/> 
          </i:Interaction.Behaviors> 
         </TextBlock> 
        </Grid> 
    </Grid> 
    

    和代碼隱藏:

    this.ParentGrid.Children.Remove(this.markedImage); 
    
    WriteableBitmap marked = new WriteableBitmap(this.markedImage, null);   
    
    MemoryStream ms = new MemoryStream(); 
    marked.SaveJpeg(ms, 1349, 1437, 0, 100); 
    using (MediaLibrary lib = new MediaLibrary()) 
        lib.SavePicture("Test", ms.ToArray()); 
    
    MessageBox.Show("H: " + marked.PixelHeight + "\t" + "W: " + marked.PixelWidth); 
    
    MessageBox.Show("Picture saved successfully"); 
    
    this.ParentGrid.Children.Add(this.markedImage); 
    
+0

它工作的很好,這樣你解決了我有關圖片質量和尺寸的問題。我只是適應它來解決其他問題:) – antedesk