Tuesday, June 17, 2025

How to save bytes data to image

using memory stream

MemoryStream m = new MemoryStream(bytes); 

System.Drawing.Image MyImage = System.Drawing.Image.FromStream(m);

MyImage.Save(filepathandname)

====================

using fileupload control

fileUpload1.FileBytes;

string filepathandname = Server.MapPath(string.Format("~/uploads/{0}-{1}.{2}", DateTime.Now.ToShortDateString(), Guid.NewGuid(), ".png"));

MyImage.Save(filepathandname)

How to save a file to binary data to varbinary sql server in asp.net c# - How to save file to database with varbinary(max) datatype

Using fileupload field

byte[] bb = fileUpload1.FileBytes;

  SqlConnection con = new SqlConnection(ConnectionString.ConString);


            con.Open();

            SqlCommand cmd = new SqlCommand("INSERT INTO UploadFiles (FileData, ContentType) VALUES (@filedata, @ContentType)", con);

            cmd.CommandType = System.Data.CommandType.Text;

            

            

            cmd.Parameters.AddWithValue("@FileData", fileUpload1.FileBytes);

            cmd.Parameters.AddWithValue("@ContentType", fileUpload1.PostedFile.ContentType);


            cmd.ExecuteNonQuery();

            con.Close();

===============================

using binary reader

 Stream fs = fileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        

        Byte[] bytes = br.ReadBytes((int)fs.Length);

  Stream fs = fileUpload1.PostedFile.InputStream;

 Byte[] bytes = br.ReadBytes((int)fs.Length);

object vs instance - object instance - instance vs object in c#

 object are also called instance.

Friday, June 6, 2025

Fundamental unit of data

 Fundamental unit of data in computer is bit or binary digit  0, 1

how to convert byte array to image in asp.net

Byte[] bytes =  fileUpload1.FileBytes;

  MemoryStream m = new MemoryStream(bytes);

     System.Drawing.Image MyImage = System.Drawing.Image.FromStream(m);            

            string filepathandname = Server.MapPath(string.Format("~/uploads/{0}-{1}-{2}", DateTime.Now.ToShortDateString(), Guid.NewGuid(), ".png"));

            MyImage.Save(filepathandname);