none
save and load image in picturebox RRS feed

  • Question

  • I download a link to a picture from the internet

    PictureBox2.Load("url")

    I want to save the pictures and call them from the Text Box   by saving bytes

    and load from it

    Saturday, March 24, 2018 8:30 PM

Answers

  • Yes saving an Image to Bitmap is the same as saving to byte array as you can open either file in Paint.

    La vida loca

    • Marked as answer by monemas Sunday, March 25, 2018 1:42 PM
    Saturday, March 24, 2018 9:24 PM

All replies

  • Well maybe this. Why you want to use a TextBox I do not understand.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "Image files *.Bmp,*.Gif,*.Png,*.Jpg,*.Tif|*.Bmp;*.Gif;*.Png;*.Jpg;*.Tif"
                    .Multiselect = False
                    .Title = "Load PictureBox1's Image"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Size = Image.FromFile(OFD.FileName).Size
                    PictureBox1.Load(OFD.FileName)
                End If
            End Using
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Using SFD As New SaveFileDialog
                With SFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "Binary files *.Bin|*.Bmp;*.Bin"
                    .Title = "Save PictureBox1's Image to binary file"
                    .AddExtension = True
                    .DefaultExt = ".Bin"
                End With
                If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim PBox1Image As New IO.MemoryStream()
                    PictureBox1.Image.Save(PBox1Image, System.Drawing.Imaging.ImageFormat.Png)
                    Dim PicBox1ByteArray() As Byte = PBox1Image.GetBuffer()
                    My.Computer.FileSystem.WriteAllBytes(SFD.FileName, PicBox1ByteArray, False)
                    PBox1Image.Dispose()
                End If
            End Using
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            PictureBox1.Image = Nothing
            Using OFD As New OpenFileDialog
                With OFD
                    .Filter = "Binary files *.Bin|*.Bmp;*.Bin"
                    .Title = "Save PictureBox1's Image to binary file"
                    .Multiselect = False
                    .Title = "Load PictureBox1's Image from binary file"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Using PBox1Image As New IO.MemoryStream(My.Computer.FileSystem.ReadAllBytes(OFD.FileName))
                        PictureBox1.Size = Image.FromStream(PBox1Image).Size
                        PictureBox1.Image = Image.FromStream(PBox1Image)
                    End Using
                End If
            End Using
        End Sub
    
    End Class
    


    La vida loca

    Saturday, March 24, 2018 8:49 PM
  • I want to save the pictures and call them from the Text Box   by saving bytes

    and load from it

    The Image.Save method will save the image to local storage so that it can be re-loaded.  See:

    https://msdn.microsoft.com/en-us/library/ktx83wah(v=vs.110).aspx

    Saturday, March 24, 2018 8:50 PM
  • Yes saving an Image to Bitmap is the same as saving to byte array as you can open either file in Paint.

    La vida loca

    • Marked as answer by monemas Sunday, March 25, 2018 1:42 PM
    Saturday, March 24, 2018 9:24 PM