如题:请问哪位大侠有 c# 上传图片显示图片的代码呀?
我在网上找了N久咳没找到合适的。哪位大侠帮帮忙呢。十分感激!!!
我在网上找了N久咳没找到合适的。哪位大侠帮帮忙呢。十分感激!!!
解决方案 »
- 我想用C#写一个音乐播放器
- ListView 拖拽数据....获取本地路径.
- 高分100, 获取异常,如何传递????????????????????
- 各位前辈,怎么样才可以用VS2005开发一个界面类似vs2005的东西?
- 可否自动生成泛型
- 如何屏蔽windows的系统快捷键,如ctrl+alt+del,C#实现
- 用C#+ORACLE和JAVA+ORACLE开发那个好些??
- 刚开发完一款Gantt图软件,有Web版和Windows版,用C#开发的,高兴散分!
- 高分请教一个继承问题谢谢
- .net平台中数据可以离线操作,那它是怎么样做到数据的正确性的呢
- gridcontrol的打印功能的问题。
- C#如何设置charset
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;
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("未找到当前文件");
}
}
}
<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>
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>");
} }
}
}
感谢楼上的代码。 不过不是我想要的效果和代码.. 我想要的是 点击 FileUpload选择一张图片后 ,img 或者 是 DIV 显示图片出来..是这样的效果.... 十分感谢您的回答呀..
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了
前台? 你要的是WinForm的 还是Web的
<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">
<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">
<asp:DropDownList ID="DropDownListType" runat="server"
DataSourceID="ObjectDataSourceCarType" DataTextField="CarTypeName"
DataValueField="CarTypeId" AppendDataBoundItems=true>
</asp:DropDownList>
<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">
<asp:Image ID="imgPhoto" runat="server" Height="100px" Width="100px" />
<asp:FileUpload ID="FileUploadPic" runat="server" onChange="showImag()" />
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="btuAddCar" runat="server" Height="26px"
onclick="btuAddCar_Click" Text="添加" />
<asp:Button ID="btnBack" runat="server" Text="返回" CausesValidation="False" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>