2013-05-12 75 views
1

我在C#中創建了一個WinForm應用程序,我可以用它來「嗅出」文件中的一些24位位圖。我已經收集了一些信息,例如它的偏移量,關於它如何寫入文件的一些分析以及它的長度。C# - 從字節創建BMP

有關文件,以便更多的信息有:

  • 的BMP數據反向寫入。 (實施例:(255 0 0)被寫入(0 0 255)
  • 它沒有BMP頭只有BMP的圖像數據的塊
  • 的PixelFormat是24位
  • 及其BMP是純品紅色(255。 0 255 RGB)

我用下面的代碼:

  using (FileStream fs = new FileStream(@"E:\MyFile.exe", FileMode.Open)) 
      { 
        int width = 190; 
        int height = 219; 
        int StartOffset = 333333; // Just a sample offset 

        Bitmap tmp_bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

        Rectangle rect = new Rectangle(0, 0, tmp_bitmap.Width, tmp_bitmap.Height); 
        System.Drawing.Imaging.BitmapData bmpData = 
         tmp_bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, 
         tmp_bitmap.PixelFormat); 

        unsafe 
        { 
         // Get address of first pixel on bitmap. 
         byte* ptr = (byte*)bmpData.Scan0; 

         int bytes = width * height * 3; //124830 [Total Length from 190x219 24 Bit Bitmap] 

         int b; // Individual Byte 

         for (int i = 0; i < bytes; i++) 
         { 
          fs.Position = StartOffset - i; // Change the fs' Position [Subtract since I'm reading in reverse] 
          b = fs.ReadByte();    // Reads one byte from its position 

          *ptr = Convert.ToByte(b); // Record byte 
          ptr ++; 
         } 
         // Unlock the bits. 
         tmp_bitmap.UnlockBits(bmpData); 
        } 
        pictureBox1.Image = tmp_bitmap; 
       } 

我得到這個輸出,我認爲原因是字節越來越搞砸時,它擊中了下一行。 (255 0 255變成0 255 255,它一直持續到becom ES 255 255 0)

Output

我希望你能幫助我解決這個問題。非常感謝你提前。

SOLUTION 現在,它是由(從我的朋友和信息詹姆斯餒給予一些幫助)

if (width % 4 != 0) 
    if ((i + 1) % (width * 3) == 0 && (i + 1) * 3 % width < width - 1) 
     ptr += 2; 

非常感謝您將此代碼添加固定的!

+0

我想問題是你不要在你的代碼中使用bmpData.Stride,在位圖數據中,像素放置在一行中是互相跟隨的,但是當改變行時可能會有一些其他數據不屬於像素所以一切都出錯了,我會爲這個問題發佈一個答案 – Mehran 2013-05-12 18:25:22

回答

4

對於一個標準的BMP,每條掃描線需要是4個字節的倍數,所以當你有一個24位圖像(每個像素3個字節)時,你通常需要在每次掃描結束時允許填充線,使其達到向的4

的倍數。例如,如果您的寬度如果150個像素,這是450個字節,這需要被向上舍入到452,使之4的倍數

我懷疑這可能是你在這裏遇到的問題。

+0

現在它運行良好。非常感謝您的信息! – CudoX 2013-05-12 18:39:46

+0

優秀。很高興我能幫上忙。 – 2013-05-12 18:41:01