0

我想要一个 OpenFileDialog 来选择任何文件类型并在位图图像中显示文件位。我的意思是所选文件包含一些 0/1 位,我想以黑白图像显示它们。任何想法?

4

1 回答 1

3

如果该文件是有效的图像文件,您可以像这样简单地读取图像:

Image image = Image.FromFile(pathOfImage);

...然后将其分配给图片框。

您将需要引用并在代码顶部System.Drawing.dll包含 a 。using using System.Drawing;


但是,如果文件中的位代表黑白像素,则需要自己绘制图像。

首先创建一个Bitmap,然后从中创建一个图形对象。然后,您可以在其上绘制像素。

using (var image = new Bitmap(width, height))
using (var g = Graphics.FromImage(image)) {
    // TODO: Draw using the graphics object. (Insert code below)
}

您可以使用此答案中的答案来读取位:BinaryReader - 读取单个“位”?

在双循环中,您可以遍历这些位。假设这些位是逐行存储的:

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);
            }
        }
    }
}

最后将图像分配给图片框

pictureBox1.Image = image;
于 2016-12-24T15:30:36.567 回答