当前位置:网站首页>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
- 硅谷产品实战学习感触
- Linux foundation - centos7 offline installation of MySQL
- Stm32f030f4 drives tim1637 nixie tube chip
- excel如何打开100万行以上的csv文件
- Door level modeling - after class exercises
- JPA handwritten SQL, received with user-defined entity classes
- 安全协议重点
- Daily three questions 6.29
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
猜你喜欢

Redis RDB snapshot

Paramètres communs de matplotlib

Door level modeling - after class exercises

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

神经网络物联网的发展趋势和未来方向

学成在线案例实战

2022 R1 fast opening pressure vessel operation test questions and answers

Matplotlib常用图表

Overview of edge calculation

Concepts of dictionary, hash table and array
随机推荐
[must] bm41 output the right view of the binary tree [medium +]
Behind sharing e-commerce: the spirit of CO creation, symbiosis, sharing, CO prosperity and win-win
Redis master-slave synchronization
ARP message header format and request flow
ADO.NET之sqlCommand对象
ShanDong Multi-University Training #3
【.Net Core】程序相关各种全局文件
Linux foundation - centos7 offline installation of MySQL
Zero foundation tutorial of Internet of things development
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
深度学习 | 三个概念:Epoch, Batch, Iteration
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
软件架构的本质
Redis data types and application scenarios
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
纪念成为首个DAYUs200三方demo贡献者
Why is PHP called hypertext preprocessor
神经网络物联网的发展趋势和未来方向
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object