当前位置:网站首页>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 .
边栏推荐
- Current situation and future development trend of Internet of things
- TS initial use, TS type
- 使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
- Daily three questions 6.28
- 写给当前及未来博士研究生一些建议整理分享
- Leetcode (34) -- find the first and last positions of elements in a sorted array
- 小程序表单校验封装
- 神经网络物联网的未来趋势与发展
- Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
- Matplotlib常用设置
猜你喜欢

物联网现状及未来发展趋势

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘

TS initial use, TS type

Linux基础 —— CentOS7 离线安装 MySQL

Know --matplotlib

What category does the Internet of things application technology major belong to

Yunxin small class | common cognitive misunderstandings in IM and audio and video

云信小课堂 | IM及音视频中常见的认知误区

2021 robocom world robot developer competition - preliminary competition of higher vocational group

2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
随机推荐
每日三题 6.28
【ES实战】ES上的安全性运行方式
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Postgresql源码(58)元组拼接heap_form_tuple剖析
写给当前及未来博士研究生一些建议整理分享
Postgresql源码(57)HOT更新为什么性能差距那么大?
物联网技术应用属于什么专业分类
2021 RoboCom 世界机器人开发者大赛-高职组复赛
sql 优化
Applet form verification encapsulation
Daily three questions 6.30 (2)
Development trend and future direction of neural network Internet of things
每日三题 6.29
Matplotlib常用图表
ARP报文头部格式和请求流程
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
字典、哈希表、数组的概念
[swoole Series 1] what will you learn in the world of swoole?
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘