Load Web image in Picture Box

Gunnar CorreaGunnar Correa25/02/2017 - 15.133 acessos

Loading image from a server into a Picturebox.


Hello guys, are you okay? I hope very well!

Most of our projects need to load images from a server, be it an advertisement, a new software update, or other needs. For this project, we will need the components below.

- PictureBox;

- Button;

- TextField.

Organize your project layout as shown below.

Visual Studio Project layout
Visual Studio Project layout

Image 1: Project layout

Now change the properties as shown in the table below.

 

ComponentProperties

PictureBox

 

Name

pcbImage

SizeMode

CenterImage

 

 

Button

 

Name

btnLoad

Text

Load Image

 

 

TextField

 

Name

txtLoad

 

 

Form

 

Name

frmMain

StartPosition

CenterScreen

ShowIcon

False

MaximizeBox

False

Text

Load Image

 

Now double-click the button and enter the algorithm shown below.

Code 01

WebRequest request = WebRequest.Create(txtLoad.Text); //Initializes an instance with the given URL
            using (var response = request.GetResponse()) //Tries to access the object
            {
                using (var str = response.GetResponseStream()) //Returns the metadata of the image
                {
                    pcbImage.Image = Bitmap.FromStream(str); //Creates a bitmap based on the loaded metadata, in the sequence inserts into the image property.
                }
}

Remember to import the System.NET class.

 

Load image web into picturebox.
Load image web into picturebox.

Image 2: Final result.

I hope you enjoyed it, leave a comment.

 



picturebox c# load web image c# web image picturebox programming in c# bitmap image c# picturebox web load web data server image into picturebox system.net visual studio

Compartilhe nas redes sociais



Gunnar Correa

Gunnar Correa

Autodidata, Graduado em Desenvolvimento Web, Especialista em Tecnologias na Educação e Pós-graduando em Segurança da Informação. Começou seus estudos na área de programação quando era criança, e atualmente, está estudando desenvolvimento de jogos. É CEO do portal SatellaSoft, criado em 2009 com o intuito de compartilhar conhecimento de forma gratuita e inédita.


Fórum

Ficou com alguma dúvida? utilize nosso fórum de perguntas e respostas, lá nossa comunidade poderá te ajudar.

https://forum.satellasoft.com