实现预览功能图片如何处理?? 预览功能图片如何显示的??必须要先上传吗 我不上传直接获取file的值给图片src这样不行呵呵~!大家怎么做的 请教了?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 低版本IE可以,高版本IE或FF有限制,不过有人想办法解决了大多数浏览器的问题,可以在客户端预览 ASP.NET带上传有图片预览功能<HTML><HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript">function viewmypic(mypic,imgfile) {if (imgfile.value){mypic.src=imgfile.value;mypic.style.display="";mypic.border=1;}} </script></HEAD><body MS_POSITIONING="GridLayout"> <form> <INPUT style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 88px" type="file" id="imgfile" name="imgfile" runat="server"> </form> <img name="showimg" id="showimg" src="" style="display:none;" alt="预览图片" /> <br /> </div> <div style="display:none"> </div></body></HTML>b.index.aspx.cs代码程序代码private void Page_Load(object sender, System.EventArgs e){if(!this.Page.IsPostBack){this.imgfile.Attributes.Add("onchange","viewmypic(showimg,this.form.imgfile);");}} 你可以给你个image控件file的值获取时,就是onchange事件里面指定image的url先让image显示出来 <script language="javascript" type="text/javascript"> function PreviewImg(imgFile) { var newPreview = document.getElementById("newPreview"); newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value; newPreview.style.width = "80px"; newPreview.style.height = "60px"; } </script> <asp:FileUpload ID="FileUpload1" runat="server" onchange="PreviewImg(this)" /> <div id="newPreview"> </div> function $(o){return document.getElementById(o);} function CheckImg(o,img) { if (!/\.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value)) { alert('只能上传jpg,bmp,gif,png格式图片!'); o.outerHTML = o.outerHTML; } else { $(img).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=o.value; } } <asp:FileUpload ID="FileUpload1" runat="server" onchange="CheckImg(this, 'img');" /> <div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src= <%= pic%>,sizingMethod=scale);width:88px;height:113px;"> </div> public string pic=""; http://topic.csdn.net/u/20091102/13/284b7868-f996-4dae-9eba-c94c36ab526e.html div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src= F:\aaHTML\images\ico_2.gif,sizingMethod=scale);width:88px;height:113px;"> </div>这样输出后不行的 private void Page_Load(object sender, System.EventArgs e) { if(!this.IsPostBack) { string filename = this.Request.QueryString[ "filename "]; if(filename + "a " != "a ") { string suffix = filename.Substring(filename.LastIndexOf( ". ")); this.Image1.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename); this.Image2.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename.Replace(suffix, "w " + suffix)); this.Image3.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename.Replace(suffix, "x " + suffix)); } } } /// <summary> /// 上传图片 /// </summary> /// <param name= "sender "> </param> /// <param name= "e "> </param> private void Button1_Click(object sender, System.EventArgs e) { System.Web.HttpPostedFile myPost = this.Request.Files[0]; if(myPost.ContentLength !=0) { string uploadpath = this.Server.MapPath( "uploadfile "); string tmpfilename = myPost.FileName; //文件名 string filename = tmpfilename.Substring(tmpfilename.LastIndexOf( "\\ ") + 1); //原文件的保存路径 string fileSavePath = uploadpath + "\\ " + filename; //保存原图片 myPost.SaveAs(fileSavePath); this.toImage(myPost.InputStream,uploadpath,filename); this.Response.Redirect( "picfont.aspx?filename= " + filename); this.Response.End(); } } private void toImage(System.IO.Stream myStream,string uploadPath,string picName) { //后缀名 string suffix = picName.Substring(picName.LastIndexOf( ". ")); //缩略图的保存路径 string fileXltPath = uploadPath + "\\ " + picName.Replace(suffix, "x " + suffix); //写字图的保存路径 string fileXztPath = uploadPath + "\\ " + picName.Replace(suffix, "w " + suffix); //创建一个图像对象取得上传图片对象 System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream,false); //对绘制前的图片产生一个缩略图(原图片一半大小) System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width/2,myImage.Size.Height/2,null,System.IntPtr.Zero); //保存缩略图 thumbImage.Save(fileXltPath,this.getImageFormat(suffix)); //关闭缩略图对象 thumbImage.Dispose(); //创建绘制对象 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage); g.DrawImage(myImage,0,0,myImage.Size.Width,myImage.Size.Height); //选择字体及字体大小 System.Drawing.Font f = new Font( "隶书 ",140); //定义字体颜色 System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Red); //开始绘制,根据上述两种设定,添加绘制的上左位置 g.DrawString( "张三李四 ",f,b,10,10); //关闭绘制对象 g.Dispose(); //保存绘制后上传图片 myImage.Save(fileXztPath,myImage.RawFormat); //关闭图片对象 myImage.Dispose(); } /// <summary> /// 根据图片的后缀名,返回要保存的图片格式 /// </summary> /// <param name= "suffix "> 带.号的后缀名 </param> /// <returns> 返回System.Drawing.Imaging.ImageForma对象 </returns> private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix) { System.Drawing.Imaging.ImageFormat myFormat; switch(suffix.ToLower()) { case ".bmp ": myFormat = System.Drawing.Imaging.ImageFormat.Bmp; break; case ".emf ": myFormat = System.Drawing.Imaging.ImageFormat.Emf; break; case ".exif ": myFormat = System.Drawing.Imaging.ImageFormat.Exif; break; case ".gif ": myFormat = System.Drawing.Imaging.ImageFormat.Gif; break; case ".icon ": myFormat = System.Drawing.Imaging.ImageFormat.Icon; break; case ".jpeg ": case ".jpg ": myFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case ".png ": myFormat = System.Drawing.Imaging.ImageFormat.Png; break; case ".tiff ": myFormat = System.Drawing.Imaging.ImageFormat.Tiff; break; case ".wmf ": myFormat = System.Drawing.Imaging.ImageFormat.Wmf; break; default: myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp; break; } return(myFormat); } 下面这个是网上找的//调用示例 this.MakeSmallImg(f.PostedFile,this.Server.MapPath( "1.jpg "),600,500); //------------------------------------------------------------ public void MakeSmallImg(System.Web.HttpPostedFile postFile,string saveImg,System.Double Width,System.Double Height) { //原始图片名称 string originalFilename = postFile.FileName; //生成的高质量图片名称 string strGoodFile = saveImg; //从文件取得图片对象 System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream,true); System.Double NewWidth,NewHeight; if(image.Width> image.Height) { NewWidth=Width; NewHeight=image.Height*(NewWidth/image.Width); } else { NewHeight=Height; NewWidth=(NewHeight/image.Height)*image.Width; } if (NewWidth> Width) { NewWidth=Width; } if (NewHeight> Height) { NewHeight=Height; } //取得图片大小 System.Drawing.Size size = new Size((int)NewWidth,(int)NewHeight); //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空一下画布 g.Clear(Color.White); //在指定位置画图 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width,image.Height), System.Drawing.GraphicsUnit.Pixel); ///文字水印 //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap); //System.Drawing.Font f=new Font( "宋体 ",10); //System.Drawing.Brush b=new SolidBrush(Color.Black); //G.DrawString( "myohmine ",f,b,10,10); //G.Dispose(); ///图片水印 //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath( "pic/1.gif ")); //Graphics a = Graphics.FromImage(bitmap); //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); //copyImage.Dispose(); //a.Dispose(); //copyImage.Dispose(); //保存高清晰度的缩略图 bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose(); image.Dispose(); bitmap.Dispose(); } 高手指点,GridView绑定数据之后,再追加行,各位高手指点一下!在线等,急!急!!急!!! 年末了,抽奖问题,请各位请教~~~~~~~~~~速结贴 给定编码中的字符无效 C#如何实现把excel中的图片导出为一个jpg的图片呀 ASP.NET柱状图表 特急问题--“ ”(十六进制值 0x0B)是无效的字符 请教应用场景 怎么控制RequiredFieldValidator等控件的作用范围? 求助:程序里如何动态添加用户自定义控件 怎样实现动态的生成水晶报表?也就是数据库中的表名和字段都是不确定的? GridView的分页出问题了 HyperLink如何带URL参数?
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function viewmypic(mypic,imgfile) {
if (imgfile.value){
mypic.src=imgfile.value;
mypic.style.display="";
mypic.border=1;
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form>
<INPUT style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 88px" type="file" id="imgfile"
name="imgfile" runat="server">
</form>
<img name="showimg" id="showimg" src="" style="display:none;" alt="预览图片" />
<br />
</div>
<div style="display:none">
</div>
</body>
</HTML>
b.index.aspx.cs代码
程序代码
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.Page.IsPostBack)
{
this.imgfile.Attributes.Add("onchange","viewmypic(showimg,this.form.imgfile);");
}
}
file的值获取时,就是onchange事件里面指定image的url先让image显示出来
function PreviewImg(imgFile)
{
var newPreview = document.getElementById("newPreview");
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
newPreview.style.width = "80px";
newPreview.style.height = "60px";
}
</script>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="PreviewImg(this)" />
<div id="newPreview"> </div>
function $(o){return document.getElementById(o);}
function CheckImg(o,img)
{
if (!/\.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value))
{
alert('只能上传jpg,bmp,gif,png格式图片!');
o.outerHTML = o.outerHTML;
}
else
{
$(img).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=o.value;
}
} <asp:FileUpload ID="FileUpload1" runat="server" onchange="CheckImg(this, 'img');" />
<div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src= <%= pic%>,sizingMethod=scale);width:88px;height:113px;"> </div>
public string pic="";
{
if(!this.IsPostBack)
{
string filename = this.Request.QueryString[ "filename "];
if(filename + "a " != "a ")
{
string suffix = filename.Substring(filename.LastIndexOf( ". "));
this.Image1.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename);
this.Image2.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename.Replace(suffix, "w " + suffix));
this.Image3.ImageUrl = this.Server.MapPath( "uploadfile/ " + filename.Replace(suffix, "x " + suffix));
}
}
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpPostedFile myPost = this.Request.Files[0];
if(myPost.ContentLength !=0)
{
string uploadpath = this.Server.MapPath( "uploadfile ");
string tmpfilename = myPost.FileName;
//文件名
string filename = tmpfilename.Substring(tmpfilename.LastIndexOf( "\\ ") + 1); //原文件的保存路径
string fileSavePath = uploadpath + "\\ " + filename; //保存原图片
myPost.SaveAs(fileSavePath); this.toImage(myPost.InputStream,uploadpath,filename); this.Response.Redirect( "picfont.aspx?filename= " + filename);
this.Response.End();
}
}
private void toImage(System.IO.Stream myStream,string uploadPath,string picName)
{
//后缀名
string suffix = picName.Substring(picName.LastIndexOf( ". ")); //缩略图的保存路径
string fileXltPath = uploadPath + "\\ " + picName.Replace(suffix, "x " + suffix);
//写字图的保存路径
string fileXztPath = uploadPath + "\\ " + picName.Replace(suffix, "w " + suffix);
//创建一个图像对象取得上传图片对象
System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream,false);
//对绘制前的图片产生一个缩略图(原图片一半大小)
System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width/2,myImage.Size.Height/2,null,System.IntPtr.Zero);
//保存缩略图
thumbImage.Save(fileXltPath,this.getImageFormat(suffix));
//关闭缩略图对象
thumbImage.Dispose(); //创建绘制对象
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage);
g.DrawImage(myImage,0,0,myImage.Size.Width,myImage.Size.Height);
//选择字体及字体大小
System.Drawing.Font f = new Font( "隶书 ",140);
//定义字体颜色
System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Red);
//开始绘制,根据上述两种设定,添加绘制的上左位置
g.DrawString( "张三李四 ",f,b,10,10);
//关闭绘制对象
g.Dispose(); //保存绘制后上传图片
myImage.Save(fileXztPath,myImage.RawFormat);
//关闭图片对象
myImage.Dispose();
} /// <summary>
/// 根据图片的后缀名,返回要保存的图片格式
/// </summary>
/// <param name= "suffix "> 带.号的后缀名 </param>
/// <returns> 返回System.Drawing.Imaging.ImageForma对象 </returns>
private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
{
System.Drawing.Imaging.ImageFormat myFormat;
switch(suffix.ToLower())
{
case ".bmp ":
myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case ".emf ":
myFormat = System.Drawing.Imaging.ImageFormat.Emf;
break;
case ".exif ":
myFormat = System.Drawing.Imaging.ImageFormat.Exif;
break;
case ".gif ":
myFormat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".icon ":
myFormat = System.Drawing.Imaging.ImageFormat.Icon;
break;
case ".jpeg ":
case ".jpg ":
myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png ":
myFormat = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".tiff ":
myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
break;
case ".wmf ":
myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
break;
default:
myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
break;
}
return(myFormat);
} 下面这个是网上找的
//调用示例
this.MakeSmallImg(f.PostedFile,this.Server.MapPath( "1.jpg "),600,500);
//------------------------------------------------------------ public void MakeSmallImg(System.Web.HttpPostedFile postFile,string saveImg,System.Double Width,System.Double Height)
{ //原始图片名称
string originalFilename = postFile.FileName;
//生成的高质量图片名称
string strGoodFile = saveImg;
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream,true); System.Double NewWidth,NewHeight;
if(image.Width> image.Height)
{
NewWidth=Width;
NewHeight=image.Height*(NewWidth/image.Width);
}
else
{
NewHeight=Height;
NewWidth=(NewHeight/image.Height)*image.Width;
} if (NewWidth> Width)
{
NewWidth=Width;
}
if (NewHeight> Height)
{
NewHeight=Height;
} //取得图片大小
System.Drawing.Size size = new Size((int)NewWidth,(int)NewHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font( "宋体 ",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString( "myohmine ",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath( "pic/1.gif "));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); //copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose();
image.Dispose();
bitmap.Dispose();
}