使用VB.net将PNG图片转成icon类型图标文件

有的时候,需要用到icon图标型图片,但一般网站上不好找到适合自己的icon型图片,所以,如果能将适合的图片直接转换成icon格式,那么会方便很多。

使用vb.net,可以自己编写一个转换的小程序,这样,需要icon格式的时候,直接转换一下就行了。

程序:

01 建立中间存储流

		Dim image As Image = New Bitmap(New Bitmap(ori), size) '读取源图片为bitmap,缩放至需要的大小
        Dim bitmapstream As MemoryStream = New MemoryStream()  '源图片的内存流
        Dim iconstream As MemoryStream = New MemoryStream()    'ico图片的内存流
        image.Save(bitmapstream, ImageFormat.Png)             '源图片读取为设定格式并存入源图片内存流
        Dim iconwriter As BinaryWriter = New BinaryWriter(iconstream)  '新建二进制写入器,写入目标ico图片内存流

02 在流中写入头文件(这里应该是固定格式)

 		iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(1))
        iconwriter.Write(Convert.ToInt16(1))
        iconwriter.Write(Convert.ToByte(image.Width))
        iconwriter.Write(Convert.ToByte(image.Height))
        iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(32))
        iconwriter.Write(Convert.ToInt32(bitmapstream.Length))
        iconwriter.Write(22)

03 写入并保存icon图片信息

 '将源图片写入目标ico图标内存流
        iconwriter.Write(bitmapstream.ToArray())

        '保存流,并将流指针定位至头部,以Icon对象进行读取输出为文件
        iconwriter.Flush()
        iconwriter.Seek(0, SeekOrigin.Begin)
        Dim iconFileStream As Stream = New FileStream(dest, FileMode.Create)
        Dim icon As Icon = New Icon(iconstream)
        icon.Save(iconFileStream)

04 释放资源

 '资源释放
        iconFileStream.Close()
        iconwriter.Close()
        iconstream.Close()
        bitmapstream.Close()
        icon.Dispose()
        image.Dispose()

上面是转换程序的关键代码,实际使用时,需要自己添加一下UI界面,方面操作。

完整程序:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
'Imports png_to_icon

Public Class Form5

    Dim orifilename As String
    Dim destfilename As String

    Dim imgfmt As ImageFormat

    Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then

            Label1.Text = OpenFileDialog1.FileName
            orifilename = OpenFileDialog1.FileName
        End If
        Dim a As String()
        a = orifilename.Split(".")
        Label2.Text = a(a.Length - 1)

    End Sub

    Private Sub p_to_ico(ori As String, dest As String, imgfmt As ImageFormat, size As Size)

        If size.Width > 255 Or size.Height > 255 Then
            MsgBox("ico图片尺寸超过范围", MsgBoxStyle.Exclamation, "警告消息!")
            Exit Sub
        End If
        Dim image As Image = New Bitmap(New Bitmap(ori), size) '读取源图片为bitmap,缩放至需要的大小
        Dim bitmapstream As MemoryStream = New MemoryStream()  '源图片的内存流
        Dim iconstream As MemoryStream = New MemoryStream()    'ico图片的内存流
        image.Save(bitmapstream, ImageFormat.Png)             '源图片读取为设定格式并存入源图片内存流
        Dim iconwriter As BinaryWriter = New BinaryWriter(iconstream)  '新建二进制写入器,写入目标ico图片内存流

        '根据原图信息,进行头文件写入
        iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(1))
        iconwriter.Write(Convert.ToInt16(1))
        iconwriter.Write(Convert.ToByte(image.Width))
        iconwriter.Write(Convert.ToByte(image.Height))
        iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(0))
        iconwriter.Write(Convert.ToInt16(32))
        iconwriter.Write(Convert.ToInt32(bitmapstream.Length))
        iconwriter.Write(22)

        '将源图片写入目标ico图标内存流
        iconwriter.Write(bitmapstream.ToArray())

        '保存流,并将流指针定位至头部,以Icon对象进行读取输出为文件
        iconwriter.Flush()
        iconwriter.Seek(0, SeekOrigin.Begin)
        Dim iconFileStream As Stream = New FileStream(dest, FileMode.Create)
        Dim icon As Icon = New Icon(iconstream)
        icon.Save(iconFileStream)


        ProgressBar1.Maximum = iconFileStream.Length
        ProgressBar1.Value = iconFileStream.Length

        '资源释放
        iconFileStream.Close()
        iconwriter.Close()
        iconstream.Close()
        bitmapstream.Close()
        icon.Dispose()
        image.Dispose()
        'Return File.Exists(dest)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|ico files (*.ico)|*.ico|All files (*.*)|*.*"
        SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True

        If SaveFileDialog1.ShowDialog = DialogResult.OK Then

            Label3.Text = SaveFileDialog1.FileName
            destfilename = SaveFileDialog1.FileName

        End If
        Dim a As String()
        a = destfilename.Split(".")
        Label7.Text = a(a.Length - 1)

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim a As String
        a = Label2.Text
        If a = "png" Or a = "Png" Then
            imgfmt = ImageFormat.Png
        ElseIf a = "bmp" Or a = "Bmp" Then
            imgfmt = ImageFormat.Bmp
        ElseIf a = "jpeg" Or a = "Jpeg" Or a = "jpg" Then
            imgfmt = ImageFormat.Jpeg

        End If

        ' destfilename = "C:\Users\rongjv\Desktop\tt11.ico"

        ' btoico.p_to_ico.ctoico(orifilename, destfilename, New Size(128, 128))

        p_to_ico(orifilename, destfilename, imgfmt, New Size(128, 128))

    End Sub


End Class

界面:
使用VB.net将PNG图片转成icon类型图标文件_第1张图片
界面比较简单,以实现功能为主。

你可能感兴趣的:(VB.net实例应用,.net,前端,VB.net)