VB保存带路径的img文件将文件夹名称添加到文件名

时间:2015-08-25 11:52:06

标签: asp.net vb.net parsing

您好我在使用Visual Basic保存img文件时遇到一些问题,文件名称错误,文件名被添加到文件名的开头。

我解析一个网址,然后使用拆分地址值重命名我的文件,但路径值似乎也被添加到文件中。

照片中的文件应命名为例如" DCAT040iMBE Test13.jpg" 但是这个文件是名字" Test1DCAT040iMBE Test13.jpg"

enter image description here

Protected Sub GeneratedCode()
    Dim path As String = "C:\Users\Grey\Documents\visual studio 2010\Projects\QRCodeGenerator\QRCodeGenerator\Output\"
    LogoUpload.SaveAs(path + LogoUpload.FileName)
    TextFile.SaveAs(path + TextFile.FileName)
    Dim lines() As String = IO.File.ReadAllLines(path + TextFile.FileName)
    For Each line As String In lines

        Dim count As Integer
        Dim encoder As New QRCodeEncoder()
        encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H
        encoder.QRCodeScale = 10

        Dim img As Bitmap = encoder.Encode(line)
        Dim logo As System.Drawing.Image = System.Drawing.Image.FromFile(path + LogoUpload.FileName)
        Dim left As Integer = (img.Width / 2) - (logo.Width / 2)
        Dim top As Integer = (img.Height / 2) - (logo.Height / 2)
        Dim g As Graphics = Graphics.FromImage(img)

        Dim parseLine = line
        Dim replaceDelimiter As String
        If Not String.IsNullOrWhiteSpace(line) Then
            replaceDelimiter = Replace(line, "&", "=")
        End If

        Dim fileNameSplit() As String = replaceDelimiter.Split("=")
        Dim newFileName As String

        Dim partTwo = fileNameSplit(1)
        Dim partSix = fileNameSplit(5)

        Dim objFSO
        Dim newFolder As String
        newFolder = "C:\Users\Grey\Documents\visual studio 2010\Projects\QRCodeGenerator\QRCodeGenerator\Output\" + partSix
        objFSO = CreateObject("Scripting.FileSystemObject")
        If (Not System.IO.Directory.Exists(newFolder)) Then
            System.IO.Directory.CreateDirectory(newFolder)
        End If

        count += 1
        g.DrawImage(logo, New Point(left, top))
        newFileName = partTwo & " " & partSix & count & ".jpg"

        img.Save(newFolder + newFileName, ImageFormat.Jpeg)

        amountCreatedLbl.Text = count & " QRCodes Created"
        logo.Dispose()
    Next

End Sub

可能是我错误地生成了newFolder值吗?

编辑

以从解析的txt文件中添加数据示例。

  https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test1
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test2
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test2

    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAT050&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT055iMB&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DCAT040iMBE&Logo=MyLogo&Companyloc=Test3
    https://mywebsite.com/QRCode/default.aspx?materialcode=DTAB060&Logo=MyLogo&Companyloc=Test

1 个答案:

答案 0 :(得分:2)

看起来你在线上缺少斜线:

img.Save(newFolder + newFileName, ImageFormat.Jpeg)

应该是:

img.Save(newFolder + "\" + newFileName, ImageFormat.Jpeg)

程序没有意识到newDirectory变量应该是一个目录,它只是直接连接到文件名。更好的选择是使用:

img.Save(System.IO.Path.Combine(newFolder, newFileName), ImageFormat.Jpeg)

System.IO.Path.Combine()函数会自动在目录和文件名之间添加缺少的斜杠,以及一些额外的检查以确保结果有效。

作为旁注,我还建议使用&将字符串连接在一起时代替+。当你这样做时,很难调试问题。我还建议转动Option Strict On,您会看到其他一些警告,它们会按原样提供您的代码。但是,要解决您的问题,上述方法将有效。