zoukankan      html  css  js  c++  java
  • 把文件以二进制写入到图形文件里

    今天到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、完成。


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

    出处:http://www.cnblogs.com/Chinasf/archive/2005/04/28/146740.html

  • 相关阅读:
    Android系统Recovery工作原理之使用update.zip升级过程分析(二)---update.zip差分包问题的解决
    Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作
    Android OTA升级(2):开启升级过程
    Android OTA升级(1):编译升级全包
    XMind
    How To Build CyanogenMod For Huawei Honor 5X ("kiwi")
    想把cm移植到自己的手机上,有没有大神可以教教 谢谢
    SEEM: simulation experimental environments for mobile applications in MANETs: poster
    【Henu ACM Round#16 B】 Bear and Colors
    【Henu ACM Round#16 C】Graph and String
  • 原文地址:https://www.cnblogs.com/mq0036/p/7229055.html
Copyright © 2011-2022 走看看