Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Converter PNG to ICO - Form1.vb

Form1.vb

Caricato da: R0gerblack
Scarica il programma completo

  1. 
  2. Imports System.Drawing.Imaging
  3. Public Class Form1
  4.     Dim pngFileName As String '"C:\Painting\PNG.png"
  5.     Dim srcFile As Image
  6.     Dim targetImage As System.Drawing.Bitmap = New Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
  7.     Dim iconFile As Icon = Icon.FromHandle(targetImage.GetHicon())
  8.     Dim SaveImage As New SaveFileDialog
  9.  
  10.     Private Sub Resetta_Variabili()
  11.         For Each Processo In GetObject("winmgmts:").InstancesOf("win32_process")
  12.             If Processo.Name = "conime.exe" Then Processo.Terminate()
  13.         Next
  14.         RichTextBox1.Clear()
  15.         RichTextBox2.Clear()
  16.         PictureBox1.BackgroundImage = Nothing
  17.         SLabel1.Text = "Pronto."
  18.     End Sub
  19.  
  20.     Private Sub Apri()
  21.         Try
  22.             Dim OpenImage As New OpenFileDialog
  23.             OpenImage.Filter = "Image png|*.png"
  24.             If OpenImage.ShowDialog = Windows.Forms.DialogResult.OK Then
  25.                 pngFileName = OpenImage.FileName
  26.                 PictureBox1.BackgroundImage = Image.FromFile(pngFileName)
  27.                 RichTextBox1.Text = pngFileName
  28.             End If
  29.         Catch ex As AccessViolationException
  30.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  31.             SLabel1.Text = "Accesso Negato."
  32.         Catch ex As OutOfMemoryException
  33.             MsgBox("Memoria insufficente", MsgBoxStyle.Critical)
  34.             SLabel1.Text = "Memoria insufficiente."
  35.         End Try
  36.     End Sub
  37.  
  38.     Private Sub Seleziona_Locazione()
  39.         Try
  40.             SaveImage.Filter = "Icon File|*.ico"
  41.             If SaveImage.ShowDialog = Windows.Forms.DialogResult.OK Then
  42.                 RichTextBox2.Text = SaveImage.FileName
  43.             End If
  44.         Catch ex As AccessViolationException
  45.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  46.             SLabel1.Text = "Accesso Negato."
  47.         Catch ex2 As NullReferenceException
  48.             MsgBox("Errore nel salvataggio dell'immagine", MsgBoxStyle.Critical)
  49.             SLabel1.Text = "Errore nel salvataggio dell'immagine."
  50.         End Try
  51.     End Sub
  52.  
  53.     Private Sub Salva_Con_Nome()
  54.         Try
  55.             SaveImage.Filter = "Icon File|*.ico"
  56.             If SaveImage.ShowDialog = Windows.Forms.DialogResult.OK Then
  57.                 srcFile = PictureBox1.BackgroundImage
  58.                 'Dim gr As Graphics = Graphics.FromImage(srcFile)
  59.                 'gr.DrawImage(srcFile, New Rectangle(0, 0, 100, 100), New Rectangle(0, 0, srcFile.Width, srcFile.Height), GraphicsUnit.Pixel)
  60.                 Dim streamwriter As New IO.StreamWriter(SaveImage.FileName)
  61.                 targetImage = srcFile
  62.                 iconFile = Icon.FromHandle(targetImage.GetHicon())
  63.                 iconFile.Save(streamwriter.BaseStream)
  64.                 'srcFile.Save(RichTextBox2.Text, Imaging.ImageFormat.Icon)
  65.                 streamwriter.Close()
  66.                 RichTextBox2.Text = SaveImage.FileName
  67.                 SLabel1.Text = "Immagine salvata correttamente in " & RichTextBox2.Text
  68.             End If
  69.         Catch ex As AccessViolationException
  70.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  71.             SLabel1.Text = "Accesso Negato."
  72.         Catch ex2 As NullReferenceException
  73.             MsgBox("Errore nel salvataggio dell'immagine", MsgBoxStyle.Critical)
  74.             SLabel1.Text = "Errore nel salvataggio dell'immagine."
  75.         End Try
  76.     End Sub
  77.  
  78.     Private Sub Salva()
  79.         Try
  80.             If Not RichTextBox2.Text = "" Then
  81.                 srcFile = PictureBox1.BackgroundImage
  82.                 'Dim gr As Graphics = Graphics.FromImage(srcFile)
  83.                 'gr.DrawImage(srcFile, New Rectangle(0, 0, 100, 100), New Rectangle(0, 0, srcFile.Width, srcFile.Height), GraphicsUnit.Pixel)
  84.                 Dim streamwriter As New IO.StreamWriter(SaveImage.FileName)
  85.                 targetImage = srcFile
  86.                 iconFile = Icon.FromHandle(targetImage.GetHicon())
  87.                 iconFile.Save(streamwriter.BaseStream)
  88.                 'srcFile.Save(RichTextBox2.Text, Imaging.ImageFormat.Icon)
  89.                 streamwriter.Close()
  90.                 RichTextBox2.Text = SaveImage.FileName
  91.                 SLabel1.Text = "Immagine salvata correttamente in " & RichTextBox2.Text
  92.             Else
  93.                 Salva_Con_Nome()
  94.             End If
  95.         Catch ex As System.Runtime.InteropServices.ExternalException
  96.             MsgBox("Errore GDI+", MsgBoxStyle.Critical)
  97.             SLabel1.Text = "Errore GDI+."
  98.         Catch ex As NullReferenceException
  99.             MsgBox("Errore nel salvataggio dell'immagine", MsgBoxStyle.Critical)
  100.             SLabel1.Text = "Errore nel salvataggio dell'immagine."
  101.         Catch ex As AccessViolationException
  102.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  103.             SLabel1.Text = "Accesso Negato."
  104.         End Try
  105.     End Sub
  106.  
  107.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  108.         Me.Size = New Size(297, 220)
  109.         For Each Processo In GetObject("winmgmts:").InstancesOf("win32_process")
  110.             If Processo.Name = "conime.exe" Then Processo.Terminate()
  111.         Next
  112.     End Sub
  113.  
  114.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  115.         Apri()
  116.     End Sub
  117.  
  118.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  119.         Seleziona_Locazione()
  120.     End Sub
  121.  
  122.     Private Sub EsciToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EsciToolStripMenuItem.Click
  123.         Me.Close()
  124.     End Sub
  125.  
  126.     Private Sub NuovoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuovoToolStripMenuItem.Click
  127.         Resetta_Variabili()
  128.     End Sub
  129.  
  130.     Private Sub ApriToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApriToolStripMenuItem.Click
  131.         Apri()
  132.     End Sub
  133.  
  134.     Private Sub SalvaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalvaToolStripMenuItem.Click
  135.         Salva()
  136.     End Sub
  137.  
  138.     Private Sub RicaricaImmagineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RicaricaImmagineToolStripMenuItem.Click
  139.         Try
  140.             For Each Processo In GetObject("winmgmts:").InstancesOf("win32_process")
  141.                 If Processo.Name = "conime.exe" Then Processo.Terminate()
  142.             Next
  143.             PictureBox1.BackgroundImage = Image.FromFile(RichTextBox1.Text)
  144.             SLabel1.Text = "Fatto, immagine ricaricata!."
  145.         Catch ex As AccessViolationException
  146.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  147.             SLabel1.Text = "Accesso negato."
  148.         Catch ex2 As ArgumentException
  149.             MsgBox("Impossibile caricare l'immagine nel percorso specificato", MsgBoxStyle.Critical)
  150.             SLabel1.Text = "Errore nel caricamento immagine."
  151.         End Try
  152.     End Sub
  153.  
  154.     Private Sub EditaImmagineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditaImmagineToolStripMenuItem.Click
  155.         Try
  156.             MsgBox("Dopo aver editato l'immagine, premere su ""Ricarica immagine"", se si modifica il nome, invece sarà necessario andare su File -> Apri", MsgBoxStyle.Information)
  157.             Process.Start(RichTextBox1.Text)
  158.             SLabel1.Text = "Fatto."
  159.         Catch ex As InvalidOperationException
  160.             SLabel1.Text = "Errore nel ricaricamento immagine."
  161.             MsgBox("Impossibile caricare l'immagine nel percorso specificato", MsgBoxStyle.Critical)
  162.         End Try
  163.     End Sub
  164.  
  165.     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
  166.         Resetta_Variabili()
  167.     End Sub
  168.  
  169.     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
  170.         Apri()
  171.     End Sub
  172.  
  173.     Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
  174.         Salva_Con_Nome()
  175.     End Sub
  176.  
  177.     Private Sub SalvaConNomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalvaConNomeToolStripMenuItem.Click
  178.         Salva_Con_Nome()
  179.     End Sub
  180.  
  181.     Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
  182.         Salva()
  183.     End Sub
  184.  
  185.     Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
  186.         Try
  187.             MsgBox("Dopo aver editato l'immagine, premere su ""Ricarica immagine"", se si modifica il nome, invece sarà necessario andare su File -> Apri", MsgBoxStyle.Information)
  188.             Process.Start(RichTextBox1.Text)
  189.             SLabel1.Text = "Fatto."
  190.         Catch ex As InvalidOperationException
  191.             SLabel1.Text = "Errore nel ricaricamento immagine."
  192.             MsgBox("Impossibile caricare l'immagine nel  percorso specificato", MsgBoxStyle.Critical)
  193.         End Try
  194.     End Sub
  195.  
  196.     Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
  197.         Try
  198.             For Each Processo In GetObject("winmgmts:").InstancesOf("win32_process")
  199.                 If Processo.Name = "conime.exe" Then Processo.Terminate()
  200.             Next
  201.             PictureBox1.BackgroundImage = Image.FromFile(RichTextBox1.Text)
  202.             SLabel1.Text = "Fatto, immagine ricaricata!."
  203.         Catch ex As AccessViolationException
  204.             MsgBox("Accesso negato", MsgBoxStyle.Critical)
  205.             SLabel1.Text = "Accesso negato."
  206.         Catch ex2 As ArgumentException
  207.             MsgBox("Impossibile caricare l'immagine nel percorso specificato", MsgBoxStyle.Critical)
  208.             SLabel1.Text = "Errore nel caricamento immagine."
  209.         End Try
  210.     End Sub
  211.  
  212.     Private Sub BugReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BugReportToolStripMenuItem.Click
  213.         BugReport.ShowDialog()
  214.     End Sub
  215.  
  216.     Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
  217.         BugReport.ShowDialog()
  218.     End Sub
  219.  
  220.     Private Sub InformazioniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InformazioniToolStripMenuItem.Click
  221.         Informazioni.ShowDialog()
  222.     End Sub
  223.  
  224.     Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
  225.         Informazioni.ShowDialog()
  226.     End Sub
  227.  
  228.     Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
  229.         Me.Close()
  230.     End Sub
  231. End Class