当照片上传到图片箱时,将图片从图片箱保存到特定文件夹

  • 本文关键字:保存 文件夹 照片 vb.net
  • 更新时间 :
  • 英文 :


系统。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 + "DocumentsRMSPhotos" + 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

我想在不打开保存文件对话框的情况下,将图片保存到具有特定文件名的特定文件夹中。

发送帮助!

谢谢。

您显然认为这是在做一些不该做的事情:

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,
"RMSPhotosMainPic",
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,
"RMSPhotosMainPic")
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

并且图像被保存并按预期显示。这个问题对你来说可能是一样的。请注意我是如何创建路径的。你也应该这样做。

最新更新