将表单背景图像保存到My.Settings? [英] Save form background image to My.Settings?
问题描述
嘿伙计我在将表单的背景图像保存到某个设置时遇到了麻烦,因此可以在下次打开表单时将其加载回来。现在我有这样的...
<前lang =vb> 昏暗 BGimage 作为 对象
' 打开图像
如果 OpenFileDialog1.ShowDialog()= DialogResult.OK 然后
Form1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
BGimage = Image.FromFile(OpenFileDialog1.FileName)
结束 如果
' 将其保存到My.Settings
My.Settings.BGimage = BGimage
' 在Form1上加载
尝试
Dim a 作为 对象
a = My .Settings.BGimage
我 .BackgroundImage = a
Catch ex 作为例外
结束 尝试
这对我不起作用所以我觉得有人可以帮助我吗?我也尝试过将字符串中的设置更改为System.Drawing.Image,但是你不能这样做,所以我迷失了如何完成这个并且找不到任何关于此的文章。有什么建议吗?
我已经弄明白了。这是我现在的代码。
Dim BGimage 作为 对象
' < span class =code-comment>打开图像
如果 OpenFileDialog1.ShowDialog()= DialogResult.OK 然后
Form1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
BGimage = OpenFileDialog1.FileName.ToString
结束 如果
' 将其保存到My.Settings
My.Settings.BGimage = BGimage
' 在Form1上加载
尝试
我 .BackgroundImage = Image.FromFile(My.Settings.BGimage.ToS tring)
Catch ex As 例外
结束 尝试
您可以将图片转换为字符串并保存在My.Settings中。
我有一个项目,我在那里将图像保存在XML文件中...
这样你就不会需要在所选路径上存在Image。
< pre lang =vb> 公共 共享 功能 GetStringFromImage(image As Image) As String
如果 image IsNot Nothing 然后
Dim ic As 新 ImageConverter()
Dim buffer As Byte ()= < span class =code-keyword> DirectCast (ic.ConvertTo(image, GetType ( Byte ())),字节())
返回 Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks)
否则
返回 没有
结束 如果
结束 功能
公共 共享 Function GetImageFromString(base64String As String ) As 图片
如果 字符串 .IsNullOrWhiteSpace(base64String)< span class =code-keyword>然后 返回 没什么
Dim buffer As Byte ()= Convert.FromBase64String (base64String)
如果 buffer IsNot Nothing 然后
Dim ic As 新 ImageConverter()
返回 TryCa st (ic.ConvertFrom(buffer),Image)
否则
返回 没有
结束 如果
结束 功能
Hey guys I am running into some trouble saving a form''s background image to a setting so it can be loaded back up next time the form is opened. Right now I have it like this...
Dim BGimage As Object
'open the image
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Form1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
BGimage = Image.FromFile(OpenFileDialog1.FileName)
End If
'save it to My.Settings
My.Settings.BGimage = BGimage
'Load it on Form1 load
Try
Dim a As Object
a = My.Settings.BGimage
Me.BackgroundImage = a
Catch ex As Exception
End Try
This isn''t working for me so I figure someone here would be able to help me out? I''ve also tried changing the setting from a String to System.Drawing.Image but you can''t do that so I am lost on how to get this done and can''t find any articles on this. Any suggestions?
I have figured it out. Here is my code now.
Dim BGimage As Object 'open the image If OpenFileDialog1.ShowDialog() = DialogResult.OK Then Form1.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName) BGimage = OpenFileDialog1.FileName.ToString End If 'save it to My.Settings My.Settings.BGimage = BGimage 'Load it on Form1 load Try Me.BackgroundImage = Image.FromFile(My.Settings.BGimage.ToString) Catch ex As Exception End Try
You could convert your Image to String and save that in My.Settings.
I have a Project where I save an Image that way within an XML file...
That way you don''t need that Image existing at the selected Path.
Public Shared Function GetStringFromImage(image As Image) As String If image IsNot Nothing Then Dim ic As New ImageConverter() Dim buffer As Byte() = DirectCast(ic.ConvertTo(image, GetType(Byte())), Byte()) Return Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks) Else Return Nothing End If End Function Public Shared Function GetImageFromString(base64String As String) As Image If String.IsNullOrWhiteSpace(base64String) Then Return Nothing Dim buffer As Byte() = Convert.FromBase64String(base64String) If buffer IsNot Nothing Then Dim ic As New ImageConverter() Return TryCast(ic.ConvertFrom(buffer), Image) Else Return Nothing End If End Function
这篇关于将表单背景图像保存到My.Settings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!