Form1.vb
上传用户:ysf151
上传日期:2022-07-25
资源大小:14k
文件大小:5k
- Imports System
- Imports System.IO
- Imports System.Threading
- Imports System.Drawing
- Public Class Form1
- ' 局部变量声明
- Private tDrawImag As Thread ' 绘画线程序
- Private DefaultPath As String ' 默认图片路径
- Private DefaultExName As String ' 默认图片文件扩展名
- Private G As Graphics ' 主画笔
- Private IsAbortThread As Boolean ' 是否关闭线程
- ' 委托声明
- Private Delegate Sub DeleDrawImage(ByVal Filename As String) ' 绘画
- Private Delegate Sub DeleCloseWindow() ' 关闭窗口
- ' 事件: 窗体第一次显示时发生
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- ' 初始化局部变量
- Me.DefaultPath = Environment.CurrentDirectory & "images"
- Me.DefaultExName = "GIF,JPG"
- End Sub
- ' 事件: 按扭被点击后发生
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- ' 线程不存在或则不在运行的情况
- If tDrawImag Is Nothing OrElse Not tDrawImag.ThreadState = ThreadState.Running Then
- tDrawImag = New Thread(AddressOf Me.ThreadDraw)
- tDrawImag.Start()
- Return
- End If
- MessageBox.Show("正在绘制,请稍候。。。")
- End Sub
- ' 事件: 窗口准备关闭时发生
- Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- ' 线程存在且在运行的情况
- If Not tDrawImag Is Nothing AndAlso Me.tDrawImag.ThreadState = ThreadState.Running Then
- ' 标记需要关闭线程
- Me.IsAbortThread = True
- ' 取消此此关闭窗口
- e.Cancel = True
- ' 窗口隐藏
- Me.Hide()
- ' 启动关闭窗体线程序
- Dim tClose As New Thread(AddressOf Me.ThreadCloseWindow)
- tClose.Start()
- End If
- End Sub
- ''' <summary>
- ''' 绘制指定目录中的所有指定类型的文件
- ''' </summary>
- ''' <param name="Path">目录</param>
- ''' <param name="ExFilename">文件扩展名</param>
- ''' <remarks></remarks>
- Private Sub DrawImages(ByVal Path As String, ByVal ExFilename As String)
- ' 变量声明
- Dim ExName As String ' 用于保存扩展名
- Dim DrawImageDelegate As DeleDrawImage ' 绘画委托
- ' 修正扩展名类型
- ExFilename = "," & ExFilename.ToUpper() & ","
- ' 获得目录信息
- Dim infoDir As New DirectoryInfo(DefaultPath)
- If Not infoDir.Exists Then : Return : End If
- ' 变量赋值
- Me.G = Me.PictureBox1.CreateGraphics() ' 获得画笔
- DrawImageDelegate = New DeleDrawImage(AddressOf DrawImage)
- ' 循环目录获得所有文件
- For Each infoFile As FileInfo In infoDir.GetFiles()
- ' 需要关闭线程的情况
- If Me.IsAbortThread Then
- Exit For
- End If
- ' 获得小数点后的扩展名字符串
- ExName = "," & infoFile.Extension.Substring(1).ToUpper() & ","
- If Not ExFilename.IndexOf(ExName) = -1 Then
- ' 绘制文件
- Me.Invoke(DrawImageDelegate, New Object() {infoFile.FullName})
- ' 绘制后停留1秒
- 'Thread.Sleep(1000)
- End If
- Next
- ' 释放画笔
- Me.G.Dispose()
- End Sub
- ''' <summary>
- ''' 绘制文件到窗口
- ''' </summary>
- ''' <param name="Filename"></param>
- ''' <remarks></remarks>
- Private Sub DrawImage(ByVal Filename As String)
- ' 获得图象
- Dim myBitmap As New Bitmap(Filename)
- ' 修正图象尺寸
- Dim iX As Double ' 缩放比例
- Dim iWidth As Integer = myBitmap.Width ' 宽度
- Dim iHeight As Integer = myBitmap.Height ' 高度
- ' 图象宽度超过450的情况, 按比例改变图象尺寸
- If iWidth > Me.PictureBox1.Width Then
- iX = Me.PictureBox1.Width / iWidth
- iWidth *= iX
- iHeight *= iX
- End If
- ' 图象高度超过200的情况
- If iHeight > Me.PictureBox1.Height Then
- iX = Me.PictureBox1.Height / iHeight
- iWidth *= iX
- iHeight *= iX
- End If
- ' 绘制
- G.Clear(Me.BackColor)
- G.DrawImage(myBitmap, 0, 0, iWidth, iHeight)
- ' 释放
- myBitmap.Dispose()
- End Sub
- ''' <summary>
- ''' 线程用,开始按默认路径绘画
- ''' </summary>
- ''' <remarks></remarks>
- Private Sub ThreadDraw()
- Console.WriteLine("绘画线程开始")
- Me.DrawImages(Me.DefaultPath, Me.DefaultExName)
- Console.WriteLine("绘画线程退出")
- End Sub
- ''' <summary>
- ''' 线程用,关闭窗口
- ''' </summary>
- ''' <remarks></remarks>
- Private Sub ThreadCloseWindow()
- While Me.tDrawImag.ThreadState = ThreadState.Running
- Thread.Sleep(100)
- End While
- Me.Invoke(New DeleCloseWindow(AddressOf Me.Close))
- End Sub
- End Class