简体   繁体   English

将图像转换为字节[]

[英]Convert Image into Byte[]

I want to start learning about how to tear images apart to find patterns in them but in order to do that I need to first see what makes it up. 我想开始学习如何将图像拆开以找到图像中的图案,但是要做到这一点,我需要首先了解其组成。 I want to take a png and convert it into a byte array so I can print it out and see if I can recognize simple patterns in the array values. 我想采用png并将其转换为字节数组,以便可以将其打印出来,看看是否可以识别数组值中的简单模式。

So far I have this 到目前为止,我有这个

public MainWindow()
{
    InitializeComponent();
    System.Drawing.Image image;
    image = System.Drawing.Image.FromFile("one.png");            

    byte[] imArray = imageToByteArray(image);

    String bytes = "";
    foreach (Char bite in imArray)
    {
        bytes += "-"+bite;
    }
    MessageBox.Show(bytes);


}

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

But it doesn't seem to be working. 但这似乎不起作用。 It gives me a null error when the conversion method is called. 调用转换方法时,它给我一个空错误。 I have NO clue why this isn't working because my understanding of the compenents is nill. 我不知道为什么这行不通,因为我对这些组件的了解很少。

If you can suggest an easier way to make this conversion feel free to post it. 如果您可以提出一种更简便的方法来进行此转换,请随时发布。 Im not stuck on this code I just want a working example so I have a starting point. 我没有停留在这段代码上,我只想要一个有效的示例,所以我有一个起点。

Thanks! 谢谢!

I'd recommend starting with Bitmap to look at binary data - most other formats store data compressed, so you have no chance to understand what is inside an image by looking at the bytes. 我建议从Bitmap开始查看二进制数据-大多数其他格式都存储压缩的数据,因此您没有机会通过查看字节来了解图像内部的内容。

The method you want is Bitmap.LockBits . 您想要的方法是Bitmap.LockBits The article also includes complete sample how to read from file and look t bits, excerpt below: 本文还包括完整的示例,该示例如何从文件读取并查看t位,摘录如下:

Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData =
   bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
Marshal.Copy(bmpData.Scan0, rgbValues, 0, bytes);

you could try converting the image to a dataURI then converting it to a blob, heres an example of how you can convert dataURIs to blobs Blob from DataURL? 您可以尝试将图片转换为dataURI,然后将其转换为Blob,以下是如何将DataURIs转换为Blob的示例。

function dataURItoBlob(dataURI) {

var byteString = atob(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);

for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }

var bb = new BlobBuilder();
bb.append(ab); return bb.getBlob(mimeString);
}

或者,您可以只在二进制编辑器中打开文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM