2013-06-26 56 views
0

好日子全部, 我想做一個應用程序,以便我可以對照片做一些基本的編輯。我已經將一個集合綁定到一個列表框中,並且我可以獲取所有文本信息來填充,但無法顯示我的圖像。我看到的每個示例都使用uri設置圖像,但是,我沒有這樣做來創建圖像,我不確定這是爲什麼。我知道圖像顯示爲正確加載,因爲顯示了有關圖像的所有屬性(高度寬度像素)。我相信我刪除了與該問題無關的所有代碼。問題與C#wpf綁定圖像

由於提前, 克里斯

XML

<!--Step 1--> 
     <GroupBox x:Name="Step1"> 
      <Grid> 
       <StackPanel> 
        <Label>Select Pictures</Label> 
        <ListBox x:Name="PictureNames" ItemsSource="{Binding}" Height="auto"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <Image x:Name="CurrentPhoto" Width="300" Height="200" Source="{Binding CurrentPhoto}"/> 
            <!--old code trying to make this work <Canvas x:Name="CurrentPhoto" Width="300" Height="200" Background="{Binding CurrentPhoto}"/>--> 
            <TextBlock x:Name="Name" Text="{Binding Path=Name}"></TextBlock> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 

        </ListBox> 
        <Button x:Name="AddPicture" Click="AddPicture_Click">Click to upload Pictures</Button> 
       </StackPanel> 
      </Grid> 
     </GroupBox> 

CODE

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Microsoft.Win32; 
using System.Collections.ObjectModel; 

namespace S2IPictureWatermarker 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 

    ObservableCollection<picture> Pictures; 
    Position ThisStep = new Position(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Startup(); 

    } 

    private void Startup() 
    { 
     ThisStep.CurrentStep = 1; 
     Pictures = new ObservableCollection<picture>(); 
     Step1.Visibility = System.Windows.Visibility.Visible; 
     Step2.Visibility = System.Windows.Visibility.Collapsed; 
     Step3.Visibility = System.Windows.Visibility.Collapsed; 
     Step4.Visibility = System.Windows.Visibility.Collapsed; 
    } 

    //GroupEditLstBx.ItemsSource = SelectedPhotos 

    private void AddPicture_Click(object sender, RoutedEventArgs e) 
    {//add picture to list of pictures 
     //get photo(s) location 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     InitializeOpenFileDialog(openFileDialog1); 
     Nullable<bool> result = openFileDialog1.ShowDialog(); 
     //if photos are found, add to collection 
     if (result == true) 
      CreatePictureCollection(openFileDialog1.FileNames); 
    } 

    private void CreatePictureCollection(string[] FullPathNames) 
    { 
     foreach (string pathName in FullPathNames) 
     { 
      picture newPicture = new picture(); 
      newPicture.NewPicture(pathName); 
      if(Pictures.Count >= 1) 
      {//dont do anything 
      } 
      else 
      { 
       PictureNames.ItemsSource = Pictures; 
      } 
      Pictures.Add(newPicture); 

     } 
    } 

    private void InitializeOpenFileDialog(OpenFileDialog openFileDialog1) 
    { 
     // Set the file dialog to filter for graphics files. 
     openFileDialog1.Filter = 
      "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + 
      "All files (*.*)|*.*"; 

     // Allow the user to select multiple images. 
     openFileDialog1.Multiselect = true; 
     //     ^^^^^^^

     openFileDialog1.Title = "My Image Browser"; 

    } 
} 
} 

CODE FOR圖片類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.IO; 
using System.ComponentModel; 

namespace S2IPictureWatermarker 
{ 
    class picture : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private int height; 

     public int Height 
     { 
      get { return height; } 
      set { height = value; OnPropertyChanged("Height"); } 
     } 
     private int width; 

     public int Width 
     { 
      get { return width; } 
      set { width = value; OnPropertyChanged("Width"); } 
     } 

     private string type; 

     public string Type 
     { 
      get { return type; } 
      set { type = value; OnPropertyChanged("Type"); } 
     } 

     private string location; 

     public string Location 
     { 
      get { return location; } 
      set { location = value; OnPropertyChanged("Location"); } 
     } 

     private string name; 

     public string Name 
     { 
      get { return name; } 
      set { name = value; OnPropertyChanged("Name"); } 
     } 

     private int orgHeight; 

     public int OrgHeight 
     { 
      get { return orgHeight; } 
      set { orgHeight = value; OnPropertyChanged("OrgHeight"); } 
     } 

     private int orgWidth; 

     public int OrgWidth 
     { 
      get { return orgWidth; } 
      set { orgWidth = value; OnPropertyChanged("OrgWidth"); } 
     } 

     private Image currentPhoto; 

     public Image CurrentPhoto 
     { 
      get { return currentPhoto; } 
      set { currentPhoto = value; OnPropertyChanged("CurrentPhoto"); } 
     } 

     //methods 
     //NEW PICTURE 
     public bool NewPicture(string PictureFullPath) 
     { 
      bool Created = false; 
      try 
      { 
       //set path(location), name, type 
       Location = Path.GetPathRoot(PictureFullPath);     
       Name= Path.GetFileNameWithoutExtension(PictureFullPath); 
       Type = Path.GetExtension(PictureFullPath); 


       //set current image 
       CurrentPhoto = Image.FromFile(PictureFullPath); 
       //set height and width 
       Height = CurrentPhoto.Height; 
       Width = CurrentPhoto.Width; 

       Created = true; 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
      return Created; 
     } 


     //create the OnPropertyChanged method to raise 
     protected void OnPropertyChanged(string changedName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(changedName)); 
      } 
     } 
    } 
} 

回答

0

我相信你不能把System.Windows.Image放在canvas元素的背景中。這是因爲System.Windows.Image是UIElement,不能用作筆刷。您可以從ImageSource(由Uri創建)ImageBrush並將其放入後臺。或者您可以創建ContentControl而不是您的canvas元素,並將演示者的圖像放入其中。

+0

我的錯,那一行應該讀我正在玩不同的物體,可以得到它的工作。我現在要嘗試內容控制路線,看看它是否可行。 – chris

+0

因此您需要演示者屬性中的[System.Windows.Media.ImageSource](http://msdn.microsoft.com/ru-ru/library/system.windows.media.imagesource.aspx)對象。或者使用ContentControl而不是Canvas。 – sedovav

+0

你可以[將文件轉換成Uri](http://stackoverflow.com/questions/1546419/convert-file-path-to-a-file-uri),如果它是問題的根源 – sedovav

0

嗨嘗試像這樣的畫布元素。

<Canvas.Background> 
        <ImageBrush ImageSource="{Binding CurrentPhoto}" Stretch="UniformToFill"></ImageBrush> 
</Canvas.Background> 

編輯: 如果不工作使你的財產「CurrentPhoto」到圖像刷。

+0

我在我的代碼中試過了,並且無法看到任何圖像。你能使它工作嗎? – chris

+0

您是否嘗試將圖像製作爲ImageBrush而不是使用圖像? – Tan

+0

真的必須使用畫布嗎?如果不嘗試使用圖像元素。但是如果你真的想使用畫布,你必須將屬性CurrentPhoto轉換爲一個圖像刷。 – Tan