希沃白板在学校里基本上是一直使用的,但是在非希沃电脑里面是没有启动白板的
简答介绍思路和具体的功能
1、背景颜色和画笔颜色自由切换、画笔粗细1~20可以调节。
2、画笔样式:虚线、点线、短线
3、基本图形:矩形,正方形,椭圆,正圆、直线、文字
4、橡皮、加载、保存功能
注意点:
1、主要的画图模块在pic.mousemove事件里,重点
2、使用 Pic.Invalidate()
Pic.Update()
实现实时更新
3、drawstring在透明bitmap上绘制文字会有黑边
源代码公开一下:
Imports System.ComponentModel
Imports System.Drawing.Drawing2DPublic Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadSetStyle(ControlStyles.UserPaint, True)SetStyle(ControlStyles.AllPaintingInWmPaint, True)SetStyle(ControlStyles.DoubleBuffer, True)Filled = FalsePanel3.Visible = FalsePanel1.Visible = FalsePic2.Visible = FalsePic.Location = New Point(-1000, -1000)Pic.Width = 5000Pic.Height = 5000penImg = New Bitmap(Pic.Width, Pic.Height)g1 = Graphics.FromImage(penImg)g1.Clear(Color.Transparent)g1.SmoothingMode = SmoothingMode.HighQuality' g1.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFitPic.Image = penImgpen.StartCap = LineCap.Roundpen.EndCap = LineCap.RoundIf My.Application.CommandLineArgs().Count = 1 ThenpenImg = New Bitmap(My.Application.CommandLineArgs(0))Pic.Width = penImg.WidthPic.Height = penImg.Heightg1 = Graphics.FromImage(penImg)'g1.Clear(Color.Transparent)g1.SmoothingMode = SmoothingMode.HighQualityPic.Image = penImgpen.StartCap = LineCap.Roundpen.EndCap = LineCap.RoundEnd IfEnd SubPrivate Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing' VW.Stop()End SubDim MoveDown As Boolean = FalseDim CurrX As IntegerDim CurrY As IntegerDim MousX As IntegerDim MousY As IntegerDim x1, x2, y1, y2 As IntegerPrivate Sub Pic_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic.MouseDownMousX = e.XMousY = e.YMoveDown = Truex1 = e.Xy1 = e.Yx2 = e.Xy2 = e.YEnd SubDim g1 As GraphicsPublic penImg As BitmapDim listPoint As New List(Of Drawing.Point)Private Sub Pic_MouseMove(sender As Object, e As MouseEventArgs) Handles Pic.MouseMoveIf MoveDown = True ThenPic.Invalidate()Pic.Update()If func = 0 ThenCurrX = Pic.Left - MousX + e.XCurrY = Pic.Top - MousY + e.YPic.Location = New Drawing.Point(CurrX, CurrY)ElseIf func = 1 ThenlistPoint.Add(New Drawing.Point(e.X, e.Y))If listPoint.Count > 3 Theng1.DrawCurve(pen, listPoint.ToArray(), 0.1)End If'x1 = e.X' y1 = e.Y' g1.DrawLine(pen, New Point(x1, y1), New Point(x2, y2))' x2 = e.X' y2 = e.YElseIf func = 2 ThenPic2.Visible = Truex1 = e.Xy1 = e.Yg1.CompositingMode = CompositingMode.SourceCopyg1.FillRectangle(New SolidBrush(Color.Transparent), New Rectangle(x1 - 25, y1 - 25, 50, 50))Pic2.Location = New Drawing.Point(x1 + Pic.Location.X - 25, y1 + Pic.Location.Y - 25)Pic2.Width = 50Pic2.Height = 50ElseIf func = 3 ThenIf Filled = False ThenPic.CreateGraphics.DrawEllipse(pen, New Rectangle(x1, y1, e.X - x1, e.Y - y1))x2 = e.Xy2 = e.YElsePic.CreateGraphics.FillEllipse(New SolidBrush(pen.Color), New Rectangle(x1, y1, e.X - x1, e.Y - y1))x2 = e.Xy2 = e.YEnd IfElseIf func = 4 ThenIf Filled = False ThenPic.CreateGraphics.DrawRectangle(pen, PointList(New Point(x1, y1), New Point(e.X, e.Y)))x2 = e.Xy2 = e.YElsePic.CreateGraphics.FillRectangle(New SolidBrush(pen.Color), New Rectangle(x1, y1, e.X - x1, e.Y - y1))x2 = e.Xy2 = e.YEnd IfElseIf func = 5 ThenPic.CreateGraphics.DrawLine(pen, x1, y1, e.X, e.Y)ElseIf func = 6 ThenIf Filled = False ThenPic.CreateGraphics.DrawRectangle(pen, PointListT(New Point(x1, y1), New Point(e.X, e.Y)))x2 = e.Xy2 = e.YElsePic.CreateGraphics.FillRectangle(New SolidBrush(pen.Color), PointListT(New Point(x1, y1), New Point(e.X, e.Y)))x2 = e.Xy2 = e.YEnd IfElseIf func = 7 ThenIf Filled = False ThenPic.CreateGraphics.DrawEllipse(pen, PointListT(New Point(x1, y1), New Point(e.X, e.Y)))x2 = e.Xy2 = e.YElsePic.CreateGraphics.FillEllipse(New SolidBrush(pen.Color), PointListT(New Point(x1, y1), New Point(e.X, e.Y)))x2 = e.Xy2 = e.YEnd IfElseIf func = 8 ThenPic.CreateGraphics.DrawString(s, TxtFont, New SolidBrush(pen.Color), e.X, e.Y)End If'Pic.Image = penImgEnd IfEnd SubPrivate Sub Pic_MouseUp(sender As Object, e As MouseEventArgs) Handles Pic.MouseUpMoveDown = Falseg1.CompositingMode = CompositingMode.SourceCopyPic2.Visible = FalselistPoint.Clear()If func = 3 ThenIf Filled = False ThenPic.Invalidate()g1.DrawEllipse(pen, New Rectangle(x1, y1, e.X - x1, e.Y - y1))ElsePic.Invalidate()g1.FillEllipse(New SolidBrush(pen.Color), New Rectangle(x1, y1, e.X - x1, e.Y - y1))End IfEnd IfIf func = 4 ThenIf Filled = False ThenPic.Invalidate()g1.DrawRectangle(pen, PointList(New Point(x1, y1), New Point(e.X, e.Y)))ElsePic.Invalidate()g1.FillRectangle(New SolidBrush(pen.Color), New Rectangle(x1, y1, e.X - x1, e.Y - y1))End IfEnd IfIf func = 5 ThenPic.Invalidate()g1.DrawLine(pen, x1, y1, e.X, e.Y)End IfIf func = 6 ThenIf Filled = False ThenPic.Invalidate()g1.DrawRectangle(pen, PointListT(New Point(x1, y1), New Point(e.X, e.Y)))ElsePic.Invalidate()g1.FillRectangle(New SolidBrush(pen.Color), PointListT(New Point(x1, y1), New Point(e.X, e.Y)))End IfEnd IfIf func = 7 ThenIf Filled = False ThenPic.Invalidate()g1.DrawEllipse(pen, PointListT(New Point(x1, y1), New Point(e.X, e.Y)))ElsePic.Invalidate()g1.FillEllipse(New SolidBrush(pen.Color), PointListT(New Point(x1, y1), New Point(e.X, e.Y)))End IfEnd IfIf func = 8 Theng1 = Graphics.FromImage(penImg)g1.SmoothingMode = SmoothingMode.HighQualityg1.TextRenderingHint = 5Dim stringSize As SizeF = g1.MeasureString(s, TxtFont)Dim bmp As New Bitmap(CInt(stringSize.Width), CInt(stringSize.Height))Dim gb As Graphicsgb = Graphics.FromImage(bmp)If pen.Color <> Color.Black Thengb.Clear(Color.Black)gb.TextRenderingHint = 5gb.DrawString(s, TxtFont, New SolidBrush(pen.Color), 0, 0)bmp = New Bitmap(ColorReplace(bmp, Color.Black, Color.Transparent))g1.DrawImage(bmp, New Point(e.X, e.Y))ElseIf pen.Color = Color.Black Thengb.Clear(Color.White)gb.TextRenderingHint = 5gb.DrawString(s, TxtFont, New SolidBrush(pen.Color), 0, 0)bmp = New Bitmap(ColorReplace(bmp, Color.White, Color.Transparent))g1.DrawImage(bmp, New Point(e.X, e.Y))End If'g1.DrawString(s, TxtFont, New SolidBrush(pen.Color), New Point(e.X, e.Y))End IfPic.Image = penImgEnd SubPublic Function ColorReplace(bmp As Bitmap, OldColor As Color, NewColor As Color) As BitmapDim destImg As New Bitmap(bmp.Width, bmp.Height)Dim mycolormap(0) As Imaging.ColorMapmycolormap(0) = New Imaging.ColorMap()mycolormap(0).OldColor = OldColormycolormap(0).NewColor = NewColorDim imageAttributes As New Imaging.ImageAttributes()imageAttributes.SetRemapTable(mycolormap)Dim g As Graphics = Graphics.FromImage(destImg)g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes)Return destImgEnd FunctionDim func As Integer = 0Dim pen As New Pen(Color.Red, 2)Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickIf func = 0 ThenDim cdlg As New ColorDialogIf cdlg.ShowDialog() = DialogResult.OK ThenPic.BackColor = cdlg.ColorEnd IfEnd Iffunc = 0End SubPrivate Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.ClickIf func = 1 ThenPanel1.Visible = TruePanel1.Location = New Point(Button3.Location.X + Panel2.Location.X, Button3.Location.Y + Panel2.Location.Y - 100)End Iffunc = 1End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Clickfunc = 2End SubPrivate Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.ResizePanel2.Location = New Point((Width - Panel2.Width) / 2, Height - 100)l.Location = New Point((Width - l.Width) / 2, (Height - l.Height) / 2)End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickPanel1.Visible = FalseEnd SubDim s As StringPrivate Sub Pic_Click(sender As Object, e As EventArgs) Handles Pic.ClickPanel1.Visible = FalseEnd SubPrivate Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles R1.CheckedChangedIf R1.Checked = True Thenpen.Width = 2TrB.Value = 2L1.Text = 2End IfEnd SubPrivate Sub R2_CheckedChanged(sender As Object, e As EventArgs) Handles R2.CheckedChangedIf R2.Checked = True Thenpen.Width = 4TrB.Value = 4L1.Text = 4End IfEnd SubPrivate Sub R3_CheckedChanged(sender As Object, e As EventArgs) Handles R3.CheckedChangedIf R3.Checked = True Thenpen.Width = 8TrB.Value = 8L1.Text = 8End IfEnd SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickDim save As New SaveFileDialogsave.Filter = "All .net Picture Files|*.jpg;*.png;*.bmp;*.ico;*.jpeg;*.*"save.InitialDirectory = Application.StartupPathDim a = save.ShowDialogIf a = DialogResult.OK ThenpenImg.Save(save.FileName)End IfEnd SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickDim cdlg As New ColorDialogIf cdlg.ShowDialog() = DialogResult.OK Thenpen.Color = cdlg.ColorButton6.BackColor = cdlg.ColorEnd IfEnd SubPrivate Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Clickpen.Color = Color.RedButton6.BackColor = Color.RedEnd SubPrivate Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Clickpen.Color = Color.BlackButton6.BackColor = Color.BlackEnd SubPrivate Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Clickpen.Color = Color.LimeButton6.BackColor = Color.LimeEnd SubPrivate Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Clickpen.Color = Color.CyanButton6.BackColor = Color.CyanEnd SubPrivate Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Clickpen.Color = Color.BlueButton6.BackColor = Color.BlueEnd SubPrivate Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Clickpen.Color = Color.MagentaButton6.BackColor = Color.MagentaEnd SubPrivate Sub PictureBox7_Click(sender As Object, e As EventArgs) Handles PictureBox7.Clickpen.Color = Color.YellowButton6.BackColor = Color.YellowEnd SubPrivate Sub PictureBox8_Click(sender As Object, e As EventArgs) Handles PictureBox8.Clickpen.Color = Color.OrangeButton6.BackColor = Color.OrangeEnd SubPrivate Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrB.Scrollpen.Width = TrB.ValueL1.Text = TrB.ValueEnd SubPrivate Sub PictureBox9_Click(sender As Object, e As EventArgs) Handles PictureBox9.Clickpen.Color = Color.WhiteButton6.BackColor = Color.WhiteEnd SubPrivate Sub bg1_CheckedChanged(sender As Object, e As EventArgs) Handles bg1.CheckedChangedIf bg1.Checked = True ThenFilled = FalseEnd IfEnd SubPrivate Sub bg2_CheckedChanged(sender As Object, e As EventArgs) Handles bg2.CheckedChangedIf bg2.Checked = True ThenFilled = TrueEnd IfEnd SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickIf MsgBox("确认清屏!", vbInformation + vbYesNo, "自定义白板") = MsgBoxResult.Yes Theng1.Clear(Color.Transparent)Pic.Image = penImgEnd IfEnd SubPrivate Sub RadioButton1_CheckedChanged_1(sender As Object, e As EventArgs) Handles ls0.CheckedChangedIf ls0.Checked = True Thenpen.DashStyle = DashStyle.SolidEnd IfEnd SubPrivate Sub sc0_CheckedChanged(sender As Object, e As EventArgs) Handles sc0.CheckedChangedIf sc0.Checked = True Thenpen.StartCap = LineCap.FlatEnd IfEnd SubPrivate Sub sc1_CheckedChanged(sender As Object, e As EventArgs) Handles sc1.CheckedChangedIf sc1.Checked = True Thenpen.StartCap = LineCap.ArrowAnchorEnd IfEnd SubPrivate Sub sc2_CheckedChanged(sender As Object, e As EventArgs) Handles sc2.CheckedChangedIf sc2.Checked = True Thenpen.StartCap = LineCap.RoundEnd IfEnd SubPrivate Sub ec0_CheckedChanged(sender As Object, e As EventArgs) Handles ec0.CheckedChangedIf ec0.Checked = True Thenpen.EndCap = LineCap.FlatEnd IfEnd SubPrivate Sub ec1_CheckedChanged(sender As Object, e As EventArgs) Handles ec1.CheckedChangedIf ec1.Checked = True Thenpen.EndCap = LineCap.ArrowAnchorEnd IfEnd SubPrivate Sub ec2_CheckedChanged(sender As Object, e As EventArgs) Handles ec2.CheckedChangedIf ec2.Checked = True Thenpen.EndCap = LineCap.RoundEnd IfEnd SubPrivate Sub ls1_CheckedChanged(sender As Object, e As EventArgs) Handles ls1.CheckedChangedIf ls1.Checked = True Thenpen.DashStyle = DashStyle.DotEnd IfEnd SubPrivate Sub ls2_CheckedChanged(sender As Object, e As EventArgs) Handles ls2.CheckedChangedIf ls2.Checked = True Thenpen.DashStyle = DashStyle.DashDotEnd IfEnd SubPrivate Sub ls3_CheckedChanged(sender As Object, e As EventArgs) Handles ls3.CheckedChangedIf ls3.Checked = True Thenpen.DashStyle = DashStyle.DashEnd IfEnd SubPrivate Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Clickfunc = 3End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Clickfunc = 4End SubPrivate Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Clickfunc = 5End SubPrivate Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Clickfunc = 6End SubPrivate Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Clickfunc = 7End SubPrivate Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Clickfunc = 8s = InputBox("输入文字", "自定义白板")End SubPrivate Sub Button5_MouseDown(sender As Object, e As MouseEventArgs) Handles Button5.MouseDownIf e.Button = MouseButtons.Right ThenIf MsgBox("确认清屏!", vbInformation + vbYesNo, "自定义白板") = MsgBoxResult.Yes Theng1.Clear(Color.Transparent)Pic.Image = penImgEnd IfEnd IfEnd SubPublic Function PointList(p1 As Point, p2 As Point) As RectangleDim p3 As PointDim p4 As PointDim width As IntegerDim height As IntegerDim LeftTop As PointIf p1.X < p2.X AndAlso p1.Y < p2.Y Thenp3 = New Point(p2.X, p1.X)p4 = New Point(p1.X, p2.Y)width = p3.X - p1.Xheight = p4.Y - p1.YLeftTop = p1ElseIf p1.X > p2.X AndAlso p1.Y < p2.Y Thenp3 = New Point(p1.X, p2.Y)p4 = New Point(p2.X, p1.Y)width = p1.X - p4.Xheight = p2.Y - p4.YLeftTop = p4ElseIf p1.X > p2.X AndAlso p1.Y > p2.Y Thenp3 = New Point(p1.X, p2.X)p4 = New Point(p2.X, p1.Y)width = p3.X - p2.Xheight = p4.Y - p2.YLeftTop = p2ElseIf p1.X < p2.X AndAlso p1.Y > p2.Y Thenp3 = New Point(p2.X, p1.Y)p4 = New Point(p1.X, p2.Y)width = p2.X - p4.Xheight = p1.Y - p4.YLeftTop = p4End IfReturn New Rectangle(LeftTop, New Size(width, height))End FunctionDim TxtFont As New Font("微软雅黑", 30, FontStyle.Regular)Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.ClickDim fdlg As New FontDialogIf fdlg.ShowDialog() = DialogResult.OK ThenTxtFont = fdlg.FontEnd IfEnd SubPrivate Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.ClickPanel3.Visible = FalseEnd SubPrivate Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.ClickPanel3.Visible = TruePanel3.Location = New Point(Button8.Location.X + Panel2.Location.X, Button8.Location.Y + Panel2.Location.Y - 250)End SubPrivate Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.ClickButton6.PerformClick()End SubPublic Function PointListT(p1 As Point, p2 As Point) As RectangleDim p3 As PointDim p4 As PointDim width As IntegerDim height As IntegerDim LeftTop As PointIf p1.X < p2.X AndAlso p1.Y < p2.Y Thenp3 = New Point(p2.X, p1.X)p4 = New Point(p1.X, p2.Y)width = p3.X - p1.Xheight = widthLeftTop = p1ElseIf p1.X > p2.X AndAlso p1.Y < p2.Y Thenp3 = New Point(p1.X, p2.Y)p4 = New Point(p2.X, p1.Y)width = p1.X - p4.Xheight = widthLeftTop = p4ElseIf p1.X > p2.X AndAlso p1.Y > p2.Y Thenp3 = New Point(p1.X, p2.X)p4 = New Point(p2.X, p1.Y)width = p3.X - p2.Xheight = widthLeftTop = New Point(p1.X - width, p1.Y - width)ElseIf p1.X < p2.X AndAlso p1.Y > p2.Y Thenp3 = New Point(p2.X, p1.Y)p4 = New Point(p1.X, p2.Y)width = p2.X - p4.Xheight = widthLeftTop = New Point(p1.X, p1.Y - width)End IfReturn New Rectangle(LeftTop, New Size(width, height))End FunctionPrivate Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClickPic.Location = New Point(0, 0)End SubDim Filled As BooleanDim LineStyle As Integer = 0End Class
源代码文件在这:camera类其实没有用,你们自己使用可以删掉它
链接:https://pan.baidu.com/s/1Q_TAzBGJ5FtjE3UHJuvYqw?pwd=1234
提取码:1234