繁体   English   中英

C#-以已知的偏移量从文件获取字节

[英]C# - Get bytes from file at known offset

首先,这是我第一个适合的C#程序,而我的编程经验主要是为TES5Edit编写Pascal脚本。 在Lazarus中编写了两个实际程序,但是,很糟糕。

向我上传了当前代码'ere: http : //www.mediafire.com/download/fadr8bc8d6fv7cf/WindowsFormsApplication1.7z

无论如何! 我目前正在尝试做的是获取.dds文件中两个特定偏移量的字节值。 x分辨率保持为@ offset + 0c,并由两个字节组成(即+ 0c和+ 0d)。 y分辨率相同的演出; @偏移+10和+11。 我在这里上传了我的发现: http : //pastebin.com/isBKwaas

但是,我不知道如何执行此操作。 我所能从各种Google搜索结果中得出的最大结论是:

        public void GetDDSDimensions(string strDDSFilename, out int iSourceDDSx, out int iSourceDDSy)
    {
        FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read);
        int iWidthOffset = 12; // 0c in hex, if byte is 00, then size is => 256, so get +0d as well
        int iHeightOffset = 16; // 10 in hex, same gig as above. Get byte @ +11 if +10 is 00.
        byte[] bufferDDSBytes = new byte[24]; // @ Offset +24 , byte is always 01. Use as a wee, err, "landmark".

    }

不知道如何从那里继续前进。 我需要以某种方式设置bufferDDSBytes来获取fsSourceDDS的前24个字节,然后比较十六进制值@ + 0c和+10,以获得.dds文件的分辨率。

比较应该很容易; C#应该具有与Pascal的StrToInt()函数等效的十六进制,不是吗?

首先,使用using :-)

using (FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read))
{
     // do something with the FileStream
} // Ensures that it is properly closed/disposed

要转到流中的特定偏移量,请使用Seek方法:

fsSourceDDS.Seek(someOffset, SeekOrigin.Begin);

并对其调用ReadByteRead方法以获取所需的字节数。 读取字节后,流中的位置将增加读取的字节数。 您可以使用Position属性获取流中的当前位置。 要直接从流中读取小尾数值,可以使用BinaryReader类。

结合以上所有内容:

using (FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read))
using (BinaryReader binaryReader = new BinaryReader(fsSourceDDS))
{
    fsSourceDDS.Seek(0x0c, SeekOrigin.Begin);
    ushort with = binaryReader.ReadUInt16();
    fsSourceDDS.Seek(0x10, SeekOrigin.Begin);
    ushort height = binaryReader.ReadUInt16();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM