Answer to Question #5837 in C# for Mariyah

Question #5837
i want to read bitmap file with binary reader,without using bitmap class, and then convert it into gray scale
1
Expert's answer
2012-01-05T12:37:41-0500
As we have understood you need to convert image in byte form to gray scale also in byte form.

See code snippets

//Open file into a filestream and

//read data in a byte array.

byte[] ReadFile(string sPath)

{

//Initialize byte array with a null value initially.

byte[] data = null;

//Use FileInfo object to get file size.

FileInfo fInfo = new FileInfo(sPath);

long numBytes = fInfo.Length;

//Open FileStream to read file

FileStream fStream = new FileStream(sPath, FileMode.Open,

FileAccess.Read);

//Use BinaryReader to read file stream into byte array.

BinaryReader br = new BinaryReader(fStream);

//When you use BinaryReader, you need to

&

//supply number of bytes to read from file.

//In this case we want to read entire file.

//So supplying total number of bytes.

data = br.ReadBytes((int)numBytes);

return data;

}

//Once you have image byte array

//you transform to gray scale like this

byte[] TransformToGrayscale(byte[] imageArr)

{

byte[] result = new byte[imageArr.Length];

for(int i=0;i<imageArr.Length;i++)
{
result[i] = (byte)(imageArr[i] >> 8);
}
return result;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS