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

📄 add.aspx.cs

📁 个人博客。。毕业设计````用来做毕业设计`
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WesternByte.MyBlog.Core.Blog;
using WesternByte.MyBlog.Core.Picture;
using WesternByte.MyBlog.Core.Category;

namespace WesternByte.MyBlog.Blog.Admin.Picture
{
	/// <summary>
	/// Add 的摘要说明。
	/// </summary>
	public class Add : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.RequiredFieldValidator valTitleRequired;
		protected System.Web.UI.WebControls.TextBox subject;
		protected System.Web.UI.WebControls.RequiredFieldValidator valftbBodyRequired;
		protected System.Web.UI.WebControls.DropDownList categoryID;
		protected System.Web.UI.WebControls.Button addButton;
		protected System.Web.UI.HtmlControls.HtmlInputFile image;
		protected System.Web.UI.WebControls.DataList pictureCategory;
		protected PictureDAO pDAO = new PictureDAO();
		protected CategoryDAO cDAO = new CategoryDAO();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if(Session["Blog"]!=null)
			{
				BlogVO bVO = (BlogVO)Session["Blog"];
				int cNo = cDAO.LoadCount(bVO.BlogID,"p");
				if(cNo == 0)
				{
					Page.Response.Redirect("/Error.aspx?flag=noPictureCategory");
				}
				else
				{
					if(!Page.IsPostBack)
					{
						BindData(bVO.BlogID);
					}
				}
			}
			else
			{
				Page.Response.Redirect("/Login.aspx");
			}
		}

		private void BindData(int BlogID)
		{
			//分类下拉列表
			DataSet cDs = cDAO.LoadList(BlogID,"p");
			categoryID.DataSource = cDs.Tables[0].DefaultView;
			categoryID.DataBind();

			//分类列表
			cDs.Tables[0].Columns.Add("CategoryStr");
			for(int i=0;i<cDs.Tables[0].Rows.Count;i++)
			{
				cDs.Tables[0].Rows[i]["CategoryStr"] = "<a href=\"Picture.aspx?CategoryID="+cDs.Tables[0].Rows[i]["CategoryID"].ToString()+"\">"+cDs.Tables[0].Rows[i]["Name"].ToString()+"</a>";
			}
			this.pictureCategory.DataSource = cDs.Tables[0].DefaultView;
			this.pictureCategory.DataBind();
		}

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

		}
		#endregion

		private void addButton_Click(object sender, System.EventArgs e)
		{
			if(Session["Blog"]!=null)
			{
				BlogVO bVO = (BlogVO)Session["Blog"];
				int cNo = cDAO.LoadCount(bVO.BlogID,"p");
				if(cNo == 0)
				{
					Page.Response.Redirect("/Error.aspx?flag=noPictureCategory");
				}
				else
				{
					string imgExt;
					System.Drawing.Image oriImg, newImg, resImg;

					string imageSavePath = Server.MapPath("/") + ConfigurationSettings.AppSettings["ImageSavePath"].ToString() + bVO.BlogID + "\\" + categoryID.SelectedValue + "\\";
					int imageThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["ImageThumbWidth"]);
					int imageResizeWidth = Int32.Parse(ConfigurationSettings.AppSettings["ImageResizeWidth"]);
					int imageThumbHeight = 0,imageResizeHeight = 0;
					string imageNewName = DateTime.Now.ToString("yyyyMMddHHmmss");
					int back = 0;
					try
					{
						if(Page.IsValid)
						{
							if(image.Value!="")
							{
								HttpPostedFile PostedFile = image.PostedFile;
								imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
								if(imgExt != ".jpg" && imgExt != ".gif" && imgExt != ".png")
								{
									back = - 2;
								}
								else
								{
									imageNewName += imgExt;
                                       
									oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
									if(oriImg.Width<imageThumbWidth&&oriImg.Height<imageThumbWidth)
									{
										imageThumbHeight = oriImg.Height;
										imageThumbWidth = oriImg.Width;
									}
									else
									{
										if(oriImg.Width<oriImg.Height)
										{
											imageThumbHeight = imageThumbWidth;
											imageThumbWidth = imageThumbHeight * oriImg.Width/oriImg.Height;
										}
										else if(oriImg.Width>oriImg.Height)
										{
											imageThumbHeight = imageThumbWidth * oriImg.Height/oriImg.Width;
										}
										else imageThumbHeight = imageThumbWidth;
									}
									newImg = oriImg.GetThumbnailImage(imageThumbWidth,imageThumbHeight ,null,new System.IntPtr(0));

									if(oriImg.Width<imageResizeWidth&&oriImg.Height<imageResizeWidth)
									{
										imageResizeHeight = oriImg.Height;
										imageResizeWidth = oriImg.Width;
									}
									else
									{
										if(oriImg.Width<oriImg.Height)
										{
											imageResizeHeight = imageResizeWidth;
											imageResizeWidth = imageResizeHeight * oriImg.Width/oriImg.Height;
										}
										else if(oriImg.Width>oriImg.Height)
										{
											imageResizeHeight = imageResizeWidth * oriImg.Height/oriImg.Width;
										}
										else imageResizeHeight = imageResizeWidth;
									}
									resImg = oriImg.GetThumbnailImage(imageResizeWidth,imageResizeHeight ,null,new System.IntPtr(0));

									switch(imgExt)
									{
										case ".jpg":
											oriImg.Save(imageSavePath + "o_" + imageNewName , System.Drawing.Imaging.ImageFormat.Jpeg);
											newImg.Save(imageSavePath + "t_" + imageNewName , System.Drawing.Imaging.ImageFormat.Jpeg);
											resImg.Save(imageSavePath + "r_" + imageNewName , System.Drawing.Imaging.ImageFormat.Jpeg);
											break;
										case ".gif":
											oriImg.Save(imageSavePath + "o_" + imageNewName , System.Drawing.Imaging.ImageFormat.Gif);
											newImg.Save(imageSavePath + "t_" + imageNewName ,System.Drawing.Imaging.ImageFormat.Gif);
											resImg.Save(imageSavePath + "r_" + imageNewName ,System.Drawing.Imaging.ImageFormat.Gif);
											break;
										case ".png":
											oriImg.Save(imageSavePath + "o_" + imageNewName , System.Drawing.Imaging.ImageFormat.Png);
											newImg.Save(imageSavePath + "t_" + imageNewName ,System.Drawing.Imaging.ImageFormat.Png);
											resImg.Save(imageSavePath + "r_" + imageNewName ,System.Drawing.Imaging.ImageFormat.Png);
											break;
									}//switch
									oriImg.Dispose();
									newImg.Dispose();
									resImg.Dispose();
								}
							}//if
						}
					}
					catch(Exception ex)
					{
						Page.Response.Redirect("/Error.aspx?flag=addpicture&opStr=" + ex.Message);
					}

					PictureVO pVO = new PictureVO();
					pVO.BlogID = bVO.BlogID;
					pVO.CategoryID = Convert.ToInt32(categoryID.SelectedValue);
					pVO.Subject = subject.Text;
					pVO.ImageUrl = imageNewName;
					back += pDAO.Insert(pVO);
					if(back>0)
					{
						Page.Response.Redirect("/Success.aspx?flag=addpicture");
					}
					else
					{
						Page.Response.Redirect("/Error.aspx?flag=addpicture");
					}
				}
			}
			else
			{
				Page.Response.Redirect("/Login.aspx");
			}
		}
	}
}

⌨️ 快捷键说明

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