把文件以二進制寫入到圖形文件裏

今天到csdn有朋友短信問我如何寫文件到圖象內,反正是學習C#,就又寫了一個這樣的小工具

好比說公司不能攜帶源代碼;能夠帶圖片;要作的就是把源碼用rar打個包,而後找個bmp文件,打開它,在尾部增長几個特徵字符串,再把rar的數據增長上去,ok了。帶出去後,打開bmp文件,找到特徵字符串,把尾部記錄複製出來,保存到一個新文件內;
該方法一樣能夠用於EXE文件.


爲了簡便操做,用C#編寫了一個工具軟件,如下是部分代碼,(本人菜鳥臭做、高手勿笑):


      private   bool  EncodeDataToBitmap( string  srcBmpFile, string  srcFile, string  destBmpFile) {
      
//加入到文件尾部
      System.IO.FileStream SBF= null
      System.IO.FileStream SF
= null
      System.IO.FileStream DBF
= null;
      
byte[] srcBmpByte;
      
byte[] srcFileByte;
      
try {
        SBF 
= new System.IO.FileStream(srcBmpFile,System.IO.FileMode.Open, System.IO.FileAccess.Read);
        SF 
= new System.IO.FileStream(srcFile,System.IO.FileMode.Open, System.IO.FileAccess.Read);
        DBF 
= new System.IO.FileStream(destBmpFile,System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
        
        srcBmpByte 
= new byte[SBF.Length];
        SBF.Read(srcBmpByte,
0,(int)SBF.Length);
        srcFileByte 
= new byte[SF.Length];//取得該數據能夠進一步加密一下或壓縮一下
        SF.Read(srcFileByte,0,(int)SF.Length);
        DBF.Write(srcBmpByte,
0,srcBmpByte.Length);
        DBF.Write(System.Text.Encoding.Default.GetBytes(
"abcdefg"),0,System.Text.Encoding.Default.GetBytes("abcdefg").Length);
        DBF.Write(srcFileByte,
0,srcFileByte.Length);
        
        
return true;
      }
catch{
        
return false;
      }
finally{
        
if(SBF!=null)
          SBF.Close();
        
if(SF!=null)
          SF.Close();
        
if(DBF!=null)
          DBF.Close();
      }
      
    }


代碼就和上面所說的同樣
一、讀bmp數據
二、讀文件數據
三、建立新bmp文件
四、寫bmp數據
五、寫特徵字符串
六、寫文件數據
七、完畢。

下面是拆開文件的代碼:
     private   bool  DecodeDataFromBitmap( string  srcBmpFile, string  destFile) {
      System.IO.FileStream SBF 
= null;
      System.IO.FileStream DF 
= null;
      
byte[] srcBmpByte;
      
try{
        SBF 
= new System.IO.FileStream(srcBmpFile,System.IO.FileMode.Open,System.IO.FileAccess.Read);
        DF 
= new System.IO.FileStream(destFile,System.IO.FileMode.CreateNew,System.IO.FileAccess.Write);
        
        srcBmpByte 
= new byte[SBF.Length];
        SBF.Read(srcBmpByte,
0,(int)SBF.Length);

        
string f = "";
        
int offset = 0;

        
for(int i=0;i<srcBmpByte.Length- 7;i++){
          f 
= "";
          
for(int j=i;j<i+7;j++){
            f
+=(char)srcBmpByte[j];
          }

          
if(f=="abcdefg"){
            offset 
= i+7;
            
break;
          }

        }

        
if(offset==0){
          f 
="";
          
for(int i=srcBmpByte.Length-7;i<srcBmpByte.Length;i++){
            f
+=(char)srcBmpByte[i];
          }

          
if(f=="abcdefg"){
            offset 
= srcBmpByte.Length-7;
          }
else{
            MessageBox.Show(
"該文件未被加入數據!");
            
return false;
          }

        }


        DF.Write(srcBmpByte,offset,srcBmpByte.Length
-offset);
        
return true;
      }
catch{
        
return false;
      }
finally{
        
if(SBF!=null)SBF.Close();
        
if(DF!=null)DF.Close();
      }

    }

過程是
一、讀bmp文件
二、創建新文件
三、查找特徵字符串
四、寫新文件(特徵字符串偏移位置+特徵字符串長度)
五、完成。


是否是很簡單呢? 工程源碼下載

 

出處:http://www.cnblogs.com/Chinasf/archive/2005/04/28/146740.htmlhtml

相關文章
相關標籤/搜索