实例介绍
【实例简介】
【实例截图】
【核心代码】
Imports System.IO Imports System.Collections Public Class ImageViewer ' private member variables Private mFolder As String Private mImageList As ArrayList Private mImagePosition As Integer Private mPreviousImage As Image Private mCurrentImage As Image Private mNextImage As Image ''' <summary> ''' Default Constructor ''' </summary> ''' <remarks></remarks> Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End Sub ''' <summary> ''' Default load event handler ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub ImageViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' do nothing End Sub ''' <summary> ''' Open the folder browse dialog and load ''' the initial images ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click ' Label the folder browser dialog Me.folderBrowserDialog1.Description = "Select the image directory." ' disable create folder feature of folder browser Me.folderBrowserDialog1.ShowNewFolderButton = False ' display the folder browser Dim result As DialogResult = folderBrowserDialog1.ShowDialog() If result = DialogResult.OK Then ' on okay, set the image folder to the ' folder browser's selected path mFolder = folderBrowserDialog1.SelectedPath If (Not String.IsNullOrEmpty(mFolder)) Then txtImageDirectory.Text = mFolder Else ' exit if the user cancels Return End If ' initialize the image arraylist mImageList = New ArrayList() ' loop through the image directory ' and find all of the image files ' add the found files to the image ' list - add other image types if ' necessary Dim Dir As New DirectoryInfo(mFolder) Dim f As FileInfo For Each f In Dir.GetFiles("*.*") Select Case (f.Extension.ToUpper()) Case ".JPG" mImageList.Add(f.FullName) Case ".BMP" mImageList.Add(f.FullName) Case ".GIF" mImageList.Add(f.FullName) Case Else ' skip file End Select Next ' set the starting position to 0 ' and call the set images method ' to load the pictures mImagePosition = 0 SetImages() End If End Sub ''' <summary> ''' This function is used to set the previous, ''' current, and next images into the ''' correct picture boxes ''' </summary> ''' <remarks></remarks> Private Sub SetImages() ' clear any existing images ' memory will be an issue if ' cycling through a large ' number of images mPreviousImage = Nothing mNextImage = Nothing mCurrentImage = Nothing ' set the previous image If mImagePosition > 0 Then Try ' set delegate Dim prevCallback As Image.GetThumbnailImageAbort = _ New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) ' get the previous image Dim prevBmp As _ New Bitmap(mImageList(mImagePosition - 1).ToString()) ' thumbnail the image to the size ' of the picture box mPreviousImage = _ prevBmp.GetThumbnailImage(160, 100, _ prevCallback, IntPtr.Zero) ' set the picture box image picPreviousImage.Image = mPreviousImage ' clear everything out prevBmp = Nothing prevCallback = Nothing mPreviousImage = Nothing Catch 'stall if it hangs, the user can retry End Try Else ' at the limit clear the ' image picPreviousImage.Image = Nothing End If ' set current image If mImagePosition < mImageList.Count Then Try ' set delegate Dim currentCallback = _ New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) ' get the current image Dim currentBmp As _ New Bitmap(mImageList(mImagePosition).ToString()) ' thumbnail the image to the size ' of the picture box mCurrentImage = _ currentBmp.GetThumbnailImage(320, 200, currentCallback, IntPtr.Zero) ' set the picture box image picCurrentImage.Image = mCurrentImage ' clear everything out currentBmp = Nothing mCurrentImage = Nothing currentCallback = Nothing Catch 'stall if it hangs, the user can retry End Try End If ' set next image If mImagePosition < mImageList.Count - 1 Then Try ' set delegate Dim nextCallback As _ New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) ' get the next image Dim nextBmp As _ New Bitmap(mImageList(mImagePosition 1).ToString()) ' thumbnail the image to the size ' of the picture box mNextImage = _ nextBmp.GetThumbnailImage(160, 100, nextCallback, IntPtr.Zero) ' set the picture box image picNextImage.Image = mNextImage ' clear everything out nextBmp = Nothing nextCallback = Nothing mNextImage = Nothing Catch 'stall if it hangs, the user can retry End Try Else ' at the limit clear the ' image picNextImage.Image = Nothing End If ' call for garbage collection GC.Collect() End Sub ''' <summary> ''' Thumbnail image abort target ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function ThumbnailCallback() As Boolean Return False End Function ''' <summary> ''' Set the image position and reset all ''' of the images when the previous image ''' button is clicked ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnPreviousImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousImage.Click If (mImagePosition >= 0 And Not picPreviousImage.Image Is Nothing) Then mImagePosition -= 1 SetImages() End If End Sub ''' <summary> ''' Set the image position and reset all ''' of the images when the next image ''' button is clicked ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnNextImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextImage.Click If (mImagePosition <= (mImageList.Count - 2)) Then mImagePosition = 1 SetImages() End If End Sub ''' <summary> ''' Open the current image using the default ''' program per the user's file associations ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnOpenImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenImage.Click System.Diagnostics.Process.Start(mImageList(mImagePosition).ToString()) End Sub End Class
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论