如果想自己设计一个个性独特的ico图片,然后让它成为如"我的电脑","回收站"这样的图标该怎么做?就只有用一些专门的画图工具。因为windows的的画图程序无法创建ico文件。于是本人利用.net和GDI+就编写了一个这样的画图工具。虽然现在有很多文章都介绍了GDI+技术,但都只是纯粹的GDI+的简单应用的介绍,至少我还没有看见一篇利用GDI+开发一个完整软件或程序片段的文章。
这个程序实现了以下的功能:将BMP、JPG、jpeg、GIF、.png、.tiff文件转化成ico文件,可以对转化后的文件进行编辑;创建并编辑一个新的ico文件;对已有的ico文件进行编辑。所有被编辑的文件都保存为ico文件,可以在任何可使用ico文件的地方使用它们。
我先说明一下什么是GDI+。GDI+ 是GDI(Windows 早期版本提供的图形设备接口)的后续版本,是Microsoft Windows XP操作系统即后续版本的图形显示技术。它已经集成到了.net开发环境中,所以不管你的OS是什么版本,只要安装了.NET框架,就有了GDI+(注意:是.net框架,而不是.net开发环境,所以win98中也可以使用GDI+)。当然它也提供了传统的api,可以由.net或非.net开发工具调用它。由于他和GDI的使用有很大的差别,所以要使用GDI+就必须从头学。GDI+要比GDI简单得多。
现在就来看一下如何实现这个软件:先添加picturebox,0penfiledialog,savefiledialog,colordialog,domainupdown,label控件;然后添加两个菜单即它们的子菜单,添加的菜单如下"文件"菜单包括"新建","打开","保存","退出","功能"菜单包括"直线","选择颜色"代码如下,在代码后给出程序说明:
| 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 |