Copier une icône des Ressources vers l'ordinateur

Résolu
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 - 10 nov. 2016 à 15:45
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 - 21 nov. 2016 à 13:22
Bonjour,
Dans le dernier poste que j'ai posté " Vb.net Créer un Raccourci de Fichier ou Dossier ", il y avait beaucoups de propositions, j'ai décidé de copier l'icône qui se trouve dans les Ressources vers l'ordinateur, après l'utiliser comme icône pour le raccourci
Pour là copier j'ai utilisé tout d'abord le code suivant (je l'utilise souvent pour copier des fichiers des Ressources vers l'ordinateur) :
System.IO.File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), My.Resources.icone1)

Mais cela ne fonction pas
J'ai fais des recherche est j'ai fini par trouver le code suivant :
My.Resources.icone1.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\icone1.ico")

L'icône est bien copier dans l'ordinateur, mais elle est inutilisable (je ne peux pas l'utiliser comme icône pour le raccourci)
Aidez moi s'il vous plaît à régler ce problème
Merci d'avance pour votre aide


5 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
Modifié par cs_Le Pivert le 10/11/2016 à 18:40
Bonjour,

Comme ceci:

My.Resources.icone1.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\icone1.ico", System.Drawing.Imaging.ImageFormat.Icon)


Sinon une autre manière:

 Dim path As String = Application.StartupPath
        path = Replace(path, "bin\Debug", "Resources")
        path = path & "\icone1.ico"
        FileCopy(path, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\icone1.ico")


@+ Le Pivert
0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1
10 nov. 2016 à 20:01
Salut cs_Le Pivert
merci de m'avoir répondu aussi rapidement
1er mot : pour la deuxieme méthode :
Dim path As String = Application.StartupPath
path = Replace(path, "bin\Debug", "Resources")
path = path & "\icone1.ico"
FileCopy(path, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\icone1.ico")

elle ne sera pas très utile car après avoir généré le exe le chemin de bin,Debug ou Resources ne sera plus disponible (seul l'application exe sera déplacé et utilisé dans d'autre ordinateur)
2eme mot : concernant la première méthode :
My.Resources.icone1.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\icone1.ico", System.Drawing.Imaging.ImageFormat.Icon)

il y a deux méthode pour ajouter une icône au Resources :
1. ajouter la ressource en tant qu'image (dans la liste des images)
c'est bien possible de copier l'icône, mais toujours impossible de l'utiliser et il y a une différence entre l'icône originale et l'icône copier :

2. Ajouter l'icône en tant qu'icône (dans la liste des icônes)
le code ne fonction pas, il y a une erreur qui s'affiche pour le chemin ou l'icône sera copier ("Impssible de convertir une valeur de type 'String' en 'System.IO.Stream'.")
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
11 nov. 2016 à 08:06
Si je comprends bien tu veux simplement extraire une icone d'un executable?

Voir ceci:

http://codes-sources.commentcamarche.net/source/46960-extraire-tous-les-icones-d-un-executable-ou-d-une-dll

0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1
11 nov. 2016 à 16:43
Salut cs_Le Pivert,
merci pour le lien, l'application est superbe (j'ai cherché il y a pas longtemps le moyen d'extraire les icônes d'un dll), mais ce n'est pas ce que je veux faire pour le projet actuel
j'ai imaginé une solution (ou on peut dire que j'ai fait un petit détour), l'idée que j'ai imaginé fonction très bien :
1er mot : je copie l'image png de My.Ressources dans l'ordinateur
2 eme mot : je converti l'image png en icône (*.ico)
je ne suis pas sûr, mais je crois que tu a utilisé dans l'un de tes projets des fonctions pour convertir des images, l'idée m'est venue quand je me suis souvenue de ça
3 eme mot : comme je n'ai plus besoin de l'image png je la supprime définitivement
4 eme mot : je choisi l'icône converti comme icône pour le raccourci
j'ai décidé de mettre la 1er et 2 eme étape dans le Dossier Windows en donnent à mon application des droit d'administrateur
Voici le code
Imports System.IO
Public Class Form1
#Region "Raccourci"
Private Function CreerRaccourciAvecIcone(ByVal TargetName As String, ByVal ShortCutPath As String, ByVal ShortCutName As String, ByVal Icones As String) As Boolean
Dim oShell As Object
Dim Raccourcis As Object
Try
oShell = CreateObject("WScript.Shell")
Raccourcis = oShell.CreateShortcut(ShortCutPath & "\" & ShortCutName & ".lnk")
Raccourcis.TargetPath = TargetName
Raccourcis.WindowStyle = 1
Raccourcis.IconLocation = Icones
Raccourcis.Save()
Catch ex As Exception
End Try
End Function
#End Region
#Region "Convertire"
Public Shared Function Convert(ByVal Entrées As System.IO.Stream, ByVal Sortie As System.IO.Stream, ByVal Size As Integer, Optional ByVal Conserver_les_proportions As Boolean = False) As Boolean
Dim Bit_entrée As System.Drawing.Bitmap = DirectCast(System.Drawing.Bitmap.FromStream(Entrées), System.Drawing.Bitmap)
If Bit_entrée IsNot Nothing Then
Dim Width As Integer, Height As Integer
If Conserver_les_proportions Then
Width = Size
Height = Bit_entrée.Height / Bit_entrée.Width * Size
Else
Width = InlineAssignHelper(Height, Size)
End If
Dim Nouveau_bit As New System.Drawing.Bitmap(Bit_entrée, New System.Drawing.Size(Width, Height))
If Nouveau_bit IsNot Nothing Then
Dim Données As New System.IO.MemoryStream()
Nouveau_bit.Save(Données, System.Drawing.Imaging.ImageFormat.Png)
Dim Créer_icone As New System.IO.BinaryWriter(Sortie)
If Sortie IsNot Nothing AndAlso Créer_icone IsNot Nothing Then
' 0-1 reserved, 0
Créer_icone.Write(CByte(0))
Créer_icone.Write(CByte(0))
' 2-3 image type, 1 = icon, 2 = cursor
Créer_icone.Write(CShort(1))
' 4-5 number of images
Créer_icone.Write(CShort(1))
' image entry 1
' 0 image width
Créer_icone.Write(CByte(Width))
' 1 image height
Créer_icone.Write(CByte(Height))
' 2 number of colors
Créer_icone.Write(CByte(0))
' 3 reserved
Créer_icone.Write(CByte(0))
' 4-5 color planes
Créer_icone.Write(CShort(0))
' 6-7 bits per pixel
Créer_icone.Write(CShort(32))
' 8-11 size of image data
Créer_icone.Write(CInt(Données.Length))
' 12-15 offset of image data
Créer_icone.Write(CInt(6 + 16))
' Écrire les données d'image
' Les données png doivent contenir le fichier des données png complet
Créer_icone.Write(Données.ToArray())
Créer_icone.Flush()
Return True
End If
End If
Return False
End If
Return False
End Function
Private Shared Function InlineAssignHelper(Of T)(ByRef Cible As T, ByVal Valeur As T) As T
Cible = Valeur
Return Valeur
End Function
Public Function Convert(ByVal Image_entrée As String, ByVal Icone_Sortie As String, ByVal Size As Integer, Optional ByVal conserver_les_proportions As Boolean = False) As Boolean
Dim Entrées As New System.IO.FileStream(Image_entrée, System.IO.FileMode.Open)
Dim Sortie As New System.IO.FileStream(Icone_Sortie, System.IO.FileMode.OpenOrCreate)
Dim result As Boolean = Convert(Entrées, Sortie, Size, conserver_les_proportions)
Entrées.Close()
Sortie.Close()
Return result
End Function
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Chemin_PNG As String = String.Empty
Dim Chemin_ICO As String = String.Empty
Chemin_PNG = Environment.GetFolderPath(Environment.SpecialFolder.Windows) & "\Sup.png"
Chemin_ICO = Environment.GetFolderPath(Environment.SpecialFolder.Windows) & "\Sup.ico"
My.Resources.Sup.Save(Chemin_PNG, System.Drawing.Imaging.ImageFormat.Png)
Dim Taille As Integer
Taille = 48
Convert(Chemin_PNG, Chemin_ICO, Taille, False)
My.Computer.FileSystem.DeleteFile(Chemin_PNG, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
CreerRaccourciAvecIcone("Appwiz.cpl", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Ajouter ou Supprimer un Programme", Chemin_ICO)
End Sub
End Class

au tout début je voulais les mettres dans le dossier Systeme32 (C:\Windows\System32), mais je n'ai pas pu copier le png dedans même avec les droit d'administrateur
concernant le logiciel que tu m'a suggéré j'ai remarqué qu'il ne récupère que les icônes avec une taille 32 x 32 pas plus, par exemple dans shell32.dll (‪C:\Windows\System32\shell32.dll) il y a des icônes de taille (32 x 32 ,48 x 48, 64 x 64 ... etc), mais comme je l'ai dit il ne récupère que les icônes avec une taille 32 x 32
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137 > kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018
11 nov. 2016 à 17:18
Pour que cela fonctionne il faut créer un bitmap en passant par une PictureBox comme ceci:

 Dim Chemin_PNG As String = String.Empty
        Dim Chemin_ICO As String = String.Empty
            Chemin_PNG = Environment.GetFolderPath(Environment.SpecialFolder.Windows) & "\Sup.png"
        Chemin_ICO = Environment.GetFolderPath(Environment.SpecialFolder.Windows) & "\Sup.ico"
    PictureBox1.Image = My.Resources.Sup.ToBitmap()
        PictureBox1.Image.Save(Chemin_PNG, System.Drawing.Imaging.ImageFormat.Png)
        Dim Taille As Integer
        Taille = 48
        Convert(Chemin_PNG, Chemin_ICO, Taille, False)
0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1 > cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024
11 nov. 2016 à 17:47
Le code que j'ai fourni fonction très bien normalement
et quand j'essaye d'utiliser ton code il affiche dans
PictureBox1.Image = My.Resources.Sup.ToBitmap()

l'erreur
'ToBitmap' n'est pas un membre de 'System.Drawing.Bitmap'
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137 > kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018
11 nov. 2016 à 18:05
Ton code ne fonctionne pas chez moi:
Un message d'erreur sur cette ligne:

 My.Resources.Shell32_024.Save(Chemin_PNG, System.Drawing.Imaging.ImageFormat.Png)


impossible de convertir une valeur de type String en System.IO.Stream

Ce n'est pas grave du moment que cela fonctionne chez toi.

Il y a certaines bizarreries!
0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1 > cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024
11 nov. 2016 à 18:32
Cette erreur s'afficher pour moi quand j'ai essayé d'enregistrer une icône dans l'ordinateur, je vais chercher une solution, je te tiendrai au courant
Merci beaucoup pour ton aide
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
21 nov. 2016 à 09:08
J'ai trouvé une solution pour mettre l'icône de l'executable sur le Bureau:

une ImageList et une Picturebox

 Dim oBitmap As Bitmap
        Dim HIcon As IntPtr
        Dim newIcon As Icon
        Dim oFileStream As IO.FileStream
        Dim ico As Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath) 'chemin de l'executable
        ImageList1.Images.Add(ico)
        PictureBox1.Image = ImageList1.Images(0)
        oBitmap = New Bitmap(PictureBox1.Image)
        'Set transparence color.
        oBitmap.MakeTransparent(Color.Transparent)
        oBitmap.SetResolution(96, 96)
        ' Get an Hicon for myBitmap.
        HIcon = oBitmap.GetHicon()
        ' Create a new icon from the handle.
        newIcon = System.Drawing.Icon.FromHandle(HIcon)
        ' Set the form Icon attribute to the new icon.
        Me.Icon = newIcon
        oFileStream = New IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Monicone.ico", IO.FileMode.CreateNew)
        newIcon.Save(oFileStream)
        oFileStream.Close()
        MessageBox.Show("Opération réussie.", "Extraire icône", MessageBoxButtons.OK, MessageBoxIcon.Information)


Cela fait quand même moins de manipulation !
0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1
21 nov. 2016 à 12:21
Bonjour cs_Le Pivert
C'est une bonne solution, mais es que en peut augmenter la taille de l'icône récupérer (16 X 16 trop petite)
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
21 nov. 2016 à 13:01
Bien sur, tu aurais trouvé la solution ici:

http://codes-sources.commentcamarche.net/source/101737-convertir-image-bmp-en-ico

   Dim HIcon As IntPtr
        Dim newIcon As Icon
        Dim oFileStream As IO.FileStream
        Dim ico As Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath) 'chemin de l'executable
        ImageList1.Images.Add(ico)
        PictureBox1.Image = ImageList1.Images(0)
        Dim imageSource As New Bitmap(PictureBox1.Image)
        'crée un Bitmap avec la nouvelle taille
        Dim bp As New Bitmap(32, 32)
        Dim gr As Graphics = Graphics.FromImage(bp)
        'copie l'image source dans la nouvelle image
        gr.DrawImage(imageSource, 0, 0, bp.Width + 1, bp.Height + 1)
        bp.MakeTransparent(Color.Transparent)
        bp.SetResolution(96, 96)
        ' Get an Hicon for myBitmap.
        HIcon = bp.GetHicon()
        ' Create a new icon from the handle.
        newIcon = System.Drawing.Icon.FromHandle(HIcon)
        ' Set the form Icon attribute to the new icon.
        Me.Icon = newIcon
        oFileStream = New IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Monicone.ico", IO.FileMode.CreateNew)
        newIcon.Save(oFileStream)
        oFileStream.Close()
        MessageBox.Show("Opération réussie.", "Extraire icône", MessageBoxButtons.OK, MessageBoxIcon.Information)


Et voilà
0
kikou93 Messages postés 417 Date d'inscription mardi 4 février 2014 Statut Membre Dernière intervention 24 septembre 2018 1
21 nov. 2016 à 13:22
Oui c'est vrais, J'ai déjà vue ton dernier logiciel, mais je n'ai pas lu le code en détail
Merci pour ton aide tu m'a beaucoup aidé
0
Rejoignez-nous