How to read a binary file into a byte array and convert it to a string using WinForms in C#

1 Answer

0 votes
namespace WinFormsApp1 
{
    public partial class Form1 : Form
    {
    public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] array = File.ReadAllBytes("D:\\test.jpg");

            string base64string = Convert.ToBase64String(array);

            // Write first 30 chars.
            MessageBox.Show(base64string.Substring(0, 30) + "...");
        }
    }
}



/*
run:

/9j/4AAQSkZJRgABAQAAAQABAAD/2w...
 
*/

 



answered May 27, 2024 by avibootz
...