我先说明一下什么是GDI+。GDI+ 是GDI(Windows 早期版本提供的图形设备接口)的后续版本,是Microsoft Windows
XP操作系统即后续版本的图形显示技术。它已经集成到了.net开发环境中,所以不管你的OS是什么版本,只要安装了.NET框架,就有了GDI+(注意:是.net框架,而不是.net开发环境,所以win98中也可以使用GDI+)。当然它也提供了传统的api,可以由.net或非.net开发工具调用它。由于他和GDI的使用有很大的差别,所以要使用GDI+就必须从头学。GDI+要比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 '打开图片文件即"打开"菜单"
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)
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)