2009年9月15日 星期二

C# BinaryWriter and BinaryReader 應用

一個簡單的應用,不過現在還用不太到,先寫起來作個紀錄

Code 大概是這樣, 將一段字串和一個圖片檔一起存到一個 txt 的 file , 在從file 裡面讀出之前存入的字串和圖片

兩個 function
1. SaveFileByBinaryWrite
2. LoadFileByBinaryReader


Code 如下


SaveFileByBinaryWrite :


/**
* @name SaveFileByBinaryWrite
*
* Save the file by using BinaryWrite method,
* the file include sample description and the image source binary array
*
*
* @param ImgFileRoot [in] - the image file path
* @param Description1 [in] - sample description 1
* @param Description2 [in] - sample description 2
* @param newFile [in] - the new file path
*
*/
public void SaveFileByBinaryWrite(string ImgFileRoot, string Description1, string Description2, string NewFile)
{

/// Open FIleStream to transform image to byte array "MyData"
FileStream FsImageSource = new FileStream(ImgFileRoot, FileMode.OpenOrCreate, FileAccess.Read);
byte[] MyData = new byte[FsImageSource.Length];
FsImageSource.Read(MyData, 0, System.Convert.ToInt32(FsImageSource.Length));
FsImageSource.Close();


/// Open fileStream to write the information to the txt file
FileStream fs = new FileStream(NewFile, FileMode.CreateNew);

/// Open BinaryWriter
BinaryWriter bw = new BinaryWriter(fs);

/// Write information
bw.Write(Description1);
bw.Write(Description2);

/// Set the next write start position
/// In there , set 300
bw.Seek(300, SeekOrigin.Begin);

/// Write byte array MyData begin MyData[0] and length is MyData.Length
bw.Write(MyData, 0, MyData.Length);


/// Close the BinaryWriter and fileStream
bw.Close();
fs.Close();

}




LoadFileByBinaryReader:


/**
* @name LoadFileByBinaryReader
*
* Load the file by using BinaryRead method,
* the file include sample description and the image source binary array
*
*
* @param FileRoot [in] - the file path
*
*
* @return the BitmapImage to shw the image
*
*/
public BitmapImage LoadFileByBinaryReader(string FileRoot)
{

/// Set the Description Length
int DescriptionLength = 300;

/// Open FIleStream to transform image to byte array "MyData"
FileStream fs = new FileStream(FileRoot, FileMode.OpenOrCreate, FileAccess.Read);

/// Open BinaryReader read the FileStream
BinaryReader br = new BinaryReader(fs);

/// Declare the description char array
/// In there length is 300
char[] CharDescripion = new char[DescriptionLength];

/// Write the binary array in CharDescripion , start position 0 and length 300
br.Read(CharDescripion, 0, DescriptionLength);

/// Declare the string dscription
string Descripion = "";


/// Write the Char information to the string Descripion
for (int i = 1; i < DescriptionLength ; i++)
/// when no file break

if (CharDescripion[i] == '\0')
break;
Descripion = Descripion + CharDescripion[i];
}

/// Declare the byte array to get the image
byte[] MyData = new byte[fs.Length - DescriptionLength];

/// Get the ByteLength
int ByteLength = System.Convert.ToInt32(fs.Length - DescriptionLength);
/// Write the binary array in MyData, start position 0 and length ByteLength fs.Read(MyData, 0, ByteLength);
/// Close FIleStream
fs.Close();
/// Put the image byte[] to MemoryStream
MemoryStream ms = new MemoryStream(MyData);
/// Declare BitmapImage Bi
BitmapImage
Bi = new BitmapImage();
Bi.BeginInit();
/// Set Bi source from MemoryStream
Bi.StreamSource = ms;
Bi.EndInit();
return Bi;
}

沒有留言: