本文整理汇总了C#中System.IO.BinaryReader.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.BinaryReader.Dispose方法的具体用法?C# System.IO.BinaryReader.Dispose怎么用?C# System.IO.BinaryReader.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.BinaryReader
的用法示例。
在下文中一共展示了System.IO.BinaryReader.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Load
public bool Load(string path)
{
System.IO.FileStream fs = null;
try
{
fs = System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
}
catch(System.IO.FileNotFoundException e)
{
return false;
}
var br = new System.IO.BinaryReader(fs);
var buf = new byte[1024];
if (br.Read(buf, 0, 8) != 8)
{
fs.Dispose();
br.Dispose();
return false;
}
// png Header 89 50 4E 47 0D 0A 1A 0A
if (buf[0] == 0x89 &&
buf[1] == 0x50 &&
buf[2] == 0x4E &&
buf[3] == 0x47 &&
buf[4] == 0x0D &&
buf[5] == 0x0A &&
buf[6] == 0x1A &&
buf[7] == 0x0A)
{
if (br.Read(buf, 0, 25) != 25)
{
fs.Dispose();
br.Dispose();
return false;
}
var width = new byte[] { buf[11], buf[10], buf[9], buf[8] };
var height = new byte[] { buf[15], buf[14], buf[13], buf[12] };
Width = BitConverter.ToInt32(width, 0);
Height = BitConverter.ToInt32(height, 0);
}
else
{
fs.Dispose();
br.Dispose();
return false;
}
fs.Dispose();
br.Dispose();
return true;
}
示例2: LoadHeightmapFromImageFile
public static HeightData LoadHeightmapFromImageFile(string path){
Config config = Main.Get().GetConfig();
HeightData heights;
string ext = path.Substring(path.LastIndexOf('.'), path.Length - path.LastIndexOf('.')).ToLower();
if (ext == ".shd")
{
// byte-by-byte binary reading
System.IO.BinaryReader reader = new System.IO.BinaryReader(System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read));
// read first eight bytes with map dimensions
int width = reader.ReadInt32();
int height = reader.ReadInt32();
heights = new HeightData((UInt16) width,(UInt16) height);
// read the double bytes containing the height data
for (int i = 0; i < width * height; i++)
{
heights[i] = reader.ReadInt16();
}
reader.Close();
reader.Dispose();
}
else
{
// read the bitmap file
System.Drawing.Bitmap bitmap;
try
{
bitmap = new System.Drawing.Bitmap(path);
}
catch(ArgumentException){
return null;
}
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
heights = new HeightData((UInt16) bitmap.Width, (UInt16)bitmap.Height);
// prepare memory space for the color data
byte[] bytes = new byte[data.Stride * bitmap.Height];
int pixelSize = data.Stride / data.Width;
// get a pointer to the to first line (=first pixel)
IntPtr ptr = data.Scan0;
// create a byte copy of the heightmap data
System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, data.Stride * bitmap.Height);
// create the color data
for (int i = 0; i < bytes.Length; i += pixelSize)
{
heights[i / pixelSize] = (short)((short)bytes[i] * 128);
}
bitmap.UnlockBits(data);
bitmap.Dispose();
}
HeightData heights2 = Main.GetResizedHeightData(heights, Math.Min(heights.Width, (int)config.mapDetailLevel), Math.Min(heights.Height, (int)config.mapDetailLevel));
return heights2;
}
示例3: btnChooseFile_Click
private void btnChooseFile_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".data";
dlg.Filter = "Frame data (*.data)|*.data";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
frameList.Clear();
// Open document
string filename = dlg.FileName;
using (System.IO.BinaryReader br =
new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open)))
{
while (br.BaseStream.Position < br.BaseStream.Length)
{
//deserialises and reads the binary file
DateTime date = new DateTime();
date = DateTime.FromBinary(br.ReadInt64());
// Console.WriteLine(date.ToString());
Int32 stimCode = br.ReadInt32();
Int32 nextBlock = br.ReadInt32();
byte[] frameData = br.ReadBytes(nextBlock);
Leap.Frame newFrame = new Leap.Frame();
newFrame.Deserialize(frameData);
if (stimCode == 5443)
{
Debug.WriteLine("5443 detected: " + newFrame.Id);
}
frameList.Add(newFrame);
//Console.WriteLine(newFrame.CurrentFramesPerSecond);
}
br.Close();
br.Dispose();
}
/* WRITE CODE HERE TO EXTRACT DATA FROM FILE
foreach (Leap.Frame frame in frameList)
{
//Console.WriteLine(frame.Id);
}
*/
}
}