davidOhara
davidOhara

Reputation: 1008

how to convert image file into image bytes

can you guys help to convert a JPG File into an 2-dimensional int [] [] array?! There´s a solution to convert it to a bytearray, but i need it in an int array....

byte[] imageBytes = File.ReadAllBytes("example.jpg");

Upvotes: 3

Views: 2018

Answers (2)

Remus Rusanu
Remus Rusanu

Reputation: 294197

If you need to provide raw access to the underlying image for some image processing API (which is most likely what you actually need) and not access to the file then see How to: Use LockBits.

this article also covers some basics like scans and stride: Using the LockBits method to access image data.

Upvotes: 2

Simon Ejsing
Simon Ejsing

Reputation: 1495

ReadAllBytes will not get you the pixels of the JPEG. JPEG is a compressed image type. You need to load it into an Image class first to uncompress it. Then you can access the pixels of the image plus determine the width and height of it.

Bitmap image = new Bitmap("example.jpg"); 

// Loop through the image
for(x=0; x<image.Width; x++)
{
    for(y=0; y<image.Height; y++)
    {
        Color pixelColor = image1.GetPixel(x, y);
        my_int_array[x][y] = pixelColor.ToArgb();
    }
}

Upvotes: 3

Related Questions