在VB.NET中将JPEG转换为PNG(保存的输出没有扩展名)

时间:2012-11-28 02:21:16

标签: vb.net png jpeg converter

这是我转换图片的代码

Public Sub ConvertImage(ByVal Filename As String, _
      ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, _
      ByVal NewFilename As String)
        NewFilename = "ConvertedToPNG-" + NewFilename + "-" + Format(Date.Now, "MMMMddyyyyhhmmtt")
        Try
            Dim imgFile As System.Drawing.Image = _
              System.Drawing.Image.FromFile(Filename)
            imgFile.Save(txtPNGFileDestination.Text & "\" & NewFilename, DesiredFormat)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

它已正常运行,但保存的图像没有PNG文件扩展名,因此只是一个文件。我做错了吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

只需将扩展名添加到NewFileName即可。您还需要声明NewFileName“ByRef”:

Public Sub ConvertImage(ByVal Filename As String, ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, ByRef NewFilename As String)
  NewFilename = "ConvertedToPNG-" + NewFilename + "-" + Format(Date.Now, "MMMMddyyyyhhmmtt") & "." & DesiredFormat.ToString