扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:张悦 来源:yesky 2007年11月10日
关键字: Visual Basic GDI+ 图标编辑器
Public Class Form1 Inherits System.Windows.Forms.Form Public imagepen, newbit, changiamge, mpen 'movepen,moveb,,grh,filenames,endpen Dim xd, yd, xu, yu, pk, ps Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem9.Click '新建一个ico文件,即"新建"菜单 PictureBox1.Image = Nothing Dim bitnew As New System.Drawing.Bitmap(32, 32, Drawing.Imaging.PixelFormat.Format32bppArgb)'建立一个Bitmap对象,以便在它上面画图 Dim x, y For x = 0 To 31 For y = 0 To 31 bitnew.SetPixel(x, y, Color.Transparent)'将Bitmap的背景设置为透明 Next Next newbit = bitnew MenuItem3.Enabled = False'"选择颜色"菜单不可用 MenuItem2.Enabled = True'"直线"菜单可用 End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)' Handles MenuItem6.Click '打开图片文件即"打开"菜单" OpenFileDialog1.Filter = "ico文件(*.ico)|*.ico|图像文件 (*.BMP;*.JPG;*.jpeg;*.GIF;*.png;*.tiff)|*.BMP;*.JPG;*.jpeg;*.GIF;*.png;*.tiff" OpenFileDialog1.FilterIndex = 2 OpenFileDialog1.ShowDialog() OpenFileDialog1.FileName = "" End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem8.Click Me.Close()'退出 End Sub Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem7.Click '保存文件,即"保存"对话筐 PictureBox1.Cursor = System.Windows.Forms.Cursors.Default SaveFileDialog1.Filter = "ico文件(*.ico)|*.ico"'设置要保存的文件后缀 SaveFileDialog1.ShowDialog() If SaveFileDialog1.FileName <> "" Then If Not SaveFileDialog1.ShowDialog.Cancel Then Dim bmp As New System.Drawing.Bitmap(PictureBox1.Image, 32,32)'从PictureBox1.Image初始化Bitmap,设置保存为图片的大小,标准ico图由 32*32和16*16两种格式组成,此处为32*32,你也可以设置为16*16 Dim ico As System.Drawing.Icon = ico.FromHandle(bmp.GetHicon()) '用Bitmap的句柄,初始化icon,他是专门处理ico文件的类 Dim file As New System.IO.FileStream(SaveFileDialog1.FileName(), IO.FileMode.Create)'创建文件流 ico.Save(file)'保存为ico文件 file.Close()'关闭流 End If End If End Sub Public Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem2.Click '是用直线在新建的ico中画图 PictureBox1.Cursor =System.Windows.Forms.Cursors.Cross '在PictureBox1中鼠标的样式 ColorDialog1.ShowDialog() Dim pen As New Pen(ColorDialog1.Color, DomainUpDown1.Text())'创建画笔 imagepen = pen End Sub Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) 'Handles PictureBox1.MouseDown '当按下鼠标左键时获取直线的起点 If e.Button = MouseButtons.Left Then xd = e.X / 8 : yd = e.Y / 8 End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) 'Handles PictureBox1.MouseUp '画出直线 If PictureBox1.Cursor Is System.Windows.Forms.Cursors.Cross And ps <> 1 Then xu = e.X : yu = e.Y Me.k(1, imagepen, yu / 8, xu / 8, xd, yd) Else If OpenFileDialog1.FilterIndex = 1 Then xu = e.X : yu = e.Y Me.k(2, mpen, yu / 8, xu / 8, xd, yd) End If End If End Sub Public Sub k(ByVal k As Integer, ByVal drawtool As Object, ByVal x As Integer, ByVal y As Integer, ByVal xs As Integer, ByVal ys As Integer) If k = 1 Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage'自动容纳图片 PictureBox1.Image = newbit Dim Graphic As Graphics Graphic = Graphic.FromImage(Me.PictureBox1.Image)'在PictureBox1上画图 Graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias'锯齿削边 Graphic.DrawLine(drawtool, y, x, xs, ys)'画线 End If If k = 2 Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage PictureBox1.Image = changiamge Dim Graphic As Graphics Graphic = Graphic.FromImage(Me.PictureBox1.Image) Graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias Graphic.DrawLine(drawtool, y, x, xs, ys) End If End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles MenuItem3.Click '对打开的ico文件用直线画图 ColorDialog1.ShowDialog() Dim m3pen As New Pen(ColorDialog1.Color, DomainUpDown1.Text())'建立画笔 mpen = m3pen End Sub Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) 'Handles OpenFileDialog1.FileOk '打开文件 If OpenFileDialog1.FilterIndex = 1 Then Dim m3pen As New Pen(Color.Black, DomainUpDown1.Text()) mpen = m3pen MenuItem2.Enabled = False MenuItem3.Enabled = True Else MenuItem3.Enabled = False MenuItem2.Enabled = False End If If OpenFileDialog1.FileName <> "" Then PictureBox1.Cursor = System.Windows.Forms.Cursors.Default Dim images As New System.Drawing.Bitmap(OpenFileDialog1.FileName) changiamge = images PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage PictureBox1.Image = images Me.Text = OpenFileDialog1.FileName End If End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles MyBase.Load '由于刚运行次程序时,没有打开的ico文件和新建立的ico对象所以不可以创建画图工具对象 MenuItem3.Enabled = False MenuItem2.Enabled = False End Sub End Class |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者