csharp

Display Byte Array as Image in PictureBox control in Windows Application using C#

Here LaxmiKant has explained with an example, Display Byte Array as Image in PictureBox control in Windows Application using C#

The Image file will be selected from your system folder using the OpenFileDialog control and then the chosen Image file will be converted to Byte Array and then displayed in PictureBox control in Windows Forms (WinForms) Application using C#.

Output

display-byte-array-image-picturebox-control-windows-application-using-c

In this article i will explained with an example, Display Byte Array as Image in PictureBox control in Windows Application using C#

Use Of OpenFileDialog

OpenFieDialog : An OpenFileDialog in C# is a control that allows us to browse and select files on a computer in an application.

Step 1: Open visual studio 2017 and click on Project.

display-byte-array-image-picturebox-control-windows-application-using-c

Ste 2: Select Windows Form Application and write the name of Application.

display-byte-array-image-picturebox-control-windows-application-using-c

Step 3: In the form Drag and Drop One Button, PictureBox and Label Control from ToolBox.

display-byte-array-image-picturebox-control-windows-application-using-c

Ste 4: Change the label name of button and label control.

display-byte-array-image-picturebox-control-windows-application-using-c

display-byte-array-image-picturebox-control-windows-application-using-c

Form Design

display-byte-array-image-picturebox-control-windows-application-using-c

Step 5: Import this Namespaces

using System.IO;

Open Form1.cs Page and write this function for Display Byte Array as Image in PictureBox control in Windows Application and call this function on button click event control.

When the Choose File Button is clicked, the OpenFileDialog is opened and the Image file is selected.The selected Image file is first converted to Byte Array and then displayed in PictureBox control.


        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            uploadImage();
        }
        public void uploadImage()
        {

             OpenFileDialog openFileDialog1 = new OpenFileDialog();
             if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    lblImageName.Text = Path.GetFileName(openFileDialog1.FileName);

                    //Read Image File into Image object.
                    Image img = Image.FromFile(openFileDialog1.FileName);

                    //ImageConverter Class convert Image object to Byte Array.
                    byte[] bytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));

                    //Convert Byte Array to Image and display in PictureBox.
                    pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
                }
            

        }

Compplete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace PictureBoxDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            uploadImage();
        }

        public void uploadImage()
        {

             OpenFileDialog openFileDialog1 = new OpenFileDialog();
             if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    lblImageName.Text = Path.GetFileName(openFileDialog1.FileName);

                    //Read Image File into Image object.
                    Image img = Image.FromFile(openFileDialog1.FileName);

                    //ImageConverter Class convert Image object to Byte Array.
                    byte[] bytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));

                    //Convert Byte Array to Image and display in PictureBox.
                    pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
                }
            

        }
    }
}

Any Question and suggestion related to this article please comment me.

Leave a Reply

Your email address will not be published. Required fields are marked *