Visual Basic.NET和GDI+共创图标编辑器
Visual Basic.NET和GDI+共创图标编辑器,Visual Basic.NET和GDI+共创图标编辑器
我先说明一下什么是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
Priva