Transform Image into byte?

Asked

Viewed 614 times

2

I need to capture an image of a printer in the /doc, write your bytes in a text file and save to another folder in the /doc.

How can I do that?

  • The correct thing is to write the bytes in a text file. An image (as well as any file) is a set of bytes.

1 answer

8


You can try the following:

// Carrega sua imagem e salva em um array de bytes
byte[] imgdata = System.IO.File.ReadAllBytes(@"C:\Test\simba.jpg");

// Salva seu array de bytes em um arquivo
System.IO.File.WriteAllBytes(@"C:\Test\byteArray.txt", imgdata);

If the folder is in My Documents (My Documents), you can use the following code to fetch your image:

string pathToImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "nomeDaSuaImagem.jpg");

Note that I used an image .jpg. If your image is in another format, change to the desired format, for example .png, .bmp, .gif, etc..

  • 2

    Thank you very much. Solved my problem.

  • @Viniciusmarson Remembering that if the answer helped you, mark it as correct to help other people with the same problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.