当前位置:网站首页>Use vb Net to convert PNG pictures into icon type icon files
Use vb Net to convert PNG pictures into icon type icon files
2022-07-01 23:37:00 【Organization Division】
sometimes , Need to use icon Icon Pictures , But it's hard to find suitable ones on general websites icon Type picture , therefore , If you can convert suitable pictures directly into icon Format , It will be much more convenient .
Use vb.net, You can write a conversion applet by yourself , such , need icon Format time , Just convert it directly .
Program :
01 Create an intermediate storage stream
Dim image As Image = New Bitmap(New Bitmap(ori), size) ' Read the source image as bitmap, Zoom to the desired size
Dim bitmapstream As MemoryStream = New MemoryStream() ' Memory stream of source image
Dim iconstream As MemoryStream = New MemoryStream() 'ico Memory flow of pictures
image.Save(bitmapstream, ImageFormat.Png) ' The source image is read as a set format and stored in the source image memory stream
Dim iconwriter As BinaryWriter = New BinaryWriter(iconstream) ' Create a new binary writer , Write the target ico Picture memory stream
02 Write header file in stream ( This should be a fixed format )
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 Write and save icon Image information
' Write the source image to the destination ico Icon memory flow
iconwriter.Write(bitmapstream.ToArray())
' Save stream , And position the flow pointer to the head , With Icon Object to read and output to a file
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 Release resources
' Release resources
iconFileStream.Close()
iconwriter.Close()
iconstream.Close()
bitmapstream.Close()
icon.Dispose()
image.Dispose()
The above is the key code of the conversion program , In actual use , You need to add it yourself UI Interface , Aspect operation .
The whole program :
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 The picture size is out of range ", MsgBoxStyle.Exclamation, " Warning message !")
Exit Sub
End If
Dim image As Image = New Bitmap(New Bitmap(ori), size) ' Read the source image as bitmap, Zoom to the desired size
Dim bitmapstream As MemoryStream = New MemoryStream() ' Memory stream of source image
Dim iconstream As MemoryStream = New MemoryStream() 'ico Memory flow of pictures
image.Save(bitmapstream, ImageFormat.Png) ' The source image is read as a set format and stored in the source image memory stream
Dim iconwriter As BinaryWriter = New BinaryWriter(iconstream) ' Create a new binary writer , Write the target ico Picture memory stream
' According to the information of the original drawing , Write header file
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)
' Write the source image to the destination ico Icon memory flow
iconwriter.Write(bitmapstream.ToArray())
' Save stream , And position the flow pointer to the head , With Icon Object to read and output to a file
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
' Release resources
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
Interface :
The interface is relatively simple , Focus on realizing functions .
边栏推荐
猜你喜欢
What professional classification does the application of Internet of things technology belong to
Matplotlib常用設置
神经网络物联网的未来趋势与发展
2021 robocom world robot developer competition - semi finals of higher vocational group
SWT / anr problem - SWT causes kernel fuse deadlock
2022 crane driver (limited to bridge crane) examination questions and simulation examination
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
ARP报文头部格式和请求流程
2021 RoboCom 世界机器人开发者大赛-高职组初赛
How to display real-time 2D map after rviz is opened
随机推荐
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
第六章 数据流建模
2021 RoboCom 世界机器人开发者大赛-高职组初赛
What professional classification does the application of Internet of things technology belong to
Daily three questions 6.30
Zero foundation tutorial of Internet of things development
[must] bm41 output the right view of the binary tree [medium +]
每日三题 6.30(2)
2022 crane driver (limited to bridge crane) examination questions and simulation examination
The essence of software architecture
Matplotlib常用设置
Redis数据类型和应用场景
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
Li Kou today's question -241 Design priorities for operational expressions
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
mysql:insert ignore、insert和replace区别
TS initial use, TS type
Notblank and notempty
股票开户哪个证券公司最好,有安全保障吗
【必会】BM41 输出二叉树的右视图【中等+】