在WPF中渲染图像的问题 [英] Problem in Rendering Images in WPF

查看:57
本文介绍了在WPF中渲染图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态加载图片。但是我在加载图像时遇到问题



这是输出。 http://i57.tinypic.com/v7awye.jpg [ ^ ]



XAML代码是......



 <   Window     x:Class   = < span class =code-keyword> WpfApplication3.MainWindow  

xmlns = HTTP:// SC hemas.microsoft.com/winfx/2006/xaml/presentation\"

xmlns:x = < span class =code-keyword> http://schemas.microsoft.com/winfx/2006/xaml

< span class =code-attribute> 标题 = MainWindow 高度 = 350 宽度 = 525 >
< 网格 >
< < span class =code-leadattribute> Grid.RowDefinitions >
< RowDefinition 高度 = 28 / >
< RowDefinition 高度 = 自动 / >
< / Grid.RowDefinitions > ;
< 网格 Grid.Row = 0 >
< 网格.ColumnDefinitions >
< ColumnDefinition 宽度 = 自动 / >
< ColumnDefinition 宽度 = 90 / >
< / Grid.ColumnDefinitions >
< TextBox Grid.Column = 0 名称 = txtFolderPath MinWidth = 120 保证金 = 5 文字 = E:\\QueleaTest / >
< 按钮 网格.Column = 1 名称 = btnLoadFolderPath < span class =code-attribute> 内容 = 加载 MinWidth = 80 保证金 = 5 点击 = btnLoadFolderPath_Click / >
< ; / Grid >
< ListView Grid.Row = 1 < span class =code-attribute>保证金 = 0,0.2,-0.4,-242.4 名称 = < span class =code-keyword> lstView ScrollViewer.Horizo​​ntalScrollBarVisibility = 已禁用 ScrollViewer.VerticalScrollBarVisibility = 可见 >
< ListView.ItemsPanel >
< ItemsPanelTemplate >
< WrapPanel 方向 = 水平 / >
< / ItemsPanelTemplate >
< /名单View.ItemsPanel >
< ListView.ItemTemplate >
< DataTemplate >
< 图像 宽度 = 100 高度 = 100 / >
< < span class =code-leadattribute> / DataTemplate >
< / ListView .ItemTemplate >
< / ListView >
< / Grid >
< / Window >





C#代码是..



列表<图像> lstImages =  new 列表< Image>(); 

private void btnLoadFolderPath_Click( object sender,RoutedEventArgs e)
{
Image imgTemp;
List< string> lstFileNames = new 列表< string>(System.IO.Directory.EnumerateFiles( @ E:\Quelea \ images *。png< /跨度>));
foreach 字符串 fileName in lstFileNames)
{
imgTemp = new Image();
imgTemp.Source = new BitmapImage( new Uri(fileName));
imgTemp.Height = imgTemp.Width = 100 ;
lstImages.Add(imgTemp);
}
lstView.ItemsSource = lstImages;
}





图像在那里,但没有渲染..

我在做什么错误?



请注意,images文件夹可以是文件系统中的任何文件夹,并且在运行时选择了图像文件夹路径。

解决方案

尝试以下内容:



 imgTemp.Source =  new  BitmapImage( new  Uri(fileName,UriKind.Absolute)); 





而不是

 imgTemp.Source = new BitmapImage(new Uri(fileName)); 


您不是想要使用您创建的图像对象的事件。



它们与您通过XAML规定的图像无关。如果你真的想这样做,你应该给你的名字(通过向你的XAML添加XML属性 Name ),这样你就可以引用那些通过XAML生成的对象,通过命名引用在代码中使用它们。这样,您可以通过此属性将* .PNG文件用作URL: https://msdn.microsoft.com/en-us/library/system.windows.controls.image.baseuri%28v=vs.110%29.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.windows.controls.image.baseuri%28v=vs.110%29.aspx\"target =_ blanktitle =新窗口> ^ ]。



这只是建议你如何以你想象的方式快速修复你的应用程序,但几乎没有建议如何你以合理的方式发展它。你一般做坏事。表单示例,您硬编码图像大小(100),更糟糕的是,您在XAML和后面的代码中单独执行。它怎么可能成为可支持的?如果您的网址包含一些不同尺寸的图片,该怎么办?等等......所以,如果你解释了你真正希望实现的目标,可以给出确切的建议。



-SA

I am loading images dynamically. But I am getting problem in loading the images

Here is the output. http://i57.tinypic.com/v7awye.jpg[^]

XAML code is...

<Window x:Class="WpfApplication3.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="28"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="90"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Name="txtFolderPath" MinWidth="120" Margin="5" Text="E:\\QueleaTest" />
            <Button Grid.Column="1" Name="btnLoadFolderPath" Content="Load" MinWidth="80" Margin="5" Click="btnLoadFolderPath_Click"/>
        </Grid>
        <ListView Grid.Row="1" Margin="0,0.2,-0.4,-242.4" Name="lstView" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Image Width="100" Height="100" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>



C# code is..

List<Image> lstImages = new List<Image>();

        private void btnLoadFolderPath_Click(object sender, RoutedEventArgs e)
        {
            Image imgTemp;
            List<string> lstFileNames = new List<string>(System.IO.Directory.EnumerateFiles(@"E:\Quelea\images", "*.png"));
            foreach (string fileName in lstFileNames)
            {
                imgTemp = new Image();
                imgTemp.Source = new BitmapImage(new Uri(fileName));
                imgTemp.Height = imgTemp.Width = 100;
                lstImages.Add(imgTemp);
            }
            lstView.ItemsSource = lstImages;
        }



Images are there, but are not rendering..
Where m I doing wrong?

Please note, the images folder can be any on the filesystem and the image folder path is selected at runtime.

解决方案

try following:

imgTemp.Source = new BitmapImage(new Uri(fileName,UriKind.Absolute));



instead of

imgTemp.Source = new BitmapImage(new Uri(fileName));


You are not event trying to use the image objects you create.

They are not related to the images you prescribe via your XAML. If you really want to do it this way, you should give the names (by adding XML attributes Name to your XAML, so you could have reference to those objects generated via XAML, to use them in your code, through the named references). This way, you could use your *.PNG files as URLs via this property: https://msdn.microsoft.com/en-us/library/system.windows.controls.image.baseuri%28v=vs.110%29.aspx[^].

This is just the advice how could your quickly fix your application the way you envisioned it, but hardly and advice on how you develop it in a reasonable way. You generally do bad things. Form example, you hard-code image size (100), worse, you do it separately in XAML and code behind. How it possibly could be made supportable? And what if your URLs contain images of some different sizes? And so on… So, exact advice could be given if you explained what you really want to achieve with all that.

—SA


这篇关于在WPF中渲染图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆