当前位置:网站首页>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 .
边栏推荐
- 使用 pair 做 unordered_map 的键值
- 软件架构的本质
- Paramètres communs de matplotlib
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- mysql:insert ignore、insert和replace区别
- 边缘计算概述
- 有没有一段代码,让你为人类的智慧所折服
- excel如何打开100万行以上的csv文件
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- Three development trends of enterprise application from the perspective of the third technological revolution
猜你喜欢

边缘计算概述

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

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

Is there a piece of code that makes you convinced by human wisdom

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

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

Redis AOF log

How to display real-time 2D map after rviz is opened

Matplotlib common settings

2022 R1 fast opening pressure vessel operation test questions and answers
随机推荐
Matplotlib常用图表
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
BlocProvider为什么感觉和Provider很相似?
2021 RoboCom 世界机器人开发者大赛-高职组初赛
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
MT manager test skiing Adventure
Is there a piece of code that makes you convinced by human wisdom
每日三题 6.30
股票开户哪个证券公司最好,有安全保障吗
Redis 主从同步
[must] bm41 output the right view of the binary tree [medium +]
距离度量 —— 汉明距离(Hamming Distance)
PostgreSQL source code (57) why is the performance gap so large in hot update?
Matplotlib常用设置
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Write some suggestions to current and future doctoral students to sort out and share
安全协议重点
PostgreSQL notes (10) dynamically execute syntax parsing process
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
dat. GUI