如题:请问哪位大侠有 c# 上传图片显示图片的代码呀? 
  我在网上找了N久咳没找到合适的。哪位大侠帮帮忙呢。十分感激!!!

解决方案 »

  1.   

    FileStream fs = new System.IO.FileStream(@"D:\My Documents\a.jpg", System.IO.FileMode.Open);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, (int)fs.Length);
                string s = Convert.ToBase64String(buf);//数据库要存的
                // fs.Close();
                byte[] b = Convert.FromBase64String(s);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
             //   System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                Bitmap img = new Bitmap(ms);
                pictureBox1.BackgroundImage = img;
      

  2.   

    我说的是在 选择 FileUpload控件实现  有一个img 或者 是 DIV 来显示图片..怎么实现的.十分谢谢.
      

  3.   


     private void btnupload_Click(object sender, EventArgs e)
            {
                DialogResult result= openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    if (openFileDialog1.CheckFileExists)
                    {
                        FileStream fs = new System.IO.FileStream(openFileDialog1.FileName, FileMode.Open);
                        byte[] buf = new byte[fs.Length];
                        fs.Read(buf, 0, (int)fs.Length);
                        string strDB = Convert.ToBase64String(buf);//数据库要存的
                        byte[] buff = Convert.FromBase64String(strDB);
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(buff);
                        Bitmap img = new Bitmap(ms);
                        pictureBox1.Image = img;
                    }
                    else
                    {
                        MessageBox.Show("未找到当前文件");
                    }
                }
            }
      

  4.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin_UpFile.aspx.cs" Inherits="Admin_Admin_UpFile" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>文件上传</title>    <script type="text/javascript">
        function UploadPageClose(txtlogo,strValue)
        {
    window.opener.document.getElementById(txtlogo).value = strValue;
    window.close();
        }
        
        function ModalPageHidden(ctrlID , strValue)
        {
    dialogArguments.document.getElementById(ctrlID).value = strValue;
    window.close();
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
         <div>
                <table cellpadding="0" cellspacing="0" class="twidth" width="570">
                    <tr>
                        <td align="center">
                            <div class="mframe">
                                <table cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td class="tl">
                                        </td>
                                        <td class="tm">
                                            <span class="tt">上传文件</span></td>
                                        <td class="tr">
                                        </td>
                                    </tr>
                                </table>
                                <table cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td >
                                        </td>
                                        <td  style="width: 505px" align="center">
                                            <asp:Panel runat="server" ID="panel1">
                                                <table cellpadding="3" cellspacing="3" width="90%">
                                                    <tr>
                                                        <td align="center" colspan="3">
                                                            支持文件类型:JPG,JPEG,GIF</td>
                                                    </tr>
                                                    <tr>
                                                        <td align="center" style="width: 81px">
                                                            选择文件:</td>
                                                        <td colspan="2">
                                                            <asp:FileUpload ID="FileUpload1" runat="server" Width="292px" /></td>
                                                    </tr>
                                                    <tr>
                                                        <td align="center" colspan="3">
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td align="center" colspan="3">
                                                            <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /></td>
                                                    </tr>
                                                </table>
                                            </asp:Panel>
                                            <asp:Panel runat="server" ID="panel2" Visible="false">
                                                <table id="UploadedTbl" runat="server" cellpadding="3" cellspacing="3" width="92%">
                                                    <tr>
                                                        <td align="center" colspan="2" style="color:Red">
                                                            文件上传成功。</td>
                                                    </tr>
                                                    <tr>
                                                        <td align="center">
                                                            文件地址:</td>
                                                        <td>
                                                            <input id="CopyTxt" runat="server" readonly="readonly" size="50" type="text" /></td>
                                                    </tr>
                                                    <tr>
                                                        <td align="center" colspan="2" style="height: 30px">
                                                            <input onclick="UploadPageClose('<%=controlID%>','<%=strReturnPath%>');" type="button" value="确 定"/>
                                                            <input onclick="window.open('../' + CopyTxt.value,'Preview')" type="button" value="预览图片"/>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </asp:Panel>
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>
    <script type="text/javascript">
    function DoCopy()
    {
    if (document.form1.CopyTxt.value != "")
    {
    document.form1.CopyTxt.select();
    textRange = document.form1.CopyTxt.createTextRange();
    textRange.execCommand("Copy");
    }
    }
    </script>
      

  5.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.IO;public partial class Admin_Admin_UpFile : System.Web.UI.Page
    {
        #region 定义变量
        protected static string controlID = string.Empty;
        protected static string strReturnPath = string.Empty;
        #endregion    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["controlID"] != null)
                {
                    controlID = Request.QueryString["controlID"].ToString();
                }
            }    }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //判断是否有文件
            if (this.FileUpload1.HasFile)
            {
                //文件物理路径
                string strPath = Server.MapPath("../Upload/") + FileUpload1.FileName;            //获取扩展名
                string strExt = Path.GetExtension(strPath);            if (strExt == ".jpg" || strExt == ".gif" || strExt == ".png" || strExt == ".ico" || strExt == ".swf")
                {
                    try
                    {
                        //上传文件
                        FileUpload1.SaveAs(strPath);                    //文件路径
                        strReturnPath = "Upload/" + FileUpload1.FileName;                    //显示文件地址
                        CopyTxt.Value = strReturnPath;                    //设置面板是否显示
                        panel1.Visible = false;
                        panel2.Visible = true;                }
                    catch
                    {                }            }
                else
                {
                    Response.Write("<script>alert('图片格式不正确!');</script>");
                }        }
        }
    }
      

  6.   


    感谢楼上的代码。   不过不是我想要的效果和代码..   我想要的是  点击 FileUpload选择一张图片后 ,img 或者 是 DIV 显示图片出来..是这样的效果....   十分感谢您的回答呀..
      

  7.   

    <script language="javascript">
    function PreviewPhotoatwidth(photo_file,img_object,imgwidth)
    {
    var fileext=photo_file.value.substring(photo_file.value.lastIndexOf("."),photo_file.value.length);
    fileext=fileext.toLowerCase();
    if ((fileext!='.jpg')&&(fileext!='.gif')&&(fileext!='.jpeg')&&(fileext!='.png')&&(fileext!='.bmp'))
    {
    alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");
    photo_file.focus();
    }
    else
    {
    img_object.src=photo_file.value;
    if (img_object.width>imgwidth)
    {
    img_object.width=imgwidth; 

    }
    }
            
    <asp:Image ID="picpreview" alt='预览' runat="server" Height="125" />
    <input id="flPhoto" type="file" size="13" name="FileType1" style="width: 99%" runat="server" onchange="javascript:PreviewPhotoatwidth(this,picpreview,100)"/>
    这样就实现你选一个图片 直接在IMG 显示出来。 。 然后在上传到服务器上。 SQL记下路径就OK了 
      

  8.   


    前台?  你要的是WinForm的  还是Web的
      

  9.   

    前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddCarInfo.aspx.cs" Inherits="admin_AddCarInfo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>添加车辆</title>
        <style type="text/css">
            .style1
            {
                width: 48%;
            }
            .style2
            {
                text-align: center;
            }
            .style3
            {
                font-size: x-large;
                font-weight: bold;
            }
            .style4
            {
                text-align: center;
                width: 152px;
            }
        </style>
        
        <script  language="javascript">
            function showImag()
            {
                document.getElementById("<%=imgPhoto.ClientID %>").src=document.getElementById("<%=FileUploadPic.ClientID %>").value;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <p style="text-align: center">
            <span class="style3">添加车辆</span><asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        </p>
        <div style="text-align: center">
        
            <table class="style1" border="1" 
                style="background-color: #66B3FF; width: 90%; height: 329px; z-index: 1;">
                <tr>
                    <td class="style4">
                        车辆名称</td>
                    <td style="text-align: left">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:TextBox ID="txtCarName" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                            ControlToValidate="txtCarName">请输入汽车名称</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="style4">
                        车辆类型</td>
                    <td style="text-align: left">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:DropDownList ID="DropDownListType" runat="server" 
                            DataSourceID="ObjectDataSourceCarType" DataTextField="CarTypeName" 
                            DataValueField="CarTypeId" AppendDataBoundItems=true>
                        </asp:DropDownList>
    &nbsp;<asp:Button ID="btnAddType" runat="server" onclick="btnAddType_Click" Text="添加类型" />
                        <asp:ObjectDataSource ID="ObjectDataSourceCarType" runat="server" 
                            SelectMethod="GetAllCarTypes" TypeName="TourismBLL.CarTypeManager">
                        </asp:ObjectDataSource>
                    </td>
                </tr>
                <tr>
                    <td class="style4">
                        租赁价格</td>
                    <td style="text-align: center">
                        <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>元/天
                     
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                            ControlToValidate="txtPrice">请输入租赁价格</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                            ControlToValidate="txtPrice" 
                            ValidationExpression="/^[0-9]+$/">价格必须是数字</asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="style4">
                        车辆图片</td>
                    <td style="text-align: left">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Image ID="imgPhoto" runat="server" Height="100px" Width="100px" />
                        <asp:FileUpload ID="FileUploadPic" runat="server" onChange="showImag()" />
                    &nbsp;
                        </td>
                </tr>
                <tr>
                    <td class="style2" colspan="2">
                        <asp:Button ID="btuAddCar" runat="server" Height="26px" 
                            onclick="btuAddCar_Click" Text="添加" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnBack" runat="server" Text="返回" CausesValidation="False" />
                    </td>
                </tr>
            </table>
        
        </div>
        </form>
    </body>
    </html>
      

  10.   

       恩  是的呢我做的那个  只能在IE6 效果。。在IE7.8  不支持了呀不知道 有没有更好的办法呢。
      

  11.   

      是web  网站的... 我说的前台 是  ASPX 页面呢。。
      

  12.   

    感谢  楼上贴出的代码您的代码和我的代码效果一样只能在 IE6下 可以 在IE7  ,8就不行了。还有更好的办法吗????????