将图像转换为另一种格式异常错误!! [英] convert image to another format exception error !!

查看:146
本文介绍了将图像转换为另一种格式异常错误!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将jpg转换为其他格式的应用程序,但是我得到了异常错误....任何建议,附加的是完整的代码。

I am coding an application that convert jpg to other formats , but im getting exception errors .... any suggestion , attached is the complete code .

Imports System.IO
Imports System.Drawing




Public Class Form1
    Private Class WorkerParameters
        Public Property InputDirectory As String
        Public Property OutputFile As String
    End Class





    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()

    End Sub




    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork


        Dim folders() As String = Directory.GetDirectories(TextBox1.Text, "*.*", SearchOption.AllDirectories)

        For Each FO As String In folders



            If (Directory.GetFiles(FO, "*.jpg").Length = 0) Then


                ListBox1.Items.Add("The Folder Named : " & " " & FO & " " & "Has NO supported images")

            Else




                Dim file As StreamWriter
                file = My.Computer.FileSystem.OpenTextFileWriter(TextBox1.Text & "\" & "LOG.txt", True)
                file.WriteLine("The Folder Named : " & " " & FO & " " & "Contains images and is being processed now")
                file.Close()

            End If

            Dim files() As String = Directory.GetFiles(FO, "*.*", SearchOption.TopDirectoryOnly)

            For Each f As String In files

              
             


                Dim completepath As String = Path.GetDirectoryName(f)


                Dim outputpath As String = completepath.Substring(completepath.LastIndexOf("\") + 1)



                Dim completedestinationflip As String = FolderBrowserDialog2.SelectedPath & "\" & outputpath
                TextBox2.Text = completedestinationflip




                Dim thefilename As String = Path.GetFileNameWithoutExtension(f)


                Dim ora As String = completedestinationflip & "\" & thefilename


                TextBox3.Text = ora




                Directory.CreateDirectory(FolderBrowserDialog2.SelectedPath & "\" & outputpath)

              

            Next

            

            Dim info As New WorkerParameters
            With info
                .InputDirectory = TextBox1.Text
                .OutputFile = TextBox3.Text
            End With


            ' Image Conversion section


                'Function to find compatible file types.

            Dim findMatch = Function(filePath As String)

                                Return filePath.ToLower.EndsWith(".png") Or _
                                       filePath.ToLower.EndsWith(".bmp") Or _
                                       filePath.ToLower.EndsWith(".jpg") Or _
                                       filePath.ToLower.EndsWith(".gif")
                            End Function

                'Find entire array.
                Dim filesToProcess = Array.FindAll(Of String)(files, findMatch)

                'Get file count.
                Dim fileCount As Integer = filesToProcess.Length - 1


                For i As Integer = 0 To fileCount
                Try

             

                Dim image1 As System.Drawing.Image = System.Drawing.Image.FromFile(TextBox3.Text & ".jpg")

                  

                    ' Save the image in JPEG format.
                '    image1.Save(info.OutputFile, System.Drawing.Imaging.ImageFormat.Jpeg)

                ' Save the image in GIF format.
                image1.Save(info.OutputFile & "\" & TextBox3.Text, System.Drawing.Imaging.ImageFormat.Gif)

                ' Save the image in PNG format.
                image1.Save(info.OutputFile & "\" & TextBox3.Text, System.Drawing.Imaging.ImageFormat.Png)






                BackgroundWorker1.ReportProgress((i / fileCount) * 100)

                Catch ex1 As Exception

                    MessageBox.Show(ex1.Message)
                  

                End Try


            Next

                'Erase all items in array. Memory leaking free...
                Erase filesToProcess

             


              


          


            ' end of Converting Section


        Next



    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        lblPercent.Text = e.ProgressPercentage & "%"



    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        MessageBox.Show("Task Done , press ok to view the Complete LOG File", "Any to Any Image Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Process.Start(TextBox1.Text & "\" & "LOG.txt")



    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Control.CheckForIllegalCrossThreadCalls = False

    End Sub

   

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        FolderBrowserDialog2.ShowDialog()
        TextBox2.Text = FolderBrowserDialog2.SelectedPath


    End Sub

    Private Sub ConvertToPDFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConvertToolStripMenuItem.Click

        If (TextBox1.Text = String.Empty) Then

            MessageBox.Show("Path can not be left empty", "Any to Any Iiamge Converter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return

        End If


        If (TextBox2.Text = String.Empty) Then

            MessageBox.Show("No Destination selected !", "Any to Any Iiamge Converter", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return

        End If



        If (TextBox1.Text = TextBox2.Text) Then

            MessageBox.Show("Path and Destination can not be the same", "Any to Any Iiamge Converter", MessageBoxButtons.OK, MessageBoxIcon.Hand)
            Return
        End If


        BackgroundWorker1.RunWorkerAsync()


    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FolderBrowserDialog1.ShowDialog()

        Dim thepath As String = FolderBrowserDialog1.SelectedPath()

        TextBox1.Text = thepath

        ProgressBar1.Value = 0
        lblPercent.Text = "0%"


    End Sub
End Class

Georges Naffah

Georges Naffah

推荐答案

嗨Georges,

Hi Georges,

根据您的描述,您可以将jpg图像保存为其他格式。

According to your description, you would save jpg image to other format.

但是,当您保存图像时。下面的代码让我感到困惑:

However, when you save image. The code as below makes me confused:

image1.Save(info.OutputFile & "\" & TextBox3.Text,System.Drawing.Imaging.ImageFormat.Gif)

TextBox3.Text 只是图像的路径,没有任何格式。

The TextBox3.Text is only path of image which is without any format.

所以我建议您使用特定格式保存图像,并使用
保存()
方法中的第一个参数:

So I suggest you save image with specific format with the first parameter in Save() method:

Dim image1 As System.Drawing.Image = System.Drawing.Image.FromFile("C:\Users\Jjc\Desktop\New folder\jpg\111.jpg")

image1.Save("C:\Users\Jjc\Desktop\New folder\png\123.png", System.Drawing.Imaging.ImageFormat.Png)

请参考以下有关Save()方法的文件:

Please refer to the following document about Save() method:

图片。保存方法(S. tring,ImageFormat)

如果您仍然遇到问题,请提供一些错误消息,以便我能更好地帮助您。

If you still have problem, please provide some error message so that I could help you better.

最诚挚的问候,

Stanly


这篇关于将图像转换为另一种格式异常错误!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆