webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work074.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="创建文件" OnClick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="交换数据" OnClick="Button2_Click" />
</td>
<td>
<asp:Button ID="Button3" runat="server" Text="还原数据" OnClick="Button3_Click" />
</td>
</tr>
</table>
<table border="1">
<tr>
<td>
1.jpg
</td>
<td>
<img src="./1.jpg" alt="1.jpg" width="300" height="300"/>
</td>
</tr>
<tr>
<td>
2.jpg
</td>
<td>
<img src="./2.jpg" alt="2.jpg" width="300" height="300"/>
</td>
</tr>
<tr>
<td>
2change3.jpg
</td>
<td>
<img src="./2change3.jpg" alt="2change3.jpg(因为二进制数据对称交换,无法使用图片格式打开)" width="300" height="300"/>
</td>
</tr>
<tr>
<td>
2Revert.jpg
</td>
<td>
<img src="./2Revert.jpg" alt="2Revert.jpg" width="300" height="300"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work074
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 读取二进制数据
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
protected byte[] ReadBinaryData(string fileName)
{
byte[] bytes = null;
//文件是否存在
if(System.IO.File.Exists(fileName))
{
//新建文件
System.IO.FileInfo file1 = new System.IO.FileInfo(fileName);
//打开文件,
System.IO.FileStream stream1 = file1.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
//建立文件长度的字节数组
bytes = new byte[file1.Length];
//从文件读入流中
stream1.Read(bytes, 0, bytes.Length);
//关闭流
stream1.Close();
}
return bytes;
}
/// <summary>
/// 交换数据
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
protected byte[] ExchangeByte(byte[] bytes)
{
byte[] result = null;
if (bytes != null)
{
int length = bytes.Length;
result = new byte[length];
//首尾交换
for (int i = 0; i <= (length + 1) / 2; i++)
{
result[i] = bytes[length - i - 1];
result[length - i - 1] = bytes[i];
}
}
return result;
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//根目录,放一个图片
string path = Server.MapPath("~/1.jpg");
byte[] bytes = ReadBinaryData(path);
if (bytes != null)
{
//新建一个图片路径,根目录
string newPath = Server.MapPath("~/2.jpg");
System.IO.FileStream stream = System.IO.File.Open(newPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
//生成文件
stream.Write(bytes,0,bytes.Length);
stream.Close();
}
}
/// <summary>
/// 交换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/2.jpg");
byte[] bytes = ReadBinaryData(path);
if (bytes != null)
{
byte[] result = ExchangeByte(bytes);
string newPath = Server.MapPath("~/2change3.jpg");
System.IO.FileStream stream = System.IO.File.Open(newPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
stream.Write(result, 0, result.Length);
stream.Close();
}
}
/// <summary>
/// 还原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/2change3.jpg");
byte[] bytes = ReadBinaryData(path);
if (bytes != null)
{
byte[] result = ExchangeByte(bytes);
string newPath = Server.MapPath("~/2Revert.jpg");
System.IO.FileStream stream = System.IO.File.Open(newPath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
stream.Write(result,0,result.Length);
stream.Close();
}
}
}
}