简体   繁体   English

为什么 XamlIslands Image 控件不显示图像?

[英]Why does the XamlIslands Image control not display the image?

I am trying to let the user pick a file, and then display the file's icon on the XamlIslands GridView .我试图让用户选择一个文件,然后在 XamlIslands GridView上显示文件的图标。 After the user picks a file (the filepath of the file is myfile ), the icon of the file is then saved to C:\LauncherX\Temp\Icons.用户选择一个文件后(文件的文件路径为myfile ),该文件的图标随后保存到 C:\LauncherX\Temp\Icons。 The icon saves fine as I can open it and view it, however, when I try to display it in a XamlIsland Image control, it only displays certain images.该图标保存得很好,因为我可以打开它并查看它,但是,当我尝试在 XamlIsland Image控件中显示它时,它只显示某些图像。 For example, here are the two icons I am trying to display:例如,这是我要显示的两个图标:

This icon is called Github Desktop.png这个图标叫做Github Desktop.png

and

在此处输入图像描述

This icon is called TextIcon.png这个图标叫做TextIcon.png

The Xaml Islands Image Control displays Github Desktop.png perfectly fine: Xaml Islands Image Control 完美显示Github Desktop.png

在此处输入图像描述

However, for whatever reason, it does NOT display TextIcon.png但是,无论出于何种原因,它都不会显示TextIcon.png

在此处输入图像描述

Here is the C# code:这是 C# 代码:

private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

            //init a gridview
            var gridView = gridviewhost.Child as Windows.UI.Xaml.Controls.GridView;

            //init open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog() { DereferenceLinks = false };



            //check if openfile dialog is ok
            if (openFileDialog.ShowDialog() == true)
            {

                //and then get the icon and put it in into the grid view
                foreach (string myfile in openFileDialog.FileNames)
                {
                    //init filename
                    var filename = System.IO.Path.GetFileName(myfile);
                    filename = filename.Remove(filename.Length - 4);
                    filename = filename + ".png";

                    //save file icon
                    if(File.Exists(Path.Combine("C:\\LauncherX\\Temp\\Icons", filename)))
                    {
                        filename = count.ToString() + filename;

                        FileStream stream = new FileStream(System.IO.Path.Combine("C:\\LauncherX\\Temp\\Icons\\", filename), FileMode.Create);
                        Bitmap icon1 = new Bitmap(System.Drawing.Icon.ExtractAssociatedIcon(myfile).ToBitmap());
                        icon1.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        count += 1;

                    }
                    else
                    {
                        FileStream stream = new FileStream(System.IO.Path.Combine("C:\\LauncherX\\Temp\\Icons\\", filename), FileMode.Create);
                        Bitmap icon1 = new Bitmap(System.Drawing.Icon.ExtractAssociatedIcon(myfile).ToBitmap());
                        icon1.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    }

                    //create a stackpanel
                    Windows.UI.Xaml.Controls.StackPanel stackpanel = new Windows.UI.Xaml.Controls.StackPanel();
                    stackpanel.Width = 85;
                    stackpanel.Height = 85;

                    //load file icon into uwp image control
                    Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
                    image.Width = 50;
                    image.Height = 50;
                    string path = Path.Combine(@"C:\LauncherX\Temp\Icons\" + filename);
                    Uri fileuri = new Uri(path);                    
                    image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(fileuri);
                    image.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
                    image.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;

                    //create a textblock
                    //TODO: fix the text
                    Windows.UI.Xaml.Controls.TextBlock textblock = new Windows.UI.Xaml.Controls.TextBlock();
                    textblock.FontSize = 11;
                    textblock.TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap;
                    textblock.Text = filename.Remove(filename.Length - 4);
                    textblock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
                    textblock.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom;
                    textblock.TextAlignment = Windows.UI.Xaml.TextAlignment.Center;

                    //add the controls
                    stackpanel.Children.Add(image);
                    stackpanel.Children.Add(textblock);
                    gridView.Items.Add(stackpanel);

                    //TODO: Save the item using text documents

                }
            }
        }

And here is the Xaml Code:这是 Xaml 代码:

    <Grid>
        <xamlHost:WindowsXamlHost x:Name="OpenFileHost" InitialTypeName="Windows.UI.Xaml.Controls.Button" Margin="0,10,10,0" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" RenderTransformOrigin="0.5,0.5" Width="105" ChildChanged="OpenFileHost_ChildChanged"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="My applications" VerticalAlignment="Top" FontWeight="Bold" FontSize="16"/>
        <xamlHost:WindowsXamlHost Margin="10,47,10,10.5" x:Name="gridviewhost" InitialTypeName="Windows.UI.Xaml.Controls.GridView"/>
    </Grid>

Can you help me?你能帮助我吗?

Thanks谢谢

Ok, turns out setting the extension to .tiff seems to work.好的,结果将扩展名设置为.tiff似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM