Vb.net 图像位置赢了';无法写入文件和图像,因此无法';无法从文件位置加载
我想让程序将一个图像位置写入一个文本文件,然后当用户按下“加载”按钮时,它读取该图像位置并将其设置为PictureBox的图像,但到目前为止我还没有成功Vb.net 图像位置赢了';无法写入文件和图像,因此无法';无法从文件位置加载,vb.net,string,image,object,Vb.net,String,Image,Object,我想让程序将一个图像位置写入一个文本文件,然后当用户按下“加载”按钮时,它读取该图像位置并将其设置为PictureBox的图像,但到目前为止我还没有成功 Private Sub Btn_Save_Click(sender As Object, e As EventArgs) Handles Btn_Save.Click Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Li
Private Sub Btn_Save_Click(sender As Object, e As EventArgs) Handles Btn_Save.Click
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "config_card.aygo"
Dim path2 As String = My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "set_cardimg.aygo"
' Create or overwrite the file.
Dim fs As FileStream = File.Create(path)
Dim fs2 As FileStream = File.Create(path2)
' Add text to the file.
Dim info As Byte() =
New UTF8Encoding(True).GetBytes(
"----------Saved Card Settings----------" + vbNewLine +
"Level: " + My.Settings.Level.ToString + vbNewLine +
"NoMonster: " + My.Settings.NoMonster.ToString + vbNewLine +
"Spell: " + My.Settings.Spell.ToString + vbNewLine +
"Trap: " + My.Settings.Trap.ToString + vbNewLine +
"XYZLevel: " + My.Settings.XyzLevel.ToString + vbNewLine +
"ATKValue: " + My.Settings.ATKValue.ToString + vbNewLine +
"DEFValue: " + My.Settings.DEFValue.ToString + vbNewLine +
"AttributeID: " + My.Settings.AttributeID.ToString + vbNewLine +
"CardID: " + My.Settings.CardID.ToString)
fs.Write(info, 0, info.Length)
fs.Close()
Dim info2 As Byte() =
New UTF8Encoding(True).GetBytes(CardImage.InitialImage.ToString)
fs2.Write(info2, 0, info2.Length)
fs2.Close()
MsgBox("Configuration saved successfully!", vbInformation)
End Sub
Private Sub Btn_Load_Click(sender As Object, e As EventArgs) Handles Btn_Load.Click
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "config_card.aygo"
Try
My.Settings.Level = CInt(GetSettingItem(path, "level"))
My.Settings.NoMonster = CInt(GetSettingItem(path, "nomonster"))
My.Settings.Spell = CBool(GetSettingItem(path, "spell"))
My.Settings.Trap = CBool(GetSettingItem(path, "trap"))
If My.Settings.NoMonster = 1 Then
If My.Settings.Spell = True Then
CardFt.Card_Spell()
Else
If My.Settings.Trap = True Then
CardFt.Card_Trap()
Else
CardFt.Card_Legendary()
End If
End If
End If
My.Settings.XyzLevel = CInt(GetSettingItem(path, "xyzlevel"))
If My.Settings.XyzLevel = 1 Then
CardFt.Card_XYZ()
End If
My.Settings.ATKValue = GetSettingItem(path, "atkvalue")
ATKText.Text = GetSettingItem(path, "atkvalue")
My.Settings.DEFValue = GetSettingItem(path, "defvalue")
DEFText.Text = GetSettingItem(path, "defvalue")
My.Settings.AttributeID = CInt(GetSettingItem(path, "attributeid"))
If My.Settings.AttributeID = 1 Then
AttributeLayer.Image = My.Resources.Earth
ElseIf My.Settings.AttributeID = 2 Then
AttributeLayer.Image = My.Resources.Water
ElseIf My.Settings.AttributeID = 3 Then
AttributeLayer.Image = My.Resources.Fire
ElseIf My.Settings.AttributeID = 4 Then
AttributeLayer.Image = My.Resources.Wind
ElseIf My.Settings.AttributeID = 5 Then
AttributeLayer.Image = My.Resources.Dark
ElseIf My.Settings.AttributeID = 6 Then
AttributeLayer.Image = My.Resources.Light
ElseIf My.Settings.AttributeID = 7 Then
AttributeLayer.Image = My.Resources.Divine
End If
My.Settings.CardID = CInt(GetSettingItem(path, "cardid"))
CardFt.Card_Loader()
If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "set_cardimg.aygo") Then
Try
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "set_cardimg.aygo")
Catch ex As Exception : End Try
End If
Dim bitmap As New Bitmap(My.Computer.FileSystem.SpecialDirectories.MyPictures + "Card Library\Configs\" + "set_cardimg.aygo")
CardImage.Image = CType(bitmap, System.Drawing.Image)
Catch ex As Exception
MsgBox("An error occured while loading the configuration file: " & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.ToString, vbExclamation)
My.Computer.Clipboard.SetText(ex.ToString)
End Try
End Sub
我从中得到的错误是:
System.ArgumentException:参数无效。
位于System.Drawing.Bitmap..ctor(字符串文件名)
在AnimeYuGiOhCardMaker.CardMaker.Btn_Load_单击C:\Users\Compusys\Documents\visualstudio 2012\Projects\Anime Yu Gi Oh Card Maker\Anime Yu Gi Oh Card Maker\Form1中的(对象发送者,事件参数e)。vb:第521行
现在,当按下“保存”按钮时,它不会将图像位置写入文件,但会写入以下内容:
System.Drawing.Bitmap
这就是为什么我得到上面的错误。
实际误差如下所示:
将位图变暗为新位图(My.Computer.FileSystem.SpecialDirectories.MyPictures+“卡库\Configs\”+“设置\u cardimg.aygo”)
CardImage.Image=CType(位图、系统图、图像)
即使使用实际的文件路径,也会发生此错误
我尝试了几种不同的方法,但没有一种有效。任何帮助都将不胜感激。谢谢
--
Dom原始问题已更改多次,包括例外情况。目前的问题有几个问题,主要是:
If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Card Library\Configs\" + "set_cardimg.aygo") Then
Try
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyPictures _
+ "\Card Library\Configs\" + "set_cardimg.aygo")
' EMPTY CATCH !!!!!!
Catch ex As Exception : End Try
End If
Dim bitmap As New Bitmap(My.Computer.FileSystem.SpecialDirectories.MyPictures _
+ "Card Library\Configs\" + "set_cardimg.aygo")
CardImage.Image = CType(bitmap, System.Drawing.Image)
set\u cardimg.aygo
只是一个包含一些文本的配置文件。它不是有效的图像文件,因此无法从中创建位图
您应该打开该文件,将内容读入一个变量,然后如果它是一个有效位置,则从中创建位图,或者最好设置picturebox.location,让它加载图像,而不必先创建位图。为什么要手动将
设置写入文本文件?错误在哪一行?你不能告诉我从My.Settings.AttributeID
还原哪个图像吗?@puropoix我正在手动使用设置,因为我不想保存内置设置,所以我必须使用外部文件。该错误作为异常出现在Catch ex中的MsgBox上。My.Settings.AttributeID图像存储在My.Resources中,因此它们工作正常,My.Settings.CardID也工作正常。但是,CardImage(它是一个Picturebox)加载用户选择的图像,因此它是外部的,我需要能够在用户加载时将该文件路径写入文本文件和vise verse,将从该文件还原/加载图像path@Plutonix我刚刚更新了这篇文章,你可能想再看一遍。a)捕获中没有异常-这正是你的代码报告它的地方b)由于设置可能会自动保存,只需为文件位置添加一个用户设置。您不断更改(不更新)帖子,甚至错误也已更改,但“set\u cardimg.aygo”
不是有效位图的名称-这是您尝试创建的设置文件的名称。您还有一个空的Catch。我将其设置为在关机时不保存My.Settings,因为我不希望出于特定原因保存它们,但是如果用户确实希望保存它,则我希望它保存到外部文件中,以便在启动时将My.Settings重置为默认值。我有一个想法,有没有办法将文件从Picturebox保存到该位置,然后将保存在该位置的实际图像加载回程序?你说空捕获是什么意思?我理解,所以直接加载文件就容易多了。我想我现在可以使用它了,非常感谢你向我澄清这一点。不管怎样,.aygo
中的文件名必须存在,所以请进行测试。但至于加载,我看不出在PBox为您完成任务时手动创建位图有什么好处,并且在完成任务时正确地处理资源。那么我想Picturebox最好这样做,对吗?