asp.net代码练习 work073

258 篇文章 2 订阅

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值