-
Dec 30th, 2016, 08:37 PM
#1
Thread Starter
Member
How to convert image to binary and get result in text box
I need to convert image to binary and get result in text box .
Image found in path D:/person.jpg
i using the following function :
public bool[] imageToBinaryArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
bool[] arr = new bool[50000000];
int i =0;
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitArray bitarray = new BitArray(ms.ToArray());
foreach (bool item in bitarray)
{
arr[i] = item;
i++;
}
return arr;
}
How to receive the value returned from function imageToBinaryArray in textbox1 ?
-
Dec 30th, 2016, 08:49 PM
#2
Re: How to convert image to binary and get result in text box
Firstly, please don't post unformatted code snippets as they are hard to read.
csharp Code:
public bool[] imageToBinaryArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); bool[] arr = new bool[50000000]; int i =0; imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitArray bitarray = new BitArray(ms.ToArray()); foreach (bool item in bitarray) { arr[i] = item; i++; } return arr; }
As for the question, what are you actually expecting to see in the TextBox? Do you want a binary string, i.e. a string of 1 and 0 digits?
-
Dec 30th, 2016, 08:52 PM
#3
Thread Starter
Member
Re: How to convert image to binary and get result in text box
yes because i need to get image in binary string 0 and 1
-
Dec 30th, 2016, 08:59 PM
#4
Re: How to convert image to binary and get result in text box
You already know how to get a byte array from an Image so all you need is how to get a binary string from a byte array:
csharp Code:
// No gaps between bytes. var myBinaryString1 = string.Concat(myByteArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0'))); // One space between each pair of bytes. var myBinaryString2 = string.Join(" ", myByteArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
Just be aware that a string requires two bytes per character so, if your Image is large, your string will be very big and may not be practical to display in a TextBox or even store in memory.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|