当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Matplotlib常用设置
Redis RDB snapshot
Linux基础 —— CentOS7 离线安装 MySQL
Experience of practical learning of Silicon Valley products
Development trend and future direction of neural network Internet of things
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
MySQL binlog cleanup
华为HMS Core携手超图为三维GIS注入新动能
有没有一段代码,让你为人类的智慧所折服
2022 R1 fast opening pressure vessel operation test questions and answers
随机推荐
Postgresql源码(58)元组拼接heap_form_tuple剖析
Switch to software testing, knowing these four points is enough!
What category does the Internet of things application technology major belong to
上海炒股开户选择手机办理安全吗?
Stm32f030f4 drives tim1637 nixie tube chip
Notblank and notempty
[leetcode] length of the last word [58]
sql 优化
cookie、session、tooken
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Create Ca and issue certificate through go language
ShanDong Multi-University Training #3
图的遍历之深度优先搜索和广度优先搜索
Chapter 6 data flow modeling
【C#】依赖注入及Autofac
【.Net Core】程序相关各种全局文件
Current situation and future development trend of Internet of things
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
硅谷产品实战学习感触
Practical application and extension of plain framework