⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dragdropimage.aspx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Example_12_26
{
	/// <summary>
	/// DragDropImage 的摘要说明。
	/// </summary>
	public class DragDropImage : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblHeader;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.TextBox txtContX;
		protected System.Web.UI.WebControls.Label lblContY;
		protected System.Web.UI.WebControls.TextBox txtContY;
		protected System.Web.UI.WebControls.Button btnDrawContainers;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
		protected System.Web.UI.WebControls.Image Image1;
		protected System.Web.UI.WebControls.Table Table1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}

		protected override void Render(HtmlTextWriter writer)
		{
			StringBuilder sbWater = new StringBuilder();
			sbWater.Append("style=\"");
			sbWater.Append("background-attachment: scroll;	background-image: url(images/demo.gif); background-repeat: repeat; background-color: transparent;\"");
		
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			System.IO.StringWriter sw = new System.IO.StringWriter(sb);
			HtmlTextWriter newWriter = new HtmlTextWriter(sw);
               
			base.Render(newWriter);
			
			sb.Replace("<body", "<body " + sbWater.ToString());
			sb.Replace("<BODY", "<BODY " + sbWater.ToString());

			Response.Write(sb.ToString());
		}

		/// <summary>
		/// This is another method that would generate the 
		/// boxes to be dragged/dropped on the UI.
		/// </summary>
		/// <param name="containersX"></param>
		/// <param name="containersY"></param>
		private void DrawGrid(int containersX, int containersY)
		{
			// background color.
			Color bgColor = Color.Blue;

			// color for an empty container.
			Color emptyContainerColor = Color.AliceBlue;

			// color for a filled container.
			Color filledContainerColor = Color.Cornsilk;


			Font aFont = new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Pixel); 
			Color aFontColor = Color.Black;
			StringFormat sFormat = new StringFormat();
			sFormat.Alignment = StringAlignment.Center; 
			
			const int squareSize = 40;
			int squareCenter = squareSize / 2;
			
			int totalContainers = containersX * containersY;
			
			int gridWidth = containersX * squareSize;
			int gridHeight = containersY * squareSize;

			// create the Bitmap
			Bitmap bitmap = new Bitmap(gridWidth, gridHeight);
			
			Graphics objGraphics = Graphics.FromImage(bitmap);
			objGraphics.Clear(bgColor);

			//Set some SmoothingMode so that we arnt all pixelated. 
			objGraphics.SmoothingMode = SmoothingMode.HighQuality;

			int currX = 0;
			int currY = 0;

			// Loop through the total number of containers.
			for(int i = 0;i < totalContainers; i++)
			{
				 
				int currCount = i + 1;

				// Even number?
				if(i%2 == 0)
				{
					objGraphics.FillRectangle(new SolidBrush(emptyContainerColor),currX, currY, squareSize, squareSize);
				}
				else
				{
					objGraphics.FillRectangle(new SolidBrush(filledContainerColor),currX, currY, squareSize, squareSize);
				}

				objGraphics.DrawString(currCount.ToString(), aFont, new SolidBrush(aFontColor), currX + squareCenter, currY + squareCenter, sFormat);

				
				if(currX >= (gridWidth - squareSize))
				{
					currX = 0;
					currY += squareSize;
				}
				else
				{
					currX += squareSize;
				}
			
			}
			// Save the image to the OutputStream
			Response.ContentType = "image/jpeg";
			bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
			
			// clean up...
			bitmap.Dispose();
		}

		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
			int x = Convert.ToInt32(this.txtContX.Text);
			int y = Convert.ToInt32(this.txtContY.Text);
			DrawGrid(x,y);
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			this.Image1.Visible = true;
			this.Image1.ImageUrl = "RetrieveImage.aspx";
			
		}

		private void SetImage()
		{
			// create the Bitmap
			Bitmap bitmap = new Bitmap(50, 50);
			bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
		}

		private void DrawContainers()
		{
			TableRowCollection trCol = this.Table1.Rows;
			TableRow tr = null;
			
			// Should we continue?
			if(this.txtContX.Text == null || this.txtContY.Text == null)
				return;
			
			// Size of the row.
			int rowSize = Int32.Parse(this.txtContX.Text); 
			// Number of rows.
			int rowNumber = Int32.Parse(this.txtContY.Text);
			// Total number of containers.
			int numberOfContainers = rowSize * rowNumber;
			// Boolean value for empty table cells.
			bool isEmpty = false;
			
			// Loop through all of the containers.
			for(int i=0; i< numberOfContainers; i++)
			{
				// new row mod.
				int newRow =  i % rowSize;
				
				// Should we create a new row?
				if(tr == null || newRow == 0)
				{
					tr = new TableRow();
					trCol.Add(tr);
				}

				// Empty cell generator.
				if((i+1)%17==0)
				{
					isEmpty = true;
				}
				else
				{
					isEmpty = false;
				}

				// Set the cell collection.
				TableCellCollection tdc = tr.Cells;

				// Create a new table cell.
				TableCell td = new TableCell();
				td.ID = "cell_" + i.ToString();
				
				// Set the cell backcolor.
				td.BackColor = Color.Turquoise;
				// Set the cell's class.
				td.CssClass = "SpecimenLoc";

				td.Attributes.Add("SpecimenId", "");

				// Is the cell empty?
				if(!isEmpty)
				{
					td.Attributes.Add("SpecimenId", i.ToString());
					td.BackColor = Color.Beige;
					td.Text = i.ToString();
				}
				
				// Add javascript attributes to the cell.
				td.Attributes.Add("target", "true");
				td.Attributes.Add("onmousedown", "BeginDrag(this.id);");
				td.Attributes.Add("onmouseup", "CancelDrag();"); 
				td.Attributes.Add("onmouseover", "setTarget(this.id);this.style.cursor='hand';");
				td.Attributes.Add("onmouseout", "this.style.cursor='default';");
				td.Width = Unit.Pixel(35);
				td.Height = Unit.Pixel(35);

				// Add the cell to the cell collection.
				tdc.Add(td);
			}
		}

		private void btnDrawContainers_Click(object sender, System.EventArgs e)
		{
			// Draw the containers.
			this.DrawContainers();
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnDrawContainers.Click += new System.EventHandler(this.btnDrawContainers_Click);
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -