[VB.NET] 온라인상의 이미지를 다운로드 후 BASE64로 인코딩하고 Picturebox에 표시하기

2023. 10. 16. 20:13컴관련

반응형

웹상의 이미지를 BASE64로 인코딩하기

 

Code

Imports System.Net
Imports System.IO

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim url As String = "https://example.com/image.jpg" ' 다운로드할 이미지의 URL

    Dim client As New WebClient()


    Dim imageBytes As Byte() = client.DownloadData(url)


    Dim base64String As String = Convert.ToBase64String(imageBytes)


    TextBox1.Text = base64String ' base64로 변환된 이미지 데이터를 TextBox에 출력


End Sub


End Class

 

웹상의 이미지를 BASE64로 인코딩하고 Picturebox에 출력하기

 

Example

Used Objects :Textbox1, Textbox2, Picturebox1, Button1, Button2

Test Image : Google Logo
https://www.google.co.kr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

https://www.google.co.kr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

 

Textbox1의 URL에 있는  Image를  다운로드 후 Base64로 인코딩 해서 Textbox2 에 출력.

 

Textbox2의 Base64문자열을  Picturebox1 에 이미지로 보여준다.

 

 

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' 이미지 파일의 URL
        Dim imageUrl As String
        imageUrl = TextBox1.Text

        ' 이미지를 다운로드하고 Base64로 인코딩
        Dim base64String As String = ImageToBase64(imageUrl)

        ' 결과 출력 또는 필요한 처리 수행
        textbox2.text = base64String
    End Sub

'-----------------------------------------------------------

    Private Function ImageToBase64(imageUrl As String) As String
        Try
            ' 이미지 다운로드
            Dim webClient As New System.Net.WebClient()
            Dim imageData As Byte() = webClient.DownloadData(imageUrl)

            ' 이미지 데이터를 Base64로 인코딩
            Dim base64String As String = Convert.ToBase64String(imageData)
            Return base64String

        Catch ex As Exception
            ' 예외 처리
            MessageBox.Show("오류 발생: " & ex.Message)
            Return Nothing

        End Try
    End Function

'=================================================================

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Base64로 인코딩된 이미지 문자열
        Dim base64ImageString As String
        base64ImageString = TextBox2.Text

        ' Base64 문자열을 이미지로 변환하여 PictureBox에 표시
        ShowImageFromBase64(base64ImageString)
    End Sub

'-----------------------------------------------------------

    Private Sub ShowImageFromBase64(base64String As String)
        Try
            ' Base64 문자열을 바이트 배열로 디코딩
            Dim imageData As Byte() = Convert.FromBase64String(base64String)

            ' 바이트 배열을 MemoryStream으로 변환
            Using memoryStream As New MemoryStream(imageData)
                ' MemoryStream에서 이미지 로드
                Dim image As Image = Image.FromStream(memoryStream)

                ' PictureBox에 이미지 표시
                PictureBox1.Image = image
            End Using
        Catch ex As Exception
            ' 예외 처리
            MessageBox.Show("이미지 로드 중 오류 발생: " & ex.Message)
        End Try
    End Sub

End Class

 

반응형