簡體   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