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

📄 recreateimage.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.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Example_12_26
{
	/// <summary>
	/// ReCreateImage 的摘要说明。
	/// </summary>
	public class ReCreateImage : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			//DrawGridFromDTO();
			
			string specId;
			string boxSize;
			string empty;

			specId = Request.QueryString["specId"];
			boxSize = Request.QueryString["boxSize"];
			empty = Request.QueryString["empty"];

			if((specId == null) || (boxSize == null) || (empty == null))
			{
				return;
			}
			
			this.DrawStorageBox(specId, Int32.Parse(boxSize), bool.Parse(empty));

			
		}

		private BoxDTO GetBox()
		{
			BoxDTO box = new BoxDTO();
			box.BoxUnitsX = "10";
			box.BoxUnitsY = "10";
			
			int numOfUnits = Convert.ToInt32(box.BoxUnitsX) * Convert.ToInt32(box.BoxUnitsY);
			
			ArrayList arrUnits = new ArrayList();

			for(int i=0;i<numOfUnits;i++)
			{
				BoxUnitDTO boxUnit = new BoxUnitDTO();
				if((i+1)%17==0)
				{
					boxUnit.SpecimenId = "";
					boxUnit.IsEmpty = true;
				}
				else
				{
					boxUnit.SpecimenId = String.Format("s{0}", i + 1);
					boxUnit.IsEmpty = false;
				}
				
				arrUnits.Add(boxUnit);
			}

			Array arr = arrUnits.ToArray(typeof(BoxUnitDTO));
			box.BoxUnits = (BoxUnitDTO[])arr;

			return box;
		}

		private void DrawGridFromDTO()
		{
			BoxDTO box = this.GetBox();

			if(null == box)
				return;

			int containersX = Convert.ToInt32(box.BoxUnitsX);
			int containersY = Convert.ToInt32(box.BoxUnitsY);

			// background color.
			Color bgColor = Color.Blue;

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

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


			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;


			for(int i = 0;i < totalContainers; i++)
			{
				 
				string specId = "";
				
				bool empty = true;

				if(i < box.BoxUnits.Length)
				{
					specId = box.BoxUnits[i].SpecimenId;
					empty = box.BoxUnits[i].IsEmpty;
				}

				if(empty)
					objGraphics.FillRectangle(new SolidBrush(emptyContainerColor),currX, currY, squareSize, squareSize);
				else
					objGraphics.FillRectangle(new SolidBrush(filledContainerColor),currX, currY, squareSize, squareSize);

				
				objGraphics.DrawString(specId, 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();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="specId"></param>
		/// <param name="boxSize"></param>
		/// <param name="empty"></param>
		private void DrawStorageBox(string specId, int boxSize, bool empty)
		{
			int currX = 0;
			int currY = 0;
			int squareCenter = boxSize / 2;

			// background color.
			Color bgColor = Color.Blue;

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

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


			Font aFont = new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Pixel); 
			Color aFontColor = Color.Black;
			StringFormat sFormat = new StringFormat();
			sFormat.Alignment = StringAlignment.Center; 

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

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

			if(empty)
			{
				objGraphics.FillRectangle(new SolidBrush(emptyContainerColor),currX, currY, boxSize, boxSize);
				specId = "";
			}
			else
			{
				objGraphics.FillRectangle(new SolidBrush(filledContainerColor),currX, currY, boxSize, boxSize);
			}

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

			bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
			
			// clean up...
			bitmap.Dispose();
		}

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

⌨️ 快捷键说明

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