I want a OpenFileDialog to select any file-types and show the file bits in a bitmap image. I mean the selected file contain some 0/1 bits, I want to show them in black and white image. Any Idea?
Asked
Active
Viewed 563 times
0
-
1Write a function that converts arbitrary bytes to a Bitmap image and then load that. – Scott Chamberlain Dec 24 '16 at 15:26
1 Answers
2
If the file is a valid image file, you can simply read an image like this:
Image image = Image.FromFile(pathOfImage);
...and then assign it to a picture box.
You will need a reference to System.Drawing.dll
and include a using using System.Drawing;
at the top of your code.
If the bits in the file, however, represent the black and white pixels, you will need to draw the image yourself.
First create a Bitmap
, then create a graphics object from it. You can then draw the pixels on it.
using (var image = new Bitmap(width, height))
using (var g = Graphics.FromImage(image)) {
// TODO: Draw using the graphics object. (Insert code below)
}
You can use the answer from this answer to read the bits: BinaryReader - Reading a Single “ BIT ”?
In a double loop you can then iterate through the bits. Assuming that the bits are stored line by line:
using (var stream = new FileStream("file.dat", FileMode.Open)) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
bool? bit = stream.ReadBit(true);
if (bit == null) { // No more bits
return;
}
if (bit.Value) {
g.FillRectangle(Brushes.White, x, y, 1, 1);
}
}
}
}
Finally assign the image to the picture box
pictureBox1.Image = image;

Community
- 1
- 1

Olivier Jacot-Descombes
- 104,806
- 13
- 138
- 188
-
The files are not valid images. He wants a black and white representation of the bit fields of any arbitrary file. – Scott Chamberlain Dec 24 '16 at 15:32
-
Ok, that's confusing, as valid image files also consist of 0 and 1 bits. – Olivier Jacot-Descombes Dec 24 '16 at 15:33
-
1Instead of a Graphics object I would use [`Bitmap.LockBits`](https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits(v=vs.110).aspx) with a 1 bit per pixel Bitmap. (Would write a answer but I am limited to my phone today) – Scott Chamberlain Dec 24 '16 at 15:43
-
Yes LockBits is very fast, but also very low-level. It is described in this answer: [Fast work with Bitmaps in C#](http://stackoverflow.com/a/1563170/880990). – Olivier Jacot-Descombes Dec 24 '16 at 16:03
-
You can also use the `DirectBitmap` class from SaxxonPike: [C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App](http://stackoverflow.com/a/34801225/880990). You can set pixels with `directBitmap.Bits[y * Width + x] = color.ToArgb();` – Olivier Jacot-Descombes Dec 24 '16 at 16:24
-
I tried this solution: http://stackoverflow.com/a/2426395/6268039 Now I have a file full of 0 and 1, So the problem remaining is to show these 0 and 1 in a custom height and width of black and white pixel – Vahid Dec 24 '16 at 16:34
-
And also http://stackoverflow.com/a/2426395/6268039 have limitation on file size!! – Vahid Dec 24 '16 at 16:49
-
This gets you the bytes, not the bits! In order to get the bits, use the link in my answer. – Olivier Jacot-Descombes Dec 24 '16 at 17:05
-
No, my output file include only 0 and 1. how could it be byte? Its all 0 and 1, Isn't it bits? – Vahid Dec 24 '16 at 17:26
-
This `byte[] fileBytes = File.ReadAllBytes(inputFilename);` reads a byte-array! Of course the files contains bits, but you don't get them 1 by 1, but packed into bytes, 8 at a time. Each byte contains 8 bits. [BinaryReader - Reading a Single “ BIT ”?](http://stackoverflow.com/questions/9455790/binaryreader-reading-a-single-bit/9455806#9455806) tells you how to get single bits. It unpacks the bytes into bits. – Olivier Jacot-Descombes Dec 24 '16 at 18:13