안녕하세요, 사진을 편집 할 수 있도록 응용 프로그램을 만들려고합니다. 컬렉션을 목록 상자에 바인딩하고 모든 텍스트 정보를 채울 수는 있지만 내 이미지를 표시 할 수는 없습니다. 내가 보는 모든 예는 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 사진 클래스에 대한
,536,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));
}
}
}
}
내 잘못, 해당 줄을 읽어야합니다. 다른 개체를 가지고 놀고 있었는데 그것을 작동시킬 수 있습니다. contentcontrol 라우트를 사용해 보도록하겠습니다. –
chris
이므로 [System.Windows.Media.ImageSource] (http://msdn.microsoft.com/ru-ru/library/system.windows.media.imagesource.aspx) 개체가 발표자의 속성에 필요합니다. 또는 Canvas 대신 ContentControl을 사용하십시오. – sedovav
문제의 근원 인 경우 [파일 pathes를 Uri로 변환] (http://stackoverflow.com/questions/1546419/convert-file-path-to-a-file-uri), 문제의 근원 인 경우 – sedovav