0

I am trying to let the user pick a file, and then display the file's icon on the 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. 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. For example, here are the two icons I am trying to display:

This icon is called Github Desktop.png

and

enter image description here

This icon is called TextIcon.png

The Xaml IslandsImage Control displays Github Desktop.png perfectly fine:

enter image description here

However, for whatever reason, it does NOT display TextIcon.png

enter image description here

Here is the C# code:

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:

    <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

Apollo199999999
  • 186
  • 3
  • 12
  • be sure extension for all icons is `.png` and try comment out this line `filename = filename + ".png";` then check. – Hammas Apr 11 '20 at 10:14
  • @RaoHammasHussain If I am extracting an icon from a file (like a text file, or a shortcut file), how do I make sure that extensions for all the icons is .png? How do I do that? – Apollo199999999 Apr 11 '20 at 10:48
  • 1
    The filename should end in.png string.endswith(). Or path.getextension(). – Andy Apr 11 '20 at 12:00
  • @Andy When the user picks a file, (e.g. Text.txt), the filename gives me the value of "Text.txt" So, I need to add the .png at the end so that when I save the icon of this file (done via System.Drawing.Icon.ExtractAssosiatedIcon(myfile)), it will save it as a .png file. Look at the code for more details. – Apollo199999999 Apr 11 '20 at 12:08
  • but why would you save a `.txt` file with `.png` extension ? that's why check the extension before using `path.getextension()` or by just `Yourstring.endsWith()` or i guess `Yourstring.Contains()` – Hammas Apr 11 '20 at 13:10
  • In your case it might be happening like `name.png.png` or `name.jpg.png` or `namepng` etc you'll have to check that. – Hammas Apr 11 '20 at 13:12
  • https://stackoverflow.com/questions/2701263/get-the-icon-for-a-given-extension and https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-extract-the-icon-associated-with-a-file-in-windows-forms – Hammas Apr 11 '20 at 13:17
  • @RaoHammasHussain Do you understand what I am even trying to do – Apollo199999999 Apr 11 '20 at 14:03
  • yes i did ! linked i've shared are similar to your issue. If you check them. – Hammas Apr 11 '20 at 15:07
  • @RaoHammasHussain both websites mentioned Icon.ExtractAssosiatedIcon() which is what I have already done. Anyways, I don’t think that extracting the icon from the file and saving it is the problem, as I can open and view the icons with no problem at all after it has been saved. – Apollo199999999 Apr 12 '20 at 05:19
  • Go to saving location and check if icons have correct extension. :) – Hammas Apr 12 '20 at 05:52
  • @RaoHammasHussain Yes the icons have the correct extension https://i.imgur.com/mOGbNr1.jpg – Apollo199999999 Apr 12 '20 at 06:53
  • This extension is not right ! and this was i was telling you before. – Hammas Apr 12 '20 at 07:01
  • have only one extension like `abc.png` not this `abc.txt.png` or other type you have shown – Hammas Apr 12 '20 at 07:01
  • @RaoHammasHussain How can I remove the .txt? Do I just do filename.Remove(filename.Length - 4)? – Apollo199999999 Apr 12 '20 at 07:59
  • @RaoHammasHussain and anyways, that dosen't work https://imgur.com/vKJ28rb https://imgur.com/5Dlu3mt – Apollo199999999 Apr 12 '20 at 08:11
  • debug these two lines ` var filename = System.IO.Path.GetFileName(myfile); filename = filename + ".png";` – Hammas Apr 12 '20 at 08:26
  • have a break point here and check what comes in `filename` variable if it's full name like `abc.png` then you don't need this `filename = filename + ".png` second line. – Hammas Apr 12 '20 at 08:27
  • and i am right actually `Path.GetFileName Path.GetFileNameWithoutExtension` these are two dofferent methods .. what you are using already give you full name with extension like abc.png so you dont need to attach .png in next line. got it ? – Hammas Apr 12 '20 at 08:30
  • @RaoHammasHussain When I select a **text** file named Document using the openfiledialog, filename returns as Document.txt – Apollo199999999 Apr 12 '20 at 08:33
  • actually this should work directly `Icon ico = Icon.ExtractAssociatedIcon(@"d:\\1.txt"); pictureBox1.Image = ico.ToBitmap();` – Hammas Apr 12 '20 at 08:37
  • @RaoHammasHussain I am using WPF, and the image control I am using is from XAML Islands, which means that it is a UWP control. – Apollo199999999 Apr 12 '20 at 10:11

1 Answers1

0

Ok, turns out setting the extension to .tiff seems to work.

Apollo199999999
  • 186
  • 3
  • 12