1. c#创建多个动态控件并在控件中添加多张图片
privatevoidDemo()
{
intPicCount=10;
for(inti=0;i<PicCont;i++)
{
stringpicFileName=Application.StartUpPath+@"ImagePic"+i.ToString();//图片文件名路路径
PictureBoxpic=newPictureBox();
pic.Name="PicBox"+i.ToString();
pic.Size=newSystem.Drawing.Size(20,20);
PointpicLocatoin=newPoint(0,i*20);
pic.Location=picLocation;
//加载图片
pic.Image=newBitmap(picFileName);
//窗体类加子控件添加该picBox控件
this.Controls.Add(pic);
}
}
2. VB中如何动态添加多个picturebox控件
'Dim WithEvents NewText As TextBox
Private Sub Command1_Click()
For i = 1 To 4
sname = "Txt" & i
Set NewText = Controls.Add("VB.TextBox", sname)
NewText.Move 2000, 500 * i, 1000, 360
Controls(sname).Text = "文本框" & i
NewText.Visible = True
Next i
End Sub
Private Sub Command2_Click()
For i = 1 To 4
sname = "Cmdnew" & i
Set NewButton = Controls.Add("VB.CommandButton", sname)
NewButton.Move 500, 500 * i, 1000, 360
Controls(sname).Caption = "按钮" & i
NewButton.Visible = True
Next i
End Sub
Private Sub Command3_Click()
For i = 1 To 4
BqName = "Lbl" & i
Set Newlbl = Controls.Add("VB.Label", BqName)
Newlbl.Move 3500, 500 * i, 1000, 360
Controls(BqName).Caption = "标签" & i
Newlbl.Visible = True
Next i
End
'Set Newlbl = Controls.Add("VB.Label", BqName)中,Label可替换成工具箱中任意控件类名
更多VB代码可参阅我的博客:http://hi..com/zgmg/blog/item/c1a68eec0fabdb2b63d09f5b.html#
3. C#如何动态添加控件
举个最简单的例子
比如我要不定量的上传图片
可以这样在前台添加控件
<script>
function AddAttachments()
{document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments'); newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'><input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index)
{
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件"; }
</script>
<a id="attach" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
4. c#如何连续动态增加控件
我刚好做过!
源码给你:
<%@ Page Language="C#" Theme="Glass" AutoEventWireup="true" CodeFile="JournalAdd1.aspx.cs" Inherits="admin_Activity_JournalAdd1" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v7.3, Version=7.3.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v7.3, Version=7.3.3.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<!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>
<script type="text/javascript">
var i=2;
function addFile()
{
//if (i< 9)
//{
var str = '<br><span>第'+i+'页</span><span class="goLeft"><input type="file" name="File" style="width: 300px" runat="server"/></span>';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
//}
//else
//{
//alert("您一次最多只能上传8张图片!");
i++;
//}
}
</script>
<style type="text/css">
.goLeft
{
margin-left:20px;
}
</style>
<link href="../../Css/css.css" rel="stylesheet" type="text/css" />
</head>
<body style="text-align:center; margin-left:auto;margin-right:auto; width:100%" bgcolor="aliceblue">
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="color: #111111;" width="100%">
<tr style="height: 30px;">
<td width="3%" style="background-image: url('../../Images/topbg.jpg'); height: 30px;"
align="center">
<img alt="" height="16" src="../../Images/icon/right.gif" /></td>
<td style="background-image: url('../../Images/topbg.jpg'); width: 150; height: 30px;" align="left">
<b>会刊添加</b></td>
<td style="background-image: url('../../Images/topbg.jpg'); height: 30px;" align="right">
</td>
</tr>
</table>
<table style="width: 100%; height:auto; text-align:center;">
<tr>
<td style="width:60%; height: 26px;" align="left">
<p>
<span style="margin-left:68px">期号:</span><span style="margin-left:9px">
<asp:TextBox ID="txt1" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt1"
ErrorMessage="必填"></asp:RequiredFieldValidator></span></p>
</td>
<td align="left" style="width:40%; height: 26px;" valign="bottom"></td>
</tr>
<tr>
<td style="width:60%">
<p id="MyFile">
<span>第1页</span><span class="goLeft"><input type="file" name="File" style="width: 300px" id="File1" runat="server"/></span>
</p>
</td>
<td align="left" style="width:40%" valign="bottom">
<input onclick="addFile()" type="button" value="增加"/></td>
</tr>
<tr>
<td style="width:50%; text-align:center" colspan="2">
<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="保存" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
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 myLib.PublicMole;
public partial class admin_Activity_JournalAdd1 : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
/// <summary>
/// 保存
/// </summary>
private void SetData()
{
bool judge = true;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile filePicture = files[i];
string str = "";
str = SaveFile(filePicture);
if (str == "0")
{
Javascript.Alert("图片上传失败!\\r原因:\\r 选择的图片大小为0.");
judge = false;
break;
}
else if (str == "1")
{
Javascript.Alert("图片上传失败!\\r原因:\\r 选择的文件不能超过5M.");
judge = false;
break;
}
else if (str == "2")
{
Javascript.Alert("图片上传失败!\\r原因:\\r 请选择后缀名为" + ConfigurationManager.AppSettings["ImgType"].ToString() + "的文件.");
judge = false;
break;
}
}
if (judge)
{
try
{
for (int i = files.Count - 1; i >= 0 ; i--)
{
System.Web.HttpPostedFile filePicture = files[i];
string FileType; // 上传文件类型(扩展名)
FileType = System.IO.Path.GetExtension(filePicture.FileName).ToLower();
string sFileName = Guid.NewGuid().ToString() + FileType;
filePicture.SaveAs(GetFilePathSave() + "\\" + sFileName);//保存图片
string sql = string.Format("INSERT INTO 会刊详细信息([图片],期号,标题)VALUES('{0}','{1}','{2}')", sFileName, txt1.Text.Trim(),"第"+(i+1)+"页");
db.ExecuteSql(sql);
}
Javascript.AlertAndRedirect("保存成功!", "JournalList1.aspx");
}
catch
{
Javascript.Alert("保存失败!");
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOK_Click(object sender, EventArgs e)
{
SetData();
}
protected string SaveFile(System.Web.HttpPostedFile file)
{
string FileType; // 上传文件类型(扩展名)
string ConfigType = ConfigurationManager.AppSettings["ImgType"].ToString();
string ConfigSize = ConfigurationManager.AppSettings["ImgSize"].ToString();
ConfigSize = (ConfigSize == null ? "0" : ConfigSize);
ConfigType = (ConfigType == null ? "" : ConfigType);
// 如果文件长度为0字节
if (file.ContentLength == 0)
return "0";
// 如果文件超过最大长度5000000
if (file.ContentLength > int.Parse(ConfigSize))
return "1";
// 获取文件扩展名
FileType = System.IO.Path.GetExtension(file.FileName).ToLower();
if (ConfigType.IndexOf(FileType) == -1)
return "2";
return "";
}
}
5. 用FileUpload控件怎么实现多图片上传
单存的FileUpload控件无法实现多图片上传!要做成那种多选效果一般现在用 as编写的flash控件!效果相当不错!!你可以在网上找找
6. C# 一个控件上传多张图片,求具体实现方法
主程序功能实现,详细功能见附件,内容太多。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
/// <summary>
/// FileUpload1.HasFile 如果是true,则表示该控件有文件要上传
/// FileUpload1.FileName 返回要上传文件的名称,不包含路径信息
/// FileUpload1.FileContent 返回一个指向上传文件的流对象
/// FileUpload1.PostedFile 返回已经上传文件的引用
/// FileUpload1.PostedFile.ContentLength 返回上传文件的按字节表示的文件大小
/// FileUpload1.PostedFile.ContentType 返回上传文件的MIME内容类型,也就是文件类型,如返回"image/jpg"
/// FileUpload1.PostedFile.FileName 返回文件在客户端的完全路径(包括文件名全称)
/// FileUpload1.PostedFile.InputStream 返回一个指向上传文件的流对象
/// FileInfo对象表示磁盘或网络位置上的文件。提供文件的路径,就可以创建一个FileInfo对象:
/// </summary>
public partial class BackManagement_ImagesUpload : System.Web.UI.Page
{
public string treePath = "";
public int imageW = 100;
public int imageH = 100;
protected void Page_Load(object sender, EventArgs e)
{
this.Button5.Attributes.Add("Onclick", "window.close();"); //在本地关闭当前页,而不需要发送到服务器去关闭当前页时
if (!Page.IsPostBack)
{
Label2.Text = Server.MapPath("/");
TextBox3.Text = "ImageUpload";
treePath = Server.MapPath("/") + TextBox3.Text.Trim() + "/";
TextBox4.Text = imageW.ToString();
TextBox5.Text = imageH.ToString();
}
}
protected void btnload_Click(object sender, EventArgs e)
{
//如果保存图片的目录不存在,由创建它
treePath = Server.MapPath("/") + TextBox3.Text.Trim() + "/";
imageW = Convert.ToInt32(TextBox4.Text.ToString());
imageH = Convert.ToInt32(TextBox5.Text.ToString());
if (!File.Exists(treePath + "images")) //如果/ImageUpload/images不存在,则创建/ImageUpload/images,用于存放源图片
{
System.IO.Directory.CreateDirectory(treePath + "images");
}
if (!File.Exists(treePath + "thumbnails")) //如果/ImageUpload/thumbnails不存在,则创建/ImageUpload/thumbnails,用于存放缩略图片
{
System.IO.Directory.CreateDirectory(treePath + "thumbnails");
}
if (!File.Exists(treePath + "textImages")) //如果/ImageUpload/textImages不存在,则创建/ImageUpload/textImages,用于存文字水印图片
{
System.IO.Directory.CreateDirectory(treePath + "textImages");
}
if (!File.Exists(treePath + "waterImages")) //如果/ImageUpload/waterImages不存在,则创建/ImageUpload/waterImages
//用于存图形水印图片
{
System.IO.Directory.CreateDirectory(treePath + "waterImages");
}
if (FileUpload1.HasFile) //如果是true,则表示该控件有文件要上传
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName; //返回文件在客户端的完全路径(包括文件名全称)
FileInfo file = new FileInfo(name); //FileInfo对象表示磁盘或网络位置上的文件。提供文件的路径,就可以创建一个FileInfo对象:
string fileName = file.Name; // 文件名称
string fileName_s = "x_" + file.Name; // 缩略图文件名称
string fileName_sy = "text_" + file.Name; // 水印图文件名称(文字)
string fileName_syp = "water_" + file.Name; // 水印图文件名称(图片)
string webFilePath = treePath + "images/" + fileName; // 服务器端文件路径
string webFilePath_s = treePath + "thumbnails/" + fileName_s; // 服务器端缩略图路径
string webFilePath_sy = treePath + "textImages/" + fileName_sy; // 服务器端带水印图路径(文字)
string webFilePath_syp = treePath + "waterImages/" + fileName_syp;// 服务器端带水印图路径(图片)
string webFilePath_sypf = Server.MapPath("../images/tzwhx.png"); // 服务器端水印图路径(图片)
if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
if (CheckBox1.Checked) //是否生成文字水印图
{
AddWater(webFilePath, webFilePath_sy);
}
if (CheckBox2.Checked) //是否生成图形水印图
{
AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);
}
MakeThumbnail(webFilePath, webFilePath_s, imageW, imageH, "Cut"); // 生成缩略图方法
Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
Image1.ImageUrl = "/" + TextBox3.Text.ToString() + "/images/" + fileName;
TextBox1.Text = webFilePath;
TextBox2.Text = "/" + TextBox3.Text.ToString() + "/images/" + fileName;
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}
}
7. c#,如何使用数组动态的往多个picture控件一一添加图片
1. 借助timer控件,当点击按钮的时候激活timer事件,然后在timer事件里面组织显示图片就可以了。图片呢,可以把名字存入一个数组,在timer的滴答事件中循环调用,每触发一次数组就移动一个索引,直到最后一个为止。
2 . 对于多线程的话,实现这种功能用多线程就是大材小用。
不过也可以,可以把显示图片的方法用线程去调用显示图片,调用完毕之后线程会自动停止,然后
8. asp.net动态添加控件
在Net程序开发中,有时上传功能会实现让用户上传多个图片或文件,一个一个上传肯定有点麻烦,而且还不人性化。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了。下面这种方法,在上传的地方加一个按钮,如果用户想上传几个就点几次,这样就会出现多个上传框,让他选择,好了,不多说了。下面是代码:
前台代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>文件上传_IT知道网itwis.com</title>
<script language="javascript" type="text/javascript">
function addFile(max)
{
var file = document.getElementsByName("File");
alert(file.length);
if(file.length==1 && file[0].disabled==true)
{
file[0].disabled = false;
return;
}
if(file.length<max)
{
var fileButton = '<br /><input type="file" size="50" name="File" />';
alert(fileButton);
document.getElementById("FileList").insertAdjacentHTML("beforeEnd",fileButton);
}
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p id="FileList">
<input type="file" disabled="disabled" size="50" name="File" />
</p>
<input type="button" value='增加一个文件' onclick="addFile(<%=MaxFileCounts%>)" />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
这里有一点要注意的是<input type="button" value='增加一个文件' onclick="addFile(<%=MaxFileCounts%>)" />这里面的MaxFileCounts。这个是用户上传最大数的一个限制。你可以在这里写成死值,也可以在CS进行配置,主要看你的需求。
下面是cs文件了
这里的MaxFileCount是在我配置文件写的,你可以根据你的情况写。
public int MaxFileCounts = MaxFileCount;
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection fileList = HttpContext.Current.Request.Files;
if (fileList == null)
{
return;
}
FileImage file = new FileImage();//这是自定义的一个写库的类,可根据实际情况自我定义。
try
{
for (int i = 0; i < fileList.Count; i++)
{
HttpPostedFile postedFile = fileList[i];
if (postedFile == null)
continue;
string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
if (string.IsNullOrEmpty(extension) == true)
continue;
bool flag = false;
foreach (string ext in AllowFileList)
{
if (ext == extension.ToLower())
{
flag = true;
}
}
if (flag == false)
continue;
string storeUrl = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + extension.ToString();
string Url = storeFilePath + storeUrl;
string fullPath = Server.MapPath(Url);
postedFile.SaveAs(fullPath);
Hashtable ht = new Hashtable();
ht.Add("Title",fileName);
ht.Add("imgUrl",storeUrl);
ht.Add("imgType",postedFile.ContentType);
ht.Add("imgSize",postedFile.ContentLength);
file.insertImage(ht); //这里是我的添加语句,你可写成你自己的。
}
}
catch (Exception ex)
{
this.Label1.Text = ex.Message;
}
}
本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20081024/2627.html
9. C#如何同时将已知路径的多张图片分别添加到多个动态生成的按钮上
privateSystem.Windows.Forms.Buttonbutton1;
//按你自己的要求动态实例化按钮控件
this.button1.Location=newSystem.Drawing.Point(807,440);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(75,23);
this.button1.TabIndex=8;
this.button1.Text="button1";
//添加指定路径图片值按钮控件
stringstrPath="X:xxxx.xxx";//图片路径
this.button1.Image=newBitmap(strPath);
10. c# 窗体的控件中加载多个图片的问题
如果是静态的话很好实现,拖放一个PictureBox,然后点击右上方的智能按钮(黑色小三角),根据提示选择图片路径就可以了。
但是我相信你要的是动态添加,呵呵:
先在窗体上添加一个Panel,命名为pnl
PictureBox pic= new PictureBox();
pic.Image=Image.FromFile("图片的绝对路径+扩展名");
pnl.Controls.Add(pic);
这样就完成了一次添加,你要多个图片只要重复就可以了,呵呵希望可以帮到您,如有任何疑问,欢迎网络联系!