-5

Below is the code I used in getting byte array of an image in C# language. What is the code for displaying bArr byte array in textbox?

C#

Image img = Image.FromFile("C:\\Users\\-HELEMINNA-\\Pictures\\633a7f6c0d21cca1212a25d6867f2284.jpg");
byte[] bArr = imgToByteArray(img);

The next code are my vb.net codes that displays byte array to textbox1. Ive included this because someone requested it.

VB.net

Dim img As Image = Image.FromFile("C:\Users\-HELEMINNA-\Pictures\633a7f6c0d21cca1212a25d6867f2284.jpg")
Dim bArr As Byte() = imgToByteArray(img)

TextBox1.Text = String.Join("", Array.ConvertAll(bArr, Function(byteValue) byteValue.ToString))
MAC
  • 1
  • 1
  • 4
  • 1
    It's not necessary to SHOUT here. Posting in ALL CAPS makes text harder to read and understand, it won't get you help any faster, and it's extremely rude when you come here and SHOUT FOR ATTENTION while asking for free help. Stop now. Please spend some time taking the [tour] and reading the [help] pages, particularly [ask], before posting your next question here. – Ken White Nov 20 '17 at 03:46
  • 3
    There is no one single way to interprete a byte array as string. Do you want to get a comma separated list of numbers representing the bytes? Do you want to interprete it as a string with some particular encoding? Do you want a string of "0"s and "1"s, what? – Theraot Nov 20 '17 at 03:47
  • So to be clear, you just rejected my edit and then made all the changes yourself anyways? Makes sense... – jhpratt Nov 20 '17 at 03:53
  • Welcome to Stack Overflow! Please try to be more specific about what you mean by displaying bytes because bytes can be numbers or strings or images or any other thing... – CodingYoshi Nov 20 '17 at 04:17
  • @jhpratt . Sorry for this sir. im just new here, and its my fault for not reading before posting. And i dont know why it is rejected, as soon as ive seen your first comment, i tried to edit it myself. I have not checked the suggestion too. I noticed it after i applied some changes. It is not intentional. I am not that good in english too. – MAC Nov 20 '17 at 04:18
  • @CodingYoshi images – MAC Nov 20 '17 at 04:33
  • @jhpratt just the value sir, without any separator. – MAC Nov 20 '17 at 04:34
  • It's so so sad and unfortunate that a new user comes here for help and everyone starts yelling at them. Not everyone in this world knows that caps means yelling. Not everyone knows how to to use the SO editor. So why dont you all relax and take a chill pill. What is happening to this site... – CodingYoshi Nov 20 '17 at 04:40
  • @mac why do you want to display image bytes in a textbox? – CodingYoshi Nov 20 '17 at 04:45
  • @CodingYoshi ill be using it in rc6 algorithm for image encryption. Based on the article i've read, image needs to be converted to bytes first. I have tried this on vb.net and successfully displayed the byte array to the textbox, but converting it to c# is difficult because i cannot find it on google. I have a guide on rc6 algorithm and it is in c#. Thats why. – MAC Nov 20 '17 at 04:58
  • @mac That doesn't anser Yoshi's question. You already have it in bytes. – ProgrammingLlama Nov 20 '17 at 05:07
  • @mac see [this answer](https://stackoverflow.com/a/23731642/4228458) – CodingYoshi Nov 20 '17 at 05:07
  • @MAC Definitely a coincidence *every* change I made, and no more, were in your edit then. No way you could've checked the suggestion. – jhpratt Nov 20 '17 at 05:08
  • @jhpratt i'm just new here and i'm not that familiar with these things. I'm sorry – MAC Nov 20 '17 at 05:13
  • @john Yes John, I already have it in bytes. but i want it to display on textbox – MAC Nov 20 '17 at 05:23
  • I recommend converting it to Base64 (`Convert.ToBase64String(bytes)`) or [Hexadecimal](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa). You can't display it directly because, as an image, some of the bytes will be unprintable, and some of the bytes will prevent further data from being rendered. That's why Yoshi wanted to know _why_ you want to display them. – ProgrammingLlama Nov 20 '17 at 05:26
  • You said you were able to do this successfully in vb.net. Can you share that vb.net code? – Chetan Nov 20 '17 at 05:31
  • @john ok sir. I just want to see the values because i'm in the middle of analyzing and figuring things out on this image encryption. But if it is not possible then ill just give up and try different approach. Thanks sir john. I think my question is now answered. – MAC Nov 20 '17 at 05:37
  • @ChetanRanpariya I have edited my post and added the vb.net code. – MAC Nov 20 '17 at 05:45
  • This is the equivalent C# code: `String.Join("", Array.ConvertAll(bArr, byteValue => byteValue.ToString()));` But for a byte value of 0x01 it will print 1, and for a value of 0xFF it will print 255. I'm not sure how this will help you, to be honest. I really recommend hexadecimal as a better way of doing it. Incredibly off topic side note: "sir" is a term I only had to use at secondary school. It's really [unnecessary in modern English](https://ell.stackexchange.com/questions/122808/usage-of-sir-in-the-uk) :) – ProgrammingLlama Nov 20 '17 at 06:04
  • 1
    @john That was very helpful. That is exactly what i want. Thank you – MAC Nov 20 '17 at 06:10

2 Answers2

2

The equivalent C# of your last line is:

TextBox1.Text = String.Join("", Array.ConvertAll(bArr, byteValue => byteValue.ToString()));

You replace the anonymous function with a lambda expression (byteValue => byteValue.ToString())

As I noted in my comment, this will print the decimal values of the bytes, so 0x00 will be printed as 1, and 0xFF will be printed as 255.

For example 0x00, 0x20, 0xFF will be printed as 035255. This may not be what you want. Certain combinations of bytes could result in the same printed text. I'd recommend using hexadecimal instead as this will print 2 characters for every byte, instead of 1-3 characters for every byte.

For example, you could output hexadecimal like so:

TextBox1.Text = BitConverter.ToString(bArr).Replace("-", "");

This would output the above example as 0020FF.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
-3

You do something like this:

byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters

string s1 = Encoding.UTF8.GetString(bytes);

TextBox.Text = s1;
Juran
  • 177
  • 1
  • 8
  • @Juran I have edited my post so that it will be easily understood. I have tried the code sir and it displayed 4 symbolic characters. What i want is the value on the byte array bArr. Thank you – MAC Nov 20 '17 at 04:51