System.ArgumentException:“参数无效。”
我在这条线上看到了这个错误
safecopy.Save(path + "\MainPic\" + fullname + ".png", Imaging.ImageFormat.Png)这是我的密码:
Private Sub btnUploadPhoto_Click(sender As Object, e As EventArgs) Handles btnUploadPhoto.Click
Dim getlastname As String = tboxLastName.Text
Dim getfirstname As String = tboxFirstName.Text
Dim getmidname As String = tboxMiddleName.Text
Dim fullname As String = getlastname + getfirstname + getmidname
Dim pcusername As String = GetUserName()
Dim path As String = "C:\Users\" + pcusername + "\Documents\RMS\Photos" + fullname
If ofdUploadPhotoMain.ShowDialog() = DialogResult.OK Then
PicBoxMain.SizeMode = PictureBoxSizeMode.Zoom
PicBoxMain.ImageLocation = ofdUploadPhotoMain.FileName 'Display in PictureBox
If Not System.IO.Directory.Exists(path) Then
System.IO.Directory.CreateDirectory(path)
End If
Dim safecopy As Bitmap
Dim mainpic As New Bitmap(ofdUploadPhotoMain.FileName)
safecopy = mainpic
mainpic.Dispose()
safecopy.Save(path + "\MainPic\" + fullname + ".png", Imaging.ImageFormat.Png)
safecopy.Dispose()
End If
End Sub我想将图片保存到具有特定文件名的特定文件夹中,而不打开保存文件对话框。
派人来帮忙!
谢谢。
发布于 2020-05-04 02:53:04
很明显,你认为这是在做一些事情,但事实并非如此:
safecopy = mainpic所做的就是将safecopy设置为引用与mainpic相同的Bitmap对象。没有副本。下一行将处理Bitmap对象,因此您当然不能使用它。去掉第二个变量。只需创建对象,保存对象,然后将其释放:
Using pic As New Bitmap(ofdUploadPhotoMain.FileName)
pic.Save(IO.Path.Combine(path, "MainPic", fullname + ".png"), Imaging.ImageFormat.Png)
End Using编辑:
我刚刚测试了这段代码,得到了一个通用的GDI+错误:
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = DialogResult.OK Then
Dim fullName = String.Concat("LastName", "FirstName", "MiddleName")
Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments,
"RMS\Photos\MainPic",
fullName & ".png")
Dim picture = Image.FromFile(ofd.FileName)
picture.Save(filePath)
PictureBox1.Image = picture
End If
End Using在这种情况下,路径中指定的文件夹不存在。我把它改成了这个
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = DialogResult.OK Then
Dim fullName = String.Concat("LastName", "FirstName", "MiddleName")
Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments,
"RMS\Photos\MainPic")
Directory.CreateDirectory(folderPath)
Dim filePath = Path.Combine(folderPath, fullName & ".png")
Dim picture = Image.FromFile(ofd.FileName)
picture.Save(filePath)
PictureBox1.Image = picture
End If
End Using并按预期的方式保存和显示图像。这个问题对你来说可能是一样的。注意我是如何创建路径的。你也该这么做。
https://stackoverflow.com/questions/61584352
复制相似问题