none
save image in picturebox RRS feed

  • Question

  • I put a bunch of pictures in the resource and I choose one of the pictures and I put in picturebox
    When you close the form and not the program as a whole and restore the slot to not appear I want to save images with the
    picturebox

    display code

     IMAGA_.Image = My.Resources.imag0

    Thursday, April 5, 2018 2:44 PM

Answers

  • Restore the slot? You mean open the Form after closing it and have the previous image it displayed in a picturebox displayed again? Or what exactly do you mean? I can't understand what you want. But to continue for no real reason........

    That depends on how you instantiate the Form that has the picturebox on it. That Form could get the image from a public shared variable in the main form if the main forms public shared variable is not nothing.

    Or a user setting could be used such that the Form in question runs and gets the image from a user scoped setting as a string. The string would be the image saved to base64 string. You can convert a byte array to and from a base64 string.

    '________Sample code
    
                Dim b() As Byte = Convert.FromBase64String(My.Settings.BGImageBase64String)
                Using BGImage As New MemoryStream(b)
                    Using Bmp As New Bitmap(Image.FromStream(BGImage))
                        Me.BackgroundImage = CType(Bmp.Clone, Image)
                    End Using
                End Using
            
    '________Sample code
    
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "Image files (*.Bmp, *.Gif, *.Jpg, *.Png, *.Tif|*.Bmp;*.Gif;*.Jpg;*.Png;*.Tif"
                    .Multiselect = False
                    .Title = "Get Background Image"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Me.BackgroundImage = Image.FromFile(OFD.FileName)
                    Using BGImage As New MemoryStream()
                        Me.BackgroundImage.Save(BGImage, System.Drawing.Imaging.ImageFormat.Png)
                        My.Settings.BGImageBase64String = Convert.ToBase64String(BGImage.GetBuffer)
                        My.Settings.Save()
                    End Using
                Else
                    Me.BackgroundImage = Nothing
                    My.Settings.BGImageBase64String = ""
                    My.Settings.Save()
                End If
            End Using

    Also see

    Convert.ToBase64String Method

    Convert.FromBase64String Method (String)


    La vida loca

    • Edited by Mr. Monkeyboy Thursday, April 5, 2018 8:15 PM
    • Proposed as answer by Stanly Fan Monday, April 9, 2018 6:20 AM
    • Marked as answer by monemas Thursday, November 7, 2019 10:07 PM
    Thursday, April 5, 2018 8:12 PM

All replies

  • A form is a strange object in .Net. It is as old as the first Windows. 

    It takes moslty a while before it is released, but if you close it, that starts. 

    If you close a form and want the image back. You have to set it again. 

    Be aware that what you show takes about 0.00000000000000001 seconds. 

    It is just the setting of an address. 


    Success
    Cor


    Thursday, April 5, 2018 3:15 PM
  • Restore the slot? You mean open the Form after closing it and have the previous image it displayed in a picturebox displayed again? Or what exactly do you mean? I can't understand what you want. But to continue for no real reason........

    That depends on how you instantiate the Form that has the picturebox on it. That Form could get the image from a public shared variable in the main form if the main forms public shared variable is not nothing.

    Or a user setting could be used such that the Form in question runs and gets the image from a user scoped setting as a string. The string would be the image saved to base64 string. You can convert a byte array to and from a base64 string.

    '________Sample code
    
                Dim b() As Byte = Convert.FromBase64String(My.Settings.BGImageBase64String)
                Using BGImage As New MemoryStream(b)
                    Using Bmp As New Bitmap(Image.FromStream(BGImage))
                        Me.BackgroundImage = CType(Bmp.Clone, Image)
                    End Using
                End Using
            
    '________Sample code
    
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "Image files (*.Bmp, *.Gif, *.Jpg, *.Png, *.Tif|*.Bmp;*.Gif;*.Jpg;*.Png;*.Tif"
                    .Multiselect = False
                    .Title = "Get Background Image"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Me.BackgroundImage = Image.FromFile(OFD.FileName)
                    Using BGImage As New MemoryStream()
                        Me.BackgroundImage.Save(BGImage, System.Drawing.Imaging.ImageFormat.Png)
                        My.Settings.BGImageBase64String = Convert.ToBase64String(BGImage.GetBuffer)
                        My.Settings.Save()
                    End Using
                Else
                    Me.BackgroundImage = Nothing
                    My.Settings.BGImageBase64String = ""
                    My.Settings.Save()
                End If
            End Using

    Also see

    Convert.ToBase64String Method

    Convert.FromBase64String Method (String)


    La vida loca

    • Edited by Mr. Monkeyboy Thursday, April 5, 2018 8:15 PM
    • Proposed as answer by Stanly Fan Monday, April 9, 2018 6:20 AM
    • Marked as answer by monemas Thursday, November 7, 2019 10:07 PM
    Thursday, April 5, 2018 8:12 PM
  • I put a bunch of pictures in the resource and I choose one of the pictures and I put in picturebox
    When you close the form and not the program as a whole and restore the slot to not appear I want to save images with the
    picturebox

     Could you please explain that a little more,  I don't understand what you mean by "save images with the picturebox".  Do you want to save the images to the hard drive,  or maybe you want to just keep track of the last image that was shown in the picturebox so it is shown the next time,  or maybe something else. 

     We also do not know what a "slot" is and it does not make much sense to us what you mean by that part either.  If you could clear up the explanation of what you mean,  what you want to do,  we could provide the correct answer quicker and easier.


    If you say it can`t be done then i`ll try it

    Thursday, April 5, 2018 8:26 PM
  • If you could clear up the explanation of what you mean,  what you want to do,  we could provide the correct answer quicker and easier.


    If you say it can`t be done then i`ll try it

    Yeah but then we couldn't waste a lot of time/effort and have a lengthy thread for no reason!

    La vida loca

    Thursday, April 5, 2018 8:47 PM