簡體   English   中英

為什么 XamlIslands Image 控件不顯示圖像?

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

我試圖讓用戶選擇一個文件,然后在 XamlIslands GridView上顯示文件的圖標。 用戶選擇一個文件后(文件的文件路徑為myfile ),該文件的圖標隨后保存到 C:\LauncherX\Temp\Icons。 該圖標保存得很好,因為我可以打開它並查看它,但是,當我嘗試在 XamlIsland Image控件中顯示它時,它只顯示某些圖像。 例如,這是我要顯示的兩個圖標:

這個圖標叫做Github Desktop.png

在此處輸入圖像描述

這個圖標叫做TextIcon.png

Xaml Islands Image Control 完美顯示Github Desktop.png

在此處輸入圖像描述

但是,無論出於何種原因,它都不會顯示TextIcon.png

在此處輸入圖像描述

這是 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

                }
            }
        }

這是 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>

你能幫助我嗎?

謝謝

好的,結果將擴展名設置為.tiff似乎有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM