码迷,mamicode.com
首页 > 其他好文 > 详细

Base64与Image依据具体业务需求互转

时间:2015-06-09 17:26:41      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:base64

C#Base64篇:

Base64与Image互转篇:

TODO:

Base64与Image互转篇:主要讲解依据业务需求开发图片展示与图片保存

TODO:

1,新增页面,保存路径名

2,查看页面,数据来源于其它表,读取base64数据格式图片转换为图片显示

讲解篇:1,新增页面,保存路径名2,查看页面,数据来源于其它表,读取base64数据格式图片转换为图片显示

1,新增页面,保存路径名

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Event.aspx.cs" Inherits="HMFW.Web.Pages.AndroidAPP.Event" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <%--    <script src="/Js/jquery-1.4.1.min.js"></script>--%>
    <script src="/Js/AndroidAPP/jquery-1.7.2.js"></script>
    <script src="/Js/jquery.pager.js"></script>
    <link href="/css/AndroidAPP/Pager.css" rel="stylesheet" />
    <link href="/css/AndroidAPP/jqbase.css" rel="stylesheet" />
    <link href="/css/AndroidAPP/reset.css" rel="stylesheet" />
    <link href="/css/AndroidAPP/zyUpload.css" rel="stylesheet" />
    <script src="/Js/AndroidAPP/zyFile.js"></script>
    <script src="/Js/AndroidAPP/zyUpload.js"></script>
    <script src="/Js/My97DatePicker/WdatePicker.js"></script>
    <script src="/Js/AndroidAPP/Common.js"></script>
    <script src="/Js/AndroidAPP/Event.js"></script>
    <script src="/Js/jquery.form.js"></script>
    <script src="/Js/jquery.validate.js"></script>

    <style type="text/css">
        .add {
            border-top: 1px solid #EEEEEE;
            border-left: 1px solid #EEEEEE;
        }

            .add tbody td {
                border-right: 1px solid #EEEEEE;
                border-bottom: 1px solid #EEEEEE;
                font: 16px/36px "microsoft yahei";
                color: #333333;
                padding: 2px 5px;
            }

                .add tbody td input {
                    border: 1px solid #DDDDDD;
                    height: 30px;
                    width: 300px;
                    padding: 0px 5px;
                    font: 16px/30px "microsoft yahei";
                }

                .add tbody td textarea {
                    border: 1px solid #DDDDDD;
                    height: 100px;
                    width: 680px;
                    padding: 5px;
                    font: 14px/26px "microsoft yahei";
                    margin: 5px 0px;
                }

                .add tbody td input.ssbtn {
                    width: 100px;
                    height: 32px;
                    background: #0073c6;
                    border: none;
                    color: #FFFFFF;
                    font: 16px/32px "microsoft yahei";
                    margin: 5px 0px;
                }

                .add tbody td .red {
                    color: #FF0000;
                    padding-right: 5px;
                    font: bold 16px/36px "microsoft yahei";
                }
    </style>

    <script type="text/javascript">

        $(function () {//Img1..hidImg1
            $("input[id*='delPic']").click(function () {
                if (!confirm("是否确定删除图片?")) {
                    return false;
                }
            });

            $("#Img1").click(function () {
               
            });

        });

        function CheckUpLoad(obj) {
            var picPath = $(obj).val();
            if (picPath == "") {
                alert("请选择需要上传的图片!");
                return false;
            }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="jq-top">
            <div class="top-content">
                <a href="javascript:history.go(-1)" class="top-left-action"><span class="icon icon-back"></span></a>
                <h2 class="top-title">第一时间</h2>
                <a href="main.aspx" class="top-right-action"><span class="icon icon-home"></span></a>
            </div>
        </div>
        <table id="T_Content" width="900" border="0" align="center" cellpadding="0" cellspacing="0" class="add">
            <tbody>
                <tr>
                    <td width="30%" align="right">图片显示:</td>
                    <td>
                        <img id="Img1" runat="server" src="~/Images/AndroidAPP/zyUpload/add_img.png" width="100" height="100" alt="暂无图片" />
                        <input id="hidImg1" type="hidden" runat="server" />
                        <img id="Img2" runat="server" src="~/Images/AndroidAPP/zyUpload/add_img.png" width="100" height="100" alt="暂无图片" />
                        <input id="hidImg2" type="hidden" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td align="right">图片地址:</td>
                    <td>
                        <div id="upload">
                            <input type="file" id="file1" runat="server" />
                            <asp:Button ID="bthUpLoad1" runat="server" OnClientClick="return CheckUpLoad(file1)" OnClick="bthUpLoad1_ServerClick" Text="上传" Width="80" />
                            <asp:Button ID="delPic1" runat="server" Text="删除" Width="80" OnClick="delPic1_Click" />
                        </div>
                        <div id="Div1">
                            <input type="file" id="file2" runat="server" />
                            <asp:Button ID="bthUpLoad2" runat="server" OnClientClick="return CheckUpLoad(file2)" OnClick="bthUpLoad2_ServerClick" Text="上传" Width="80" />
                            <asp:Button ID="delPic2" runat="server" Text="删除" Width="80" OnClick="delPic2_Click" />
                        </div>
                        <%-- <div id="Div2">
                            <input type="file" id="File3" runat="server" />
                            <asp:Button ID="bthUpLoad3" runat="server" Text="上传" OnClick="bthUpLoad3_Click" Width="80" />
                        </div>
                        <div id="Div3">
                            <input type="file" id="File4" runat="server" />
                            <asp:Button ID="bthUpLoad4" runat="server" Text="上传" OnClick="bthUpLoad4_Click" Width="80" />
                        </div>--%>
                    </td>
                </tr>
                <tr>
                    <td align="right"><span class="red">*</span>事件标题:</td>
                    <td>
                        <input type="text" name="sEventTitle" id="sEventTitle" runat="server" />
                    </td>
                </tr>
                <!--
                <tr>
                    <td align="right">图片地址:</td>
                    <td>
                        <input type="text" name="textfield2" id="text1" />  <input type="submit" name="Submit" value="拍照" class="ssbtn" />  <input type="submit" name="Submit" value="浏览" class="ssbtn" /></td>
                </tr>
                -->
                <tr>
                    <td align="right"><span class="red">*</span>录入人:</td>
                    <td>
                        <input type="text" name="sInputPerson" id="sInputPerson" /></td>
                </tr>
                <tr>
                    <td align="right"><span class="red">*</span>录入日期:</td>
                    <td>
                        <asp:TextBox ID="dInputDate" runat="server" class="srk001" EnableTheming="false"
                            onclick="WdatePicker({maxDate:'%y-%M-#{%d}'})"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right"><span class="red">*</span>内容:</td>
                    <td>
                        <textarea name="sContent" id="sContent" cols="45" rows="5"></textarea></td>
                </tr>
                <tr>
                    <td align="right">备注:</td>
                    <td>
                        <textarea name="sMemo" id="sMemo" cols="45" rows="5"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="button" onclick="add();" id="btnSubmit" value="提交" class="ssbtn" />  
                        <input type="button" onclick="Back();" value="返回" class="ssbtn" />
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="listmain">
        </div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace HMFW.Web.Pages.AndroidAPP
{
    public partial class Event : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bthUpLoad1_ServerClick(object sender, EventArgs e)
        {

            string strPath = UpLoadPic(file1);
            if (string.IsNullOrEmpty(strPath))
                return;
            Img1.Src = strPath;
            hidImg1.Value = strPath;
        }

        protected void bthUpLoad2_ServerClick(object sender, EventArgs e)
        {
            string strPath = UpLoadPic(file2);
            if (string.IsNullOrEmpty(strPath))
                return;
            Img2.Src = strPath;
            hidImg2.Value = strPath;
        }

        protected string UpLoadPic(HtmlInputFile control)
        {
            if (string.IsNullOrEmpty(control.PostedFile.FileName))
                return "";
            //图片保存的文件夹路径
            string path = Server.MapPath("/upload/Android/");
            //每天上传的图片一个文件夹
            string folder = DateTime.Now.ToString("yyyyMMdd");
            //如果文件夹不存在,则创建
            if (!Directory.Exists(path + folder))
            {
                Directory.CreateDirectory(path + folder);
            }
            //上传图片的扩展名
            string type = control.PostedFile.FileName.Substring(control.PostedFile.FileName.LastIndexOf('.'));
            //保存图片的文件名
            string saveName = Guid.NewGuid().ToString() + type;
            //保存图片
            control.PostedFile.SaveAs(path + folder + "/" + saveName);
            return "/upload/Android/" + folder + "/" + saveName;
        }

        protected void delPic1_Click(object sender, EventArgs e)
        {
            if (File.Exists(Server.MapPath(Img1.Src)))
            {
                FileInfo fi = new FileInfo(Server.MapPath(Img1.Src));
                fi.Delete();
                hidImg1.Value = "";
            }
        }

        protected void delPic2_Click(object sender, EventArgs e)
        {
            if (File.Exists(Server.MapPath(Img2.Src)))
            {
                FileInfo fi = new FileInfo(Server.MapPath(Img2.Src));
                fi.Delete();
                hidImg2.Value = "";
            }
        }
    }
}

2,查看页面,数据来源于其它表,读取base64数据格式图片转换为图片显示

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EventHandleTempView.aspx.cs" Inherits="HMFW.Web.Pages.AndroidAPP.EventHandleTempView" %>

<!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>

        </div>
    </form>
</body>
</html>

using HMFW.BLL.SQJS.ZJWG;
using HMFW.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace HMFW.Web.Pages.AndroidAPP
{
    public partial class EventHandleTempView : System.Web.UI.Page
    {

        protected string SpicLength
        {
            get { return ViewState["spicLength"] == null ? string.Empty : ViewState["spicLength"].ToString(); }
            set { ViewState["spicLength"] = value; }
        }

        EventBLL ibll = new EventBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string imgid = Request.QueryString["imgid"];//图片id
                int id = Convert.ToInt32(Request.QueryString["id"]);//默认第一张图片
                int spicLength = Convert.ToInt32(Request.QueryString["spicLength"]);//指定图片流的长度 循环遍历显示指定图片(多次引用)
                SpicLength = spicLength.ToString();

                SQ_Event sqEventSearch = new SQ_Event();
                sqEventSearch.gID = new Guid(imgid);
                SQ_Event model = ibll.GetModel(sqEventSearch);
                try
                {
                    if (model != null && spicLength >= 2)
                    {
                        string base64Decode = model.sPicPath.Split(',')[id];
                        byte[] bt = Convert.FromBase64String(base64Decode);
                        System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                        Bitmap bitmap = new Bitmap(stream);
                        bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                Response.End();
            }
        }
    }
}


Base64与Image依据具体业务需求互转

标签:base64

原文地址:http://blog.csdn.net/hr1187362408/article/details/46427229

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!