Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read pixel data only not meta data from 16 bit image in c#?

Tags:

c#

image

byte

I have 16 bit depth image from that I want to read only pixel data and store it to byte[]

I tried following code :

FileInfo fileInfo = new FileInfo("C:\\Image stitch API\\Temp\\output.tif");
byte[] data = new byte[fileInfo.Length];

I also tried the following :

Image img;
img = Image.FromFile("C:\\Image stitch API\\Temp\\output.tif");
ImageConverter ic = new ImageConverter();
byte[] data = (byte[])ic.ConvertTo(img, typeof(byte[]))

Here all data it is coming from image, but I need only pixel data?

Can any one help on this?

like image 871
Dileep Avatar asked Jan 23 '26 18:01

Dileep


1 Answers

If you can load your image as Bitmap, it is pretty easy to get pixel info as shown below.

    Bitmap bitmap = new Bitmap("somefile.ext");
    Color color = bitmap.GetPixel(x,y) 

GetPixel() will return Color (struct) type and you can get individual channel values as byte like shown below.

    byte g = slateBlue.G;
    byte b = slateBlue.B;
    byte r = slateBlue.R;
    byte a = slateBlue.A;

Regarding your comment, I will suggest you to use use Netvips to manipulate image in byte array form (it is way betterand faster than system.drawing). By doing so, you could get image bands as byte arrays as below.

    var imageBands = inputImage.Bandsplit();
    var R = imageBands [0];
    var B = imageBands [1];
    var G = imageBands [2];

If you dont wanna switch the libraries, you could get byte array with System.Drawing like shown below.

byte[] image = new[] {R, B, G};
like image 80
Hasan Avatar answered Jan 25 '26 09:01

Hasan