程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> 用VB.net2008編寫多種圖片格式轉換程序

用VB.net2008編寫多種圖片格式轉換程序

編輯:VB.NET

Visual Studio 2008與Visual Studio 2005的差別並不是非常大,但是不得不說Visual Studio 2008確實要比之前的Visual Studio 2005更加人性化和實用化了,但是唯一的缺點是Visual Studio2008需要配置比較高的PC才能發揮出真正的效率。對於使用.net開發平台的人們來說,使用Visual Studio2008進行開發是更好的一種選擇,也是一種趨勢。Visual Studio所有系列的產品都附帶了大量的控件,這些控件都擁有非常強大的功能。本文將通過一個完整的程序實例來讓讀者了解到編寫一個基本的程序所需要用到的控件和界面的布局等,希望這篇文章對VB.net初學者還是擁有一定經驗的編程人員會有所幫助。

打開 Visual Studio 2008在文件 (File) 菜單上,單擊新建項目 (New Project)。 在新建項目 (New Project) 對話框的模板 (Templates) 窗格中,單擊 Windows 應用程序(Windows Application)。單擊確定 (OK)如圖1。

圖1

選擇Form1窗體,在Form1窗體中添加4個Label控件,屬性設置分別如下

Label1 屬性值:Text 目標源格式: Label2 屬性值:Text 您需要轉換的格式: Label3 屬性值:Text 圖片文件夾 Label4 屬性值:Text 轉換後保存到

添加了Label控件後,我們還需要在窗體中添加2個ComboBox控件。注意兩個ComboBox我們都用默認名稱(ComboBox1,ComboBox2)分別對應Label1(目標源格式:)和Label2(您需要轉換的格式:)如圖2

圖 2

繼續在Form1窗體中添加兩個Textbox、三個Button、一個GroupBox1控件。屬性設置分別如下:

Textbox1 屬性值:Name: PictureFolder Textbox2 屬性值:Name: NeedSaveFolder Button1 屬性值:Name: SelectImage 屬性值:Textbox 選擇 Button2 屬性值:Name: SelectSave 屬性值:Textbox 選擇 Button3 屬性值:Name: SelectConvert   屬性值:Textbox 轉換 GroupBox1 屬性值:Textbox 文件夾選擇格式

,我們需要把一些控件移動到GroupBox1控件中,這樣可以更好地去進行管理。適當地去調整控件的位置如圖3

圖3

最後一步我們需要放入輸出結果的文本來提示用戶已經完成。需要在Form1中添加一個GroupBox2控件、一個Listbox控件即可。相關屬性設置如下:

GroupBox2 屬性Textbox: 轉換後的結果 Listbox 屬性Name: Result

好了基本上界面的工作已經完成了,結果如圖4

圖 4

界面工作基本已經完成了,但是我們還沒有輸入任何代碼現在的程序是空的。接下來我們需要輸入代碼。雙擊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
End Sub

圖5

選擇<編輯項> 圖6

圖 6

然後輸入:

BMP

EMF

GIF

JPG

PNG

ComboBox2,同樣按照以上步驟輸入數據項,輸入以下即可

BMP

EMF

GIF

JPG

PNG

好了現在我們運行程序測試一下吧,測試如圖7

圖 7

測試結果如圖8

好了程序運行成功,測試已經完成。可以說這個程序相當的方便,可以任意的去轉換多種圖片的格式。當然有興趣的朋友還可以在程序中添加更多的功能,編寫一個更為強大的多種圖片轉換程序。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved