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

今天到csdn有朋友短信问我如何写文件到图象内,反正是学习C#,就又写了一个这样的小工具

比如说公司不能携带源代码;可以带图片;要做的就是把源码用rar打个包,然后找个bmp文件,打开它,在尾部增加几个特征字符串,再把rar的数据增加上去,ok了。带出去后,打开bmp文件,找到特征字符串,把尾部记录复制出来,保存到一个新文件内;
该方法同样可以用于EXE文件.


为了简便操作,用C#编写了一个工具软件,以下是部分代码,(本人菜鸟臭作、高手勿笑):


ExpandedBlockStart.gif ContractedBlock.gif       private   bool  EncodeDataToBitmap( string  srcBmpFile, string  srcFile, string  destBmpFile) dot.gif {
InBlock.gif      
//加入到文件尾部
InBlock.gif
      System.IO.FileStream SBF= null
InBlock.gif      System.IO.FileStream SF
= null
InBlock.gif      System.IO.FileStream DBF
= null;
InBlock.gif      
byte[] srcBmpByte;
InBlock.gif      
byte[] srcFileByte;
ExpandedSubBlockStart.gifContractedSubBlock.gif      
try dot.gif{
InBlock.gif        SBF 
= new System.IO.FileStream(srcBmpFile,System.IO.FileMode.Open, System.IO.FileAccess.Read);
InBlock.gif        SF 
= new System.IO.FileStream(srcFile,System.IO.FileMode.Open, System.IO.FileAccess.Read);
InBlock.gif        DBF 
= new System.IO.FileStream(destBmpFile,System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
InBlock.gif        
InBlock.gif        srcBmpByte 
= new byte[SBF.Length];
InBlock.gif        SBF.Read(srcBmpByte,
0,(int)SBF.Length);
InBlock.gif        srcFileByte 
= new byte[SF.Length];//取得该数据可以进一步加密一下或压缩一下
InBlock.gif
        SF.Read(srcFileByte,0,(int)SF.Length);
InBlock.gif        DBF.Write(srcBmpByte,
0,srcBmpByte.Length);
InBlock.gif        DBF.Write(System.Text.Encoding.Default.GetBytes(
"abcdefg"),0,System.Text.Encoding.Default.GetBytes("abcdefg").Length);
InBlock.gif        DBF.Write(srcFileByte,
0,srcFileByte.Length);
InBlock.gif        
InBlock.gif        
return true;
ExpandedSubBlockStart.gifContractedSubBlock.gif      }
catchdot.gif{
InBlock.gif        
return false;
ExpandedSubBlockStart.gifContractedSubBlock.gif      }
finallydot.gif{
InBlock.gif        
if(SBF!=null)
InBlock.gif          SBF.Close();
InBlock.gif        
if(SF!=null)
InBlock.gif          SF.Close();
InBlock.gif        
if(DBF!=null)
InBlock.gif          DBF.Close();
ExpandedSubBlockEnd.gif      }
      
ExpandedBlockEnd.gif    }


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

下面是拆开文件的代码:
ExpandedBlockStart.gif ContractedBlock.gif      private   bool  DecodeDataFromBitmap( string  srcBmpFile, string  destFile) dot.gif {
InBlock.gif      System.IO.FileStream SBF 
= null;
InBlock.gif      System.IO.FileStream DF 
= null;
InBlock.gif      
byte[] srcBmpByte;
ExpandedSubBlockStart.gifContractedSubBlock.gif      
trydot.gif{
InBlock.gif        SBF 
= new System.IO.FileStream(srcBmpFile,System.IO.FileMode.Open,System.IO.FileAccess.Read);
InBlock.gif        DF 
= new System.IO.FileStream(destFile,System.IO.FileMode.CreateNew,System.IO.FileAccess.Write);
InBlock.gif        
InBlock.gif        srcBmpByte 
= new byte[SBF.Length];
InBlock.gif        SBF.Read(srcBmpByte,
0,(int)SBF.Length);
InBlock.gif
InBlock.gif        
string f = "";
InBlock.gif        
int offset = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0;i<srcBmpByte.Length- 7;i++)dot.gif{
InBlock.gif          f 
= "";
ExpandedSubBlockStart.gifContractedSubBlock.gif          
for(int j=i;j<i+7;j++)dot.gif{
InBlock.gif            f
+=(char)srcBmpByte[j];
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockStart.gifContractedSubBlock.gif          
if(f=="abcdefg")dot.gif{
InBlock.gif            offset 
= i+7;
InBlock.gif            
break;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockStart.gifContractedSubBlock.gif          
if(f=="abcdefg")dot.gif{
InBlock.gif            offset 
= srcBmpByte.Length-7;
ExpandedSubBlockStart.gifContractedSubBlock.gif          }
elsedot.gif{
InBlock.gif            MessageBox.Show(
"该文件未被加入数据!");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        DF.Write(srcBmpByte,offset,srcBmpByte.Length
-offset);
InBlock.gif        
return true;
ExpandedSubBlockStart.gifContractedSubBlock.gif      }
catchdot.gif{
InBlock.gif        
return false;
ExpandedSubBlockStart.gifContractedSubBlock.gif      }
finallydot.gif{
InBlock.gif        
if(SBF!=null)SBF.Close();
InBlock.gif        
if(DF!=null)DF.Close();
ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif    }

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


是不是很简单呢? 工程源码下载

转载于:https://www.cnblogs.com/Chinasf/archive/2005/04/28/146740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值