Click here to Skip to main content
15,891,184 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Richard MacCutchan3-Feb-14 8:17
mveRichard MacCutchan3-Feb-14 8:17 
AnswerRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dave Kreskowiak3-Feb-14 4:37
mveDave Kreskowiak3-Feb-14 4:37 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dan O'Riordan3-Feb-14 17:24
Dan O'Riordan3-Feb-14 17:24 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dave Kreskowiak3-Feb-14 18:14
mveDave Kreskowiak3-Feb-14 18:14 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dan O'Riordan3-Feb-14 18:21
Dan O'Riordan3-Feb-14 18:21 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dan O'Riordan4-Feb-14 2:04
Dan O'Riordan4-Feb-14 2:04 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dave Kreskowiak4-Feb-14 3:56
mveDave Kreskowiak4-Feb-14 3:56 
AnswerRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
TnTinMn4-Feb-14 14:28
TnTinMn4-Feb-14 14:28 
Hi Dan,
I have read through your correspondence with the others and I do not disagree at all with what they have been telling you. However, I would like to propose an alternative method based on what you have said.

You want to allow the user to select image to display on your form and you also want this selection to persisted between program executions (i.e. a user setting). VB.Net provides you an easy mechanism to persist variables using the My.Settings feature. You can access and define these settings by double clicking "My Project" in the Solution Explorer window and then clicking on the Settings tab.

The only issue is that the VS designer will not allow you to select "System.Drawing.Image" as the setting Type. Don't worry about that, it is only a limitation in the code generation capability of the designer and not that you can not store an image. The big requirement is that that type that you wish to store provides a mechanism to serialize the class instance. If you look at the documentation for the Image Class[^], you will see that is defined:
XML
'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<TypeConverterAttribute(GetType(ImageConverter))> _
Public MustInherit Class Image _
    Inherits MarshalByRefObject _
    Implements ISerializable, ICloneable, IDisposable
That <SerializableAttribute> is key to making this work. Smile | :) You will just have to provide the code to add the setting yourself. VB.Net handles these settings as Properties of the MySettings Class in the My namespace. Add a new Class file to your project and replace the autogenerated code with the following snippet.
XML
Namespace My
   Partial Friend NotInheritable Class MySettings
      ' The trick here is to tell it serialize as binary
      <Global.System.Configuration.SettingsSerializeAs(Configuration.SettingsSerializeAs.Binary)> _
      <Global.System.Configuration.UserScopedSettingAttribute()> _
      Public Property StoredImage() As System.Drawing.Image
         Get
            ' the String in: Me("StoredImage") must be the name of the setting
            Return DirectCast(Me("StoredImage"), System.Drawing.Image)
         End Get
         Set(ByVal value As System.Drawing.Image)
            ' the String in: Me("StoredImage") must be the name of the setting
            Me("StoredImage") = value
         End Set
      End Property

      ''' <summary>Dictionary of Images keyed on username</summary>
      <Global.System.Configuration.SettingsSerializeAs(Configuration.SettingsSerializeAs.Binary)> _
      <Global.System.Configuration.UserScopedSettingAttribute()> _
      Public Property UserImages() As Dictionary(Of String, System.Drawing.Image)
         Get
            ' the String in: Me("UserImages") must be the name of the setting
            If Me("UserImages") Is Nothing Then
               Me("UserImages") = New Dictionary(Of String, System.Drawing.Image)
            End If
            Return DirectCast(Me("UserImages"), Dictionary(Of String, System.Drawing.Image))
         End Get
         Set(ByVal value As Dictionary(Of String, System.Drawing.Image))
            ' the String in: Me("UserImages") must be the name of the setting
            Me("UserImages") = value
         End Set
      End Property
   End Class
End Namespace
I have defined two Properties (StoredImage and UserImages) because I was not completely clear if your application needed to store a single image or an image for each user of the application under the same installed account. The Dictionary property gives you the ability to store multiple images keyed to a String value.

Here is a sample usage based on a Form with two Buttons and two PictureBoxes. I think you can figure this code out, but feel free to ask questions.

************** Edit: The example usage code I thought that I included. D'Oh! | :doh:
VB
Public Class Form1
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim imgdialog As New System.Windows.Forms.OpenFileDialog()
      imgdialog.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*"
      If imgdialog.ShowDialog = Windows.Forms.DialogResult.OK Then
         Dim img As Image = Image.FromFile(imgdialog.FileName)
         My.Settings.StoredImage = img
         If My.Settings.UserImages.ContainsKey("username") Then
            My.Settings.UserImages("username") = img
         Else
            My.Settings.UserImages.Add("username", img)
         End If
         My.Settings.Save() ' Save the new values.
      End If
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      PictureBox1.Image = My.Settings.StoredImage
      If My.Settings.UserImages.ContainsKey("username") Then
         PictureBox2.Image = My.Settings.UserImages("username")
      End If
   End Sub

End Class
************* End Edit

Some reference material:
To find where the config file is stored see the response from Johan Stenberg[^]
My.Settings Object[^]

modified 5-Feb-14 16:50pm.

GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
Dan O'Riordan4-Feb-14 23:07
Dan O'Riordan4-Feb-14 23:07 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
TnTinMn5-Feb-14 2:31
TnTinMn5-Feb-14 2:31 
GeneralRe: Allowing User To Upload Image to Resources folder and Displaying in PictureBox Pin
TnTinMn5-Feb-14 11:12
TnTinMn5-Feb-14 11:12 
QuestionProblem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
TheComputerMan31-Jan-14 4:54
TheComputerMan31-Jan-14 4:54 
AnswerRe: Problem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
Eddy Vluggen31-Jan-14 6:22
professionalEddy Vluggen31-Jan-14 6:22 
GeneralRe: Problem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
TheComputerMan31-Jan-14 6:26
TheComputerMan31-Jan-14 6:26 
GeneralRe: Problem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
Eddy Vluggen1-Feb-14 7:11
professionalEddy Vluggen1-Feb-14 7:11 
GeneralRe: Problem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
TheComputerMan1-Feb-14 7:19
TheComputerMan1-Feb-14 7:19 
GeneralRe: Problem with AutoHidePortion in DockPanel Suite 2.8.0 (VB2010) Pin
Eddy Vluggen1-Feb-14 8:14
professionalEddy Vluggen1-Feb-14 8:14 
QuestionMaking label on UserControl work as default click event Pin
Floodlight29-Jan-14 17:49
Floodlight29-Jan-14 17:49 
AnswerRe: Making label on UserControl work as default click event Pin
TnTinMn29-Jan-14 18:16
TnTinMn29-Jan-14 18:16 
GeneralRe: Making label on UserControl work as default click event Pin
Floodlight29-Jan-14 18:44
Floodlight29-Jan-14 18:44 
AnswerRe: Making label on UserControl work as default click event Pin
Dave Kreskowiak29-Jan-14 18:17
mveDave Kreskowiak29-Jan-14 18:17 
GeneralRe: Making label on UserControl work as default click event Pin
Floodlight29-Jan-14 18:45
Floodlight29-Jan-14 18:45 
QuestionImplement an Interface in designed DataContext Pin
Rene Rose27-Jan-14 20:06
Rene Rose27-Jan-14 20:06 
SuggestionRe: Implement an Interface in designed DataContext Pin
Richard MacCutchan27-Jan-14 22:20
mveRichard MacCutchan27-Jan-14 22:20 
GeneralRe: Implement an Interface in designed DataContext Pin
Rene Rose27-Jan-14 22:34
Rene Rose27-Jan-14 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.