Convert Byte Array to Image File C# and VB.Net

Today, We want to share with you Convert Byte Array to Image File C# and VB.Net.
In this post we will show you MVC : Display Image From Byte Array using C# , hear for c# – Converting a byte array to PNG/JPG we will give you demo and example for implement.
In this post, we will learn about Convert System.Drawing.Image to Byte Array using C# and VB.Net with an example.

Convert Byte Array to Image File C# and VB.Net

There are the Following The simple About Convert Byte Array to Image File C# and VB.Net Full Information With Example and source code.

As I will cover this Post with live Working example to develop convert an image to byte array in, so the some major files and Directory structures for this example is following below.

  • Default.aspx
  • Default.aspx.cs

Convert image to byte array ASP.Net C#


This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as Default.aspx.

<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <title>Convert Byte Array to Image File C# Example</title>
    <form id="form1" runat="server">
            <h2>How to upload image on server in using c#</h2>
            <asp:FileUpload ID="liveUploadFileCtrl" runat="server" />
            <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClick="FileUploadBtn_Click" />
            <br />
            <br />
            <asp:Label runat="server" ID="MessageLabel" Text="Upload Message: " />
            <asp:Image ID="showImg" runat="server"></asp:Image>

c# – How to convert image to byte array


using System;
using System.Drawing;
using System.IO;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void FileUploadBtn_Click(object sender, EventArgs e)
        if (liveUploadFileCtrl.HasFile)
                string filename = Path.GetFileName(liveUploadFileCtrl.FileName);
                liveUploadFileCtrl.SaveAs(Server.MapPath("~/") + filename);

                var myObj = new LiveImgConvertor();

                System.Drawing.Bitmap bmpPostedImage =
                    new System.Drawing.Bitmap(liveUploadFileCtrl.PostedFile.InputStream);

                byte[] memoryImgByteArr = myObj.ConvertImageToByteArray(bmpPostedImage, ".png");
                System.Drawing.Image imageIn = myObj.ByteIntoImgConvert(memoryImgByteArr);

                string saveImagePath = Server.MapPath("~/images/") + "Image1.png";
                File.WriteAllBytes(saveImagePath, memoryImgByteArr);

                MessageLabel.Text = "Upload Message: File uploaded!'";
                MessageLabel.ForeColor =;
            catch (Exception ex)
                MessageLabel.Text = ex.Message;


Convert Image to byte[] array
Convert byte[] array to Image

using System.Drawing;
using System.IO;

public class LiveImgConvertor
    public LiveImgConvertor()
    public Image ByteIntoImgConvert(byte[] totalByteArrIn)
        using (LiveMemStream ms = new LiveMemStream(totalByteArrIn))
            return Image.FromStream(ms);
    public byte[] ConvertImageToByteArray(Image image, string extension)
        using (var liveMemStream = new LiveMemStream())
            switch (extension)
                case ".jpeg":
                case ".jpg":
                    image.Save(liveMemStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                case ".png":
                    image.Save(liveMemStream, System.Drawing.Imaging.ImageFormat.Png);
                case ".gif":
                    image.Save(liveMemStream, System.Drawing.Imaging.ImageFormat.Gif);
            return liveMemStream.ToArray();

Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Convert and save Byte Array as Image File using C#.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Create Custom helper Functions in Laravel
  2.   Top 10 Advanced MySQL Interview Questions Answers
  3.   how to add edit and delete rows of a html table with javascript?
  4.   PHP Laravel Autocomplete search from Database Example
  5.   Ng Events using Angular Example with demo
  6.   Dynamically Change Alternate row colors using jQuery
  7.   Laravel Many to many relationships with itself
  8.   How to Get the Value of Selected Option in a Combo Box Using jQuery?
  9.   How To Capitalize The First Letter Of A String In JavaScript
  10.   node.js making https post request with x-www-form-urlencoded data