如何在此向量中显示图像?

时间:2014-04-13 19:34:03

标签: c# wpf xaml

如何在此向量中显示图像?看看xaml和C#代码,看看我尝试过没有结果的东西。

            byte[] pngBytes = File.ReadAllBytes(@".\GalleryImages");
            List<WineModel> images = new List<WineModel>();
            images.Add(new WineModel(pngBytes, "this is a description"));

            var button = new KinectTileButton
            {
               Label = System.IO.Path.GetFileNameWithoutExtension(pngBytes),

               Background = images[0].image

           };

代码xaml:

<WrapPanel VerticalAlignment="Center" x:Name="wrapPanel" Orientation="Vertical" k:KinectTileButton.Click="KinectTileButtonClick">
            <!-- items for design layout.  They will be replaced at runtime. -->
            <k:KinectTileButton Label="1" Click="KinectTileButton_Click" />
            <k:KinectTileButton Label="2"/>
            <k:KinectTileButton Label="3"/>
            <k:KinectTileButton Label="4"/>
            <k:KinectTileButton Label="5" Click="KinectTileButton_Click_1" />
            <k:KinectTileButton Label="6"/>
            <k:KinectTileButton Label="7"/>
            <k:KinectTileButton Label="8"/>
            <k:KinectTileButton Label="9"/>
            <k:KinectTileButton Label="10"/>
            <Image Height="150" Name="image1" Stretch="Fill" Width="200" />
        </WrapPanel>

如果我必须在图像框中显示它们,代码看起来会像这样,但我不知道如何 为我的xaml代码调整它。

pictureBox1.Image = images[0].Image;

编辑:

  public class WineModel
{
    public WineModel(string[] bytes, string description)
    {
        this.Bytes = bytes;
        this.Description = description;
    }

    public string[] Bytes { get; set; }
    public string Description { get; set; }


}

1 个答案:

答案 0 :(得分:0)

有许多可能的解决方案来组合图像文件和描述。例如,您可以为每个图像文件(img.png)关联一个文本文件(img.png.txt)。 Personnaly我不喜欢这个解决方案,因为它取决于文件系统和文件名。

另一种方法是使用集成的.Net xml序列化功能。您所要做的就是创建一个带有一堆值的文件(filePath,desctiption)。

这样的事情:

<?xml version="1.0" encoding="utf-8" ?>
<Library>
  <Thumbs>
    <Thumb>
      <ImagePath>./Images/img1.png</ImagePath>
      <Description>Description for the image 1</Description>
    </Thumb>
    <Thumb>
      <ImagePath>./Images/img2.png</ImagePath>
      <Description>Description for the image 2</Description>
    </Thumb>
    <Thumb>
      <ImagePath>./Images/img3.png</ImagePath>
      <Description>Description for the image 3</Description>
    </Thumb>
  </Thumbs>
</Library>

然后定义相应的#class。注意类/属性名称应与xml文件中的相同:

public class Library
{
    public List<Thumb> Thumbs { get; set; }
}

public class Thumb
{
    public string ImagePath { get; set; }
    public string Description { get; set; }
}

现在您只需要调用XmlSerializer类来读取xml并自动填写库结构。

public Library DeserializeLibrary(string path)
{
    Library result;

    XmlSerializer mySerializer = new XmlSerializer(typeof(Library));

    // To read the file, create a FileStream.
    using (FileStream myFileStream = new FileStream(path, FileMode.Open))
    {
        // Call the Deserialize method and cast to the object type.
        result = (Library)mySerializer.Deserialize(myFileStream);

    }
    return result;
}

现在,当您调用DeserializeLibrary(“Library.xml”)时,它将返回用3个Thumb实例填充的Library类实例。

有关C#序列化的一些链接: http://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/fa420a9y(v=vs.90).aspx

PS:我明确避免将我的代码用于您的特定类名。我认为你已经完成了编写代码的所有工作。