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);
這樣就完成了一次添加,你要多個圖片只要重復就可以了,呵呵希望可以幫到您,如有任何疑問,歡迎網路聯系!