How to convert image to byte array

前端 未结 12 1410
悲哀的现实
悲哀的现实 2020-11-22 09:52

Can anybody suggest how I can convert an image to a byte array and vice versa?

I\'m developing a WPF application and using a stream reader.

相关标签:
12条回答
  • 2020-11-22 10:41

    You can use File.ReadAllBytes() method to read any file into byte array. To write byte array into file, just use File.WriteAllBytes() method.

    Hope this helps.

    You can find more information and sample code here.

    0 讨论(0)
  • 2020-11-22 10:44

    Code:

    using System.IO;
    
    byte[] img = File.ReadAllBytes(openFileDialog1.FileName);
    
    0 讨论(0)
  • 2020-11-22 10:44

    If you don't reference the imageBytes to carry bytes in the stream, the method won't return anything. Make sure you reference imageBytes = m.ToArray();

        public static byte[] SerializeImage() {
            MemoryStream m;
            string PicPath = pathToImage";
    
            byte[] imageBytes;
            using (Image image = Image.FromFile(PicPath)) {
                
                using ( m = new MemoryStream()) {
    
                    image.Save(m, image.RawFormat);
                    imageBytes = new byte[m.Length];
                   //Very Important    
                   imageBytes = m.ToArray();
                    
                }//end using
            }//end using
    
            return imageBytes;
        }//SerializeImage
    
    0 讨论(0)
  • 2020-11-22 10:45

    To be convert the image to byte array.The code is give below.

    public byte[] ImageToByteArray(System.Drawing.Image images)
    {
       using (var _memorystream = new MemoryStream())
       {
          images.Save(_memorystream ,images.RawFormat);
          return  _memorystream .ToArray();
       }
    }
    

    To be convert the Byte array to Image.The code is given below.The code is handle A Generic error occurred in GDI+ in Image Save.

    public void SaveImage(string base64String, string filepath)
    {
        // image convert to base64string is base64String 
        //File path is which path to save the image.
        var bytess = Convert.FromBase64String(base64String);
        using (var imageFile = new FileStream(filepath, FileMode.Create))
        {
            imageFile.Write(bytess, 0, bytess.Length);
            imageFile.Flush();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:48

    try this:

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }
    
    public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
    
    0 讨论(0)
  • 2020-11-22 10:48

    This code retrieves first 100 rows from table in SQLSERVER 2012 and saves a picture per row as a file on local disk

     public void SavePicture()
        {
            SqlConnection con = new SqlConnection("Data Source=localhost;Integrated security=true;database=databasename");
            SqlDataAdapter da = new SqlDataAdapter("select top 100 [Name] ,[Picture] From tablename", con);
            SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
            DataSet ds = new DataSet("tablename");
            byte[] MyData = new byte[0];
            da.Fill(ds, "tablename");
            DataTable table = ds.Tables["tablename"];
               for (int i = 0; i < table.Rows.Count;i++ )               
                   {
                    DataRow myRow;
                    myRow = ds.Tables["tablename"].Rows[i];
                    MyData = (byte[])myRow["Picture"];
                    int ArraySize = new int();
                    ArraySize = MyData.GetUpperBound(0);
                    FileStream fs = new FileStream(@"C:\NewFolder\" + myRow["Name"].ToString() + ".jpg", FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(MyData, 0, ArraySize);
                    fs.Close();
                   }
    
        }
    

    please note: Directory with NewFolder name should exist in C:\

    0 讨论(0)
提交回复
热议问题