C#WPF ImageControl未显示所选图像

时间:2015-05-31 13:01:22

标签: c# wpf image

我正在使用VS2013。 .NetFramework 3。我正在写一个像图库一样的简单项目。 我正在尝试将JPEG图像添加到Image Control中。首先,我将test.jpg图像添加为资源。然后我将一个图像控件添加到Window。在下一步中,我在“Source”属性中选择图像。图像显示为设计模式。一切都好。但是,当我运行项目时,没有任何显示。 我在谷歌和youtube搜索。我找到了一些解决方案,但我没有解决我的问题 (抱歉ENG) 这里是代码行

<Image Source="pack://siteoforigin:,,,/Resources/1.JPG" ... />

1 个答案:

答案 0 :(得分:1)

要在WPF应用中简单显示图像,请执行以下操作:

1)。在XAML中,添加:

<div class="container">
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/1/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/2/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/3/" />
    <h2 class="heading"> Lorem Ipsum is simply. </h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/4/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/5/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/6/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/7/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/8/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
      remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/9/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">Make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
      and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="slide">
    <img class="iImg" src="http://lorempixel.com/400/200/sports/2/" />
    <h2 class="heading"> Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h2> 
    <p class="desc">It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
</div>

2)。在Visual Studio IDE中,选择图像文件并检查其属性:

<Image Name="imgName" Source="/ProjAssemblyName;component/RelativePathToImageFile" />

注意:在大多数典型情况下,项目程序集名称与Default Namespace相同;在应用程序属性对话框中检查它有关您的案例的有用信息也包含在Microsoft参考中(如成员@Clemens所指出的):https://msdn.microsoft.com/en-us/library/aa970069%28v=vs.110%29.aspx#Resource_File_Pack_URIs___Local_Assembly。)。

另一种选择是将图像Build Action: Resources Copy to Output Directory: Do Not Copy 属性设置为Build Action,并以编程方式获取与图像文件对应的BitmapImage对象,如下所示:

EmbeddedResource

如果考虑进一步的图像处理,这种技术很有用。

希望这可能会有所帮助。亲切的问候,