Convert image to binary?

前端 未结 8 2203
南旧
南旧 2021-02-08 04:07

I have an image (in .png format), and I want this picture to convert to binary.

How can this be done using C#?

相关标签:
8条回答
  • 2021-02-08 04:31
    System.Drawing.Image image = System.Drawing.Image.FromFile("filename");
    byte[] buffer;
    MemoryStream stream = new MemoryStream();
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    
    buffer = stream.ToArray(); // converted to byte array
    stream = new MemoryStream();
    stream.Read(buffer, 0, buffer.Length);
    stream.Seek(0, SeekOrigin.Begin);
    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
    
    0 讨论(0)
  • 2021-02-08 04:32
    byte[] b = File.ReadAllBytes(file);   
    

    File.ReadAllBytes Method

    Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

    0 讨论(0)
  • 2021-02-08 04:35
    public static byte[] ImageToBinary(string imagePath)
        {
            FileStream fS = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
            byte[] b = new byte[fS.Length];
            fS.Read(b, 0, (int)fS.Length);
            fS.Close();
            return b;
        }
    

    just use above code i think your problem will be solved

    0 讨论(0)
  • 2021-02-08 04:35
    using System.IO;
    
    FileStream fs=new FileStream(Path, FileMode.Open, FileAccess.Read); //Path is image location 
    Byte[] bindata= new byte[Convert.ToInt32(fs.Length)];
    fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
    
    0 讨论(0)
  • 2021-02-08 04:39

    Since you have a file use:-

     Response.ContentType = "image/png";
     Response.WriteFile(physicalPathOfPngFile);
    
    0 讨论(0)
  • 2021-02-08 04:41

    You could do:

        MemoryStream stream = new MemoryStream();
        image.Save(stream, ImageFormat.Png);
        BinaryReader streamreader = new BinaryReader(stream);
    
        byte[] data = streamreader.ReadBytes(stream.Length);
    

    data would then contain the contents of the image.

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