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 vb.net, 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#

Default.aspx

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.




    Convert Byte Array to Image File C# Example


    

How to upload image on server in asp.net using c#



c# – How to convert image to byte array

Default.aspx.cs

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)
        {
            try
            {
                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 = Color.blue;
            }
            catch (Exception ex)
            {
                MessageLabel.Text = ex.Message;
            }
        }
    }
}

LiveImgConvertor.cs

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);
                    break;
                case ".png":
                    image.Save(liveMemStream, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case ".gif":
                    image.Save(liveMemStream, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
            }
            return liveMemStream.ToArray();
        }
    }
}
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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 Pakainfo.com 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.

Leave a Comment