将文件加入到图形文件里;

今天到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();
      }
      
    }


代码就和上面所说的一样
1、读bmp数据
2、读文件数据
3、创建新bmp文件
4、写bmp数据
5、写特征字符串
6、写文件数据
7、完毕。

下面是拆开文件的代码:
    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();
      }

    }

过程是
1、读bmp文件
2、建立新文件
3、查找特征字符串
4、写新文件(特征字符串偏移位置+特征字符串长度)
5、完成。


是不是很简单呢?工程源码下载
posted @ 2005-04-28 09:47  suifei  阅读(1914)  评论(3编辑  收藏  举报