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

📄 imageutility.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace ASPNET.StarterKit.Communities {

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Imaging;


    //*********************************************************************
    //
    // ImageUtility Class
    //
    // Contains static methods for working with images.
    //
    //*********************************************************************

	public class ImageUtility {



        //*********************************************************************
        //
        // GetSectionImage Method
        //
        // Returns a byte array containing a section image. 
        //
        //*********************************************************************

    	public static byte[] GetSectionImage(int sectionID, int imageID, int width, int height, bool isThumbnail) {
		    string contentType = String.Empty;
		    byte[] image = null;
            int returnWidth = -1;
            int returnHeight = -1;
            bool returnThumbnail = false;
		
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdGet = new SqlCommand("Community_ImagesGetSectionImage", conPortal);
			cmdGet.CommandType = CommandType.StoredProcedure;

			cmdGet.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
			cmdGet.Parameters.Add("@sectionID", sectionID);
			cmdGet.Parameters.Add("@imageID", imageID);
	   	    cmdGet.Parameters.Add("@width", width);
			cmdGet.Parameters.Add("@height", height);
		    cmdGet.Parameters.Add("@isThumbnail", isThumbnail);

			conPortal.Open();
			SqlDataReader dr = cmdGet.ExecuteReader();
			if (dr.Read()) {
			    contentType = (string)dr["Image_ContentType"]; 
			    image =  (byte[])dr["Image_ImageData"];
                returnWidth = (int)dr["Width"];
                returnHeight = (int)dr["Height"];
                returnThumbnail = Convert.ToBoolean(dr["IsThumbnail"]);
			}
			conPortal.Close();
			
			// if no resize, just return
			if (returnWidth == width && returnHeight == height && returnThumbnail == isThumbnail)
			 return image;

		    // Perform resize
		    image = ModifyImage(image, width, height, contentType, isThumbnail);

		    // Save Resized Image
			SqlCommand cmdSave = new SqlCommand("Community_ImagesSaveSizedSectionImage", conPortal);
			cmdSave.CommandType = CommandType.StoredProcedure;

			cmdSave.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
			cmdSave.Parameters.Add("@imageID", imageID);
	   	    cmdSave.Parameters.Add("@sizedImageWidth", width);
			cmdSave.Parameters.Add("@sizedImageHeight", height);
		    cmdSave.Parameters.Add("@SizedImageIsThumbnail", isThumbnail);
		    cmdSave.Parameters.Add("@sizedImageData", SqlDbType.Image).Value = image;
		  
		    conPortal.Open();
		    cmdSave.ExecuteNonQuery();
		    conPortal.Close(); 
			
			return image;
		}




        //*********************************************************************
        //
        // GetCommunityImage Method
        //
        // Returns a byte array containing a community image. 
        //
        //*********************************************************************

    	public static SqlDataReader GetCommunityImage(string fileName) {
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdGet = new SqlCommand("Community_ImagesGetCommunityImage", conPortal);
			cmdGet.CommandType = CommandType.StoredProcedure;
			cmdGet.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
			cmdGet.Parameters.Add("@fileName", fileName);

			conPortal.Open();
			return cmdGet.ExecuteReader(CommandBehavior.CloseConnection);
		}



        //*********************************************************************
        //
        // GetAllCommunityImages Method
        //
        // Returns a list of all the images in this community. 
        //
        //*********************************************************************

    	public static DataSet GetAllCommunityImages() {
			SqlDataAdapter dadGet = new SqlDataAdapter("Community_ImagesGetCommunityImages", CommunityGlobals.ConnectionString);
			dadGet.SelectCommand.CommandType = CommandType.StoredProcedure;
			dadGet.SelectCommand.Parameters.Add("@communityID", CommunityGlobals.CommunityID);

            DataSet dstImages = new DataSet();
            dadGet.Fill(dstImages);
            return dstImages;      
		}



        //*********************************************************************
        //
        // AddCommunityImage Method
        //
        // Adds a new community image to the database. 
        //
        //*********************************************************************

        public static string AddCommunityImage(CommunityImageType imageType, HttpPostedFile proposedFile) {
            // Make sure that the posted file has content
            if (proposedFile == null || proposedFile.ContentLength == 0)
                return null; 


    		string imgFileName = proposedFile.FileName.Remove(0,proposedFile.FileName.LastIndexOf("\\")+1);
			string imgContentType = proposedFile.ContentType;
            
            // Make sure that the posted file is actually an image
            if (!IsImageFile(imgFileName))
                return null;
            
            
            // Process the image into byte array
			Stream imgStream = proposedFile.InputStream;
			int imgLen = proposedFile.ContentLength;
			byte[] imgBinaryData = new byte[imgLen];
			int n = imgStream.Read(imgBinaryData,0,imgLen);

            // Add the image to the database
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdAdd = new SqlCommand("Community_ImagesAddCommunityImage", conPortal);
			cmdAdd.CommandType = CommandType.StoredProcedure;

            cmdAdd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmdAdd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
			cmdAdd.Parameters.Add("@fileName", imgFileName);
			cmdAdd.Parameters.Add("@contentType", imgContentType);
			cmdAdd.Parameters.Add("@imageType", imageType);
            cmdAdd.Parameters.Add("@imageData", SqlDbType.Image).Value = imgBinaryData;

			conPortal.Open();
            int result = cmdAdd.ExecuteNonQuery();
            conPortal.Close();
            
            return imgFileName;                    
        }



        //*********************************************************************
        //
        // DeleteCommunityImage Method
        //
        // Removes a community image from the database. 
        //
        //*********************************************************************

        public static void DeleteCommunityImage(string imageName) {
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdDelete = new SqlCommand("Community_ImagesDeleteCommunityImage", conPortal);
			cmdDelete.CommandType = CommandType.StoredProcedure;

            cmdDelete.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
			cmdDelete.Parameters.Add("@fileName", imageName);
        
            conPortal.Open();
            cmdDelete.ExecuteNonQuery();
            conPortal.Close();       
        }



        //*********************************************************************
        //
        // AddSectionImage Method
        //
        // Adds a section image to the database. 
        //
        //*********************************************************************

        public static int AddSectionImage(int contentPageID, HttpPostedFile proposedFile) {
            // Make sure that the posted file has content
            if (proposedFile == null || proposedFile.ContentLength == 0)
                return -1; 

    		string imgFileName = proposedFile.FileName.Remove(0,proposedFile.FileName.LastIndexOf("\\")+1);
			string imgContentType = proposedFile.ContentType;


            // Make sure that the posted file is actually an image
            if (!IsImageFile(imgFileName))
                return -1;
            
            // Process the image into byte array
			Stream imgStream = proposedFile.InputStream;
			int imgLen = proposedFile.ContentLength;
			byte[] imgBinaryData = new byte[imgLen];
			int n = imgStream.Read(imgBinaryData,0,imgLen);

            // Add the image to the database
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdAdd = new SqlCommand("Community_ImagesAddSectionImage", conPortal);
			cmdAdd.CommandType = CommandType.StoredProcedure;

            cmdAdd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmdAdd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
            cmdAdd.Parameters.Add("@contentPageID", contentPageID);
			cmdAdd.Parameters.Add("@imageName", imgFileName);
			cmdAdd.Parameters.Add("@contentType", imgContentType);
            cmdAdd.Parameters.Add("@imageData", SqlDbType.Image).Value = imgBinaryData;

			conPortal.Open();
            cmdAdd.ExecuteNonQuery();
            int result = (int)cmdAdd.Parameters["@RETURN_VALUE"].Value;
            conPortal.Close();
            
            return result;                    
        }


        //*********************************************************************
        //
        // UpdateSectionImage Method
        //
        // Updates a section image in the database. 
        //
        //*********************************************************************

        public static void UpdateSectionImage(int imageID, HttpPostedFile proposedFile) {
            // Make sure that the posted file has content
            if (proposedFile == null || proposedFile.ContentLength == 0)
                return; 

    		string imgFileName = proposedFile.FileName.Remove(0,proposedFile.FileName.LastIndexOf("\\")+1);
			string imgContentType = proposedFile.ContentType;


            // Make sure that the posted file is actually an image
            if (!IsImageFile(imgFileName))
                return;

            // Process the image into byte array
			Stream imgStream = proposedFile.InputStream;
			int imgLen = proposedFile.ContentLength;
			byte[] imgBinaryData = new byte[imgLen];
			int n = imgStream.Read(imgBinaryData,0,imgLen);

            // Update the image in the database
			SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
			SqlCommand cmdAdd = new SqlCommand("Community_ImagesUpdateSectionImage", conPortal);
			cmdAdd.CommandType = CommandType.StoredProcedure;

⌨️ 快捷键说明

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