65.9K
CodeProject 正在变化。 阅读更多。
Home

批量图像格式转换器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.62/5 (4投票s)

2011年6月4日

CPOL

1分钟阅读

viewsIcon

30677

downloadIcon

2961

将不同格式的图片转换为所需的格式,例如,BMP、JPG、PNG 等格式的图片可以转换为选定的格式(例如 tiff)。

引言

本文档将指导您创建一个应用程序,该应用程序可以通过从组合框中选择所需格式,将不同格式的图像转换为您想要的格式。也就是说,BMP、JPG、GIF、PNG 等不同格式的图像可以根据您的要求同时转换为 BMP、JPG 或 GIF 格式。通过本文档,您将能够转换图像格式,而不会影响图像的其他属性。为此,从资源管理器中拖动所有要转换的图像,然后将其放到左侧框中。它只会接受图像文件,如果您拖动并放置其他文件,则不会接受。同样,如果它已经被接受,则不会再次接受。

Using the Code

以下是主要源代码

以下子程序将图像从一种格式转换为另一种格式

Private Sub ImageFormat(ByVal strr As String)

        Dim bm As New Bitmap(strr)
        Dim fileinfo As Image = Image.FromFile(strr)
        Dim i As Integer

        Dim bmname As String = ""
        Dim c As Char = Nothing

        For i = 4 To Len(strr)
            c = Mid(strr, Len(strr) - i, 1)
            If c = Char.Parse("\") Then
                Exit For
            End If
            bmname = bmname + c
        Next

        bmname = mypicturefolder & "\" & StrReverse(bmname)

        Dim width As Integer = fileinfo.Width   'image width.
        Dim height As Integer = fileinfo.Height   'image height

        Dim thumb As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(thumb)

        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(bm, New Rectangle(0, 0, width, height), _
        	New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
        g.Dispose()

        Try
            Select Case ComboBox1.SelectedIndex
                Case 0
                    Exit Sub
                Case 1
                    thumb.Save(bmname & ".bmp", Imaging.ImageFormat.Bmp)
                Case 2
                    thumb.Save(bmname & ".jpg", Imaging.ImageFormat.Jpeg)
                Case 3
                    thumb.Save(bmname & ".gif", Imaging.ImageFormat.Gif)
                Case 4
                    thumb.Save(bmname & ".ico", Imaging.ImageFormat.Icon)
                Case 5
                    thumb.Save(bmname & ".png", Imaging.ImageFormat.Png)
                Case 6
                    thumb.Save(bmname & ".tiff", Imaging.ImageFormat.Tiff)
                Case 7
                    thumb.Save(bmname & ".wmf", Imaging.ImageFormat.Wmf)
            End Select
            CheckedListBox1.Items.Add(bmname & "." & _
            	Strings.LCase(ComboBox1.Items(ComboBox1.SelectedIndex)), True)
        Catch ex As Exception
            CheckedListBox1.Items.Add(bmname & "." & _
            	ComboBox1.Items(ComboBox1.SelectedIndex), False)
        End Try

        bm.Dispose()
        thumb.Dispose()

    End Sub

...

以下代码从注册表中访问“我的图片”目录的路径,以保存转换后的图像

       Dim rk1 As Microsoft.Win32.RegistryKey
       Dim getval As String
       mypicturefolder = ""
       rk1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey_
	("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", False)
       getval = rk1.GetValue("My Pictures", "NULL")
       rk1.Close()
...

当图像从资源管理器拖放到左侧框中时,它会检查以下内容

  1. 是否为图像文件。如果不是,则不要添加到列表中。
  2. 如果图像文件已添加到列表中,则不要再次添加。

选择要转换的格式,然后单击图像。

以下图像显示了它的工作方式。

© . All rights reserved.