VB.NET - Funzione che convertire un file immagine Bitmap in Icon (Codice)
Oggi vi voglio mettere a disposizione delle funzioni che trasformano un file immagine Bitmap in Icon .
ConvertiFileBmpToFileIco("C:\Documents and Settings\All Users\Documenti\Immagini\Immagini campione\Inverno.jpg")
CreateIcon("C:\Documents and Settings\All Users\Documenti\Immagini\Immagini campione\Inverno.jpg")
CreaUnaIcona("C:\Documents and Settings\All Users\Documenti\Immagini\Immagini campione\Inverno.jpg")
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET:
Private Sub ConvertiFileBmpToFileIco(ByVal FileBMP As String)
'Creo un oggetto di tipo Immagine Bitmap che contrrà l'immagine da convertire
Dim ImgBMP As New Bitmap(FileBMP)
'Prendo il nome del file cancello le ultime quattro caratteri .bmp e inserisco .ico
Dim FileICO As String = Strings.Left(FileBMP, Len(FileBMP) - 4) & ".ico"
' Creo un nuovo oggetto Bitmap con le dimensioni scelte / con la scala che voglio
Dim NewImgICO As New Bitmap(16, 16)
' Creo un oggetto Grafico e ci passo il nuovo oggetto Bitmap
Dim OggGraphics As Graphics = Graphics.FromImage(NewImgICO)
' Copy the source image into the destination bitmap.
OggGraphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
OggGraphics.DrawImage(ImgBMP, 0, 0, NewImgICO.Width, NewImgICO.Height)
NewImgICO.GetHicon()
NewImgICO.Save(FileICO, System.Drawing.Imaging.ImageFormat.Icon)
OggGraphics.Dispose()
NewImgICO.Dispose()
'Ora risalvo il file ICO
'ImgBMP.Save(FileICO, System.Drawing.Imaging.ImageFormat.Icon)
'Distruggo lo spazzio di memoria occupato in precedenza, Dealloco la memoria dell'oggetto ImgBTMP
ImgBMP.Dispose()
End Sub
Public Function MakeIcon(ByVal b As Bitmap, ByVal x As Size) As Icon
Dim ImgBMP As New Bitmap(x.Width, x.Height)
ImgBMP = b
Return Icon.FromHandle(b.GetHicon())
End Function
Private Sub CreaUnaIcona(ByVal FileBMP As String)
'Creo un oggetto di tipo Immagine Bitmap che contrrà l'immagine da convertire
Dim ImgBMP As New Bitmap(FileBMP)
'Prendo il nome del file cancello le ultime quattro caratteri .bmp e inserisco .ico
Dim FileICO As String = Strings.Left(FileBMP, Len(FileBMP) - 4) & ".ico"
' Creo un nuovo oggetto Bitmap con le dimensioni scelte / con la scala che voglio
Dim NewImgICO As New Bitmap(16, 16)
Dim fs As New IO.FileStream(FileICO, IO.FileMode.Create)
Dim hIcon As IntPtr = ImgBMP.GetHicon()
Dim icn As Icon = Icon.FromHandle(hIcon)
icn.Save(fs)
fs.Flush()
icn.Dispose() : fs.Close() : fs = Nothing
End Sub
Private Sub CreateIcon(ByVal bitmapName As String)
Try
Dim fi As New System.IO.FileInfo(bitmapName)
Dim bmp As New Bitmap(fi.FullName)
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(fi.FullName.Replace(fi.Extension, ".ico"))
Icon.FromHandle(bmp.GetHicon).Save(sw.BaseStream)
sw.Close()
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex)
End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
''' <summary>
''' Converts an image into an icon.
''' </summary>
''' <param name="img">The image that shall become an icon</param>
''' <param name="size">The width and height of the icon. Standard
''' sizes are 16x16, 32x32, 48x48, 64x64.</param>
''' <param name="keepAspectRatio">Whether the image should be squashed into a
''' square or whether whitespace should be put around it.</param>
''' <returns>An icon!!</returns>
Private Function MakeIcon(img As Image, size As Integer, keepAspectRatio As Boolean) As Icon
Dim square As New Bitmap(size, size)
' create new bitmap
Dim g As Graphics = Graphics.FromImage(square)
' allow drawing to it
Dim x As Integer, y As Integer, w As Integer, h As Integer
' dimensions for new image
If Not keepAspectRatio OrElse img.Height = img.Width Then
' just fill the square
x = InlineAssignHelper(y, 0)
' set x and y to 0
' set width and height to size
w = InlineAssignHelper(h, size)
Else
' work out the aspect ratio
Dim r As Single = CSng(img.Width) / CSng(img.Height)
' set dimensions accordingly to fit inside size^2 square
If r > 1 Then
' w is bigger, so divide h by r
w = size
h = CInt(Math.Truncate(CSng(size) / r))
x = 0
' center the image
y = (size - h) \ 2
Else
' h is bigger, so multiply w by r
w = CInt(Math.Truncate(CSng(size) * r))
h = size
y = 0
' center the image
x = (size - w) \ 2
End If
End If
' make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(img, x, y, w, h)
' draw image with specified dimensions
g.Flush()
' make sure all drawing operations complete before we get the icon
' following line would work directly on any image, but then
' it wouldn't look as nice.
Return Icon.FromHandle(square.GetHicon())
End Function
By ImaginSystems & Queen Gin