用VB.net2008编写多种图片格式转换程序
2008-07-05 来源: 浏览次数 12
界面工作基本已经完成了,但是我们还没有输入任何代码现在的程序是空的。接下来我们需要输入代码。双击Form1窗体进入代码编辑器。
首先需要进入常规-声明事件:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Private origGS, destGS As String '这里包括源格式与目标格式
Private fileList() As String '列出图片
Private filePath As String '目标的位置
Private filejs As Integer '计数器
Private fileFormat As ImageFormat '目标的一种格式
Private Function getName(ByVal ftName As String) As String
Dim fileGETName As String = ftName.Substring(ftName.LastIndexOf("\") + 1)
Dim returnJG As String = fileGETName.Substring(0, fileGETName.LastIndexOf("."))
Return returnJG
End Function
进入ComboBox1_SelectedIndexChanged事件中
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.origGS = Me.ComboBox1.SelectedItem
End Sub
进入ComboBox2_SelectedIndexChanged事件中
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Me.destGS = Me.ComboBox2.SelectedItem
Select Case Me.destGS.ToUpper
Case "BMP" : fileFormat = ImageFormat.Bmp
Case "EMF" : fileFormat = ImageFormat.Emf
Case "GIF" : fileFormat = ImageFormat.Gif
Case "JPG" : fileFormat = ImageFormat.Jpeg
Case "PNG" : fileFormat = ImageFormat.Png
End Select
End Sub
进入SelectImage_Click事件中
Private Sub SelectImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectImage.Click
If origGS = String.Empty Then
MessageBox.Show("请先选择图片源格式!", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.ComboBox1.Focus()
Exit Sub
End If
Dim dg As New OpenFileDialog
With dg
.Title = "选择图片"
.Filter = String.Format("所有 {0} 图片|*." & origGS, origGS)
.RestoreDirectory = True
.ValidateNames = True
.InitialDirectory = System.Environment.SpecialFolder.MyPictures
.Multiselect = True
.CheckFileExists = True
.CheckPathExists = True
End With
If dg.ShowDialog <> Windows.Forms.DialogResult.OK Then
Exit Sub
End If
fileList = dg.FileNames
Me.PictureFolder.Text = IO.Path.GetDirectoryName(fileList(0))
End Sub
进入SelectSave_Click事件中
Private Sub SelectSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectSave.Click
If destGS = String.Empty Then
MessageBox.Show("抱歉,需要先选择图片目标格式", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.ComboBox2.Focus()
Exit Sub
End If
Dim dg As New FolderBrowserDialog
dg.Description = "将转换后的图片保存到您所需要的位置:"
dg.ShowDialog()
Me.filePath = dg.SelectedPath
Me.NeedSaveFolder.Text = Me.filePath
End Sub
进入Button3_Click事件中
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Result.Items.Clear()
'判断用户是否已经选择相应的目录,任意一个为空,则不能转换
If Me.PictureFolder.Text <> String.Empty And _
Me.NeedSaveFolder.Text <> String.Empty Then
'源格式和目标格式是否相同
If Me.destGS = Me.origGS Then
MessageBox.Show("抱歉,目标格式不能与源格式相同", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
'开始转换
Dim saveGssName As String = String.Empty '保存为新的文件名
For Each file As String In fileList
Try
Dim liTem As Image = Image.FromFile(file)
saveGssName = String.Format("{0}\{1}.{2}", Me.filePath, getName(file), Me.destGS)
'开始转换
liTem.Save(saveGssName, fileFormat)
filejs += 1
'记录转换信息
Result.Items.Add(String.Format("已经成功转换了 {0}!", saveGssName))
Catch ex As FileNotFoundException
Result.Items.Add(String.Format("已经转换 {0} 失败! 原因: 源文件错误", saveGssName))
Catch ex As Exception
Result.Items.Add(String.Format("已经转换 {0} 失败! 原因: {1}!", saveGssName, ex.ToString))
End Try
Next
Dim Msg As String = String.Format("转换已经完成!", fileList.Length)
MessageBox.Show(Msg, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
Result.Items.Add(String.Empty)
Result.Items.Add(String.Format("一共成功转换了 {0} 个图片! ", Me.filejs.ToString))
Me.filejs = 0 '在这里计数器需要清零
Exit Sub
Else
Exit Sub
End If
上一篇:用VB.NET2008编写..
下一篇:VB.NET公共运行时..

