ASP.NET 多张图片上传,并可以预览。

一、首先先上效果图:

1、上传前

ASP.NET 多张图片上传,并可以预览。_第1张图片

2、上传后

ASP.NET 多张图片上传,并可以预览。_第2张图片

二、主要使用控件:FileUpload,DataList展示多张图片

三、代码

1、前台aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductZiZhiUpload.aspx.cs" Inherits="ProductZiZhiUpload" %>




   
        
        
        
        
   
   
    


   


   

       

                              width="99%">
                   
                   
                       
                       
                      
                   
                      
                    
                   
                 
              
                    
                    
               

                            药品生产批件有效期:

                            
                       
                       
                       

                       点击图片查看大图
                   


       

        

            

               
                
                

//点击FileUpload1上传浏览按钮,通过javascript:__doPostBack('lbUploadPhoto',''),直接执行lbUploadPhoto按钮的方法
                     
                       
                     点击选择药品生产批件


                


                

                                        BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList1_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
               
               
               
               
            
               
   


                       
                           
                           
                           
                           
                       
                   
                    
                       
                     

                            <%#Eval(" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>

//onclick方法里是点击图片看大图的js
                           


                               
CommandName="De" runat="server">删除
  
                   
               
               
           


                    
                


                 


                
            

                


                    


                       
                       
                    
                         点击选择药品包装及说明书

                

                                      BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList2_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
               
               
               
               
            
               
   


                       
                           
                           
                           
                           
                       
                   
                    
                       
                     

                            <%#Eval(" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>
                           

                               
CommandName="De" runat="server">删除
  
                   
               
               
           


                


            
            

           


                    


                         
                       
                    
                         药品质量标准

                

                                        BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList3_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
               
               
               
               
            
               
   
                       
                           
                           
                           
                           
                       
                   
                    
                       
                     

                            <%#Eval(" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>
                           

                               
CommandName="De" runat="server">删除
  
                   
               

               
           

                


                
            


           


                    


                         
                       
                    
                         药品检验报告书

                

                                      BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList4_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
               
               
               
               
            
               
   


                       
                           
                           
                           
                           
                       
                   
                    
                       
                     

                            <%#Eval(" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>
                           

                               
CommandName="De" runat="server">删除
  
                   
               
               
           


                


                
            

        

        

           

           


        


    
   

//大图的idv
                   


       

   

       
   

   
x

//大图的div

   



2、后台cs代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProductZiZhiUpload : System.Web.UI.Page
{
   

    ExecuteSql executeSql = new ExecuteSql();
    public static System.IO.Stream fileDataStream = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        { 
        string ccomid= Request.QueryString["comid"];
            HiddenField1.Value = ccomid;

            HiddenField2.Value = executeSql.ExecuteSelectFirst("SELECT  COMNAME FROM dbo.V_Product where comid='"+ ccomid + "'");

            string sql1 = "select CONVERT(VARCHAR(100),endtime,23) AS endtime from prd_product_zizhi where comid='" + ccomid + "'";
            DataTable dt1 = executeSql.ExecuteSelectQuery(sql1);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                tendtime.Text = dt1.Rows[0]["endtime"].ToString();

            }

            band1();
            band2();
            band3();
            band4();
        }

    }

    protected void lbUploadPhoto_Click(object sender, EventArgs e)
    {
        fileUpload();
    }
    protected void lbUploadPhoto2_Click(object sender, EventArgs e)
    {
        fileUpload2();
    }

    protected void lbUploadPhoto3_Click(object sender, EventArgs e)
    {
        fileUpload3();
    }

    protected void lbUploadPhoto4_Click(object sender, EventArgs e)
    {
        fileUpload4();
    }

    //从控件上传文件1 
    public void fileUpload()
    {

        fileDataStream = FileUpload1.PostedFile.InputStream;

        System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//从上传文件流中实例化Image类 
        float w = image.HorizontalResolution;//获取上传文件的水平分辨率(以“像素/英寸”为单位)。
        float w1 = image.Width;
        float h1 = image.Height;
         
        //float h = image.VerticalResolution;//获取上传文件的垂直分辨率(以“像素/英寸”为单位)。 
        if (w>71.9)//这里限制了分辨率必须大于72的,这里可以不限制
        {
            if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0 && FileUpload1.PostedFile.ContentLength < 1024 * 1024 * 5)//最大不超过5M
            {


                string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
                if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
                {
                    return;
                }
                string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
                string path = "~/upload/tupian1/" + filename;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(path));
                
                string imageurl = "upload/tupian1/" + filename;
                string newimageurl = imageurl;
                newimageurl = newimageurl.Replace("Image_", "yasuoImage_");
                string insql = "insert into prd_file(typeid,comid,imagename,imageurl,newimageurl,isok,imagetitle) "
                    + " values(1,'" + HiddenField1.Value + "','" + filename + "','" + imageurl + "','"+ newimageurl+ "',0,'"+HiddenField2.Value+ "_药品生产批件')";
                executeSql.UpdateHrd_qxCount(insql);

                string spath = Server.MapPath(path);
                yasuo(spath, w1, h1);//这里上传后同时生成一张 96像素的标准大小图片,如果没有这个业务可以删除这段代码

                band1();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "");
                // Response.Write("");
            }
        }
        else
        {

            ClientScript.RegisterStartupScript(this.GetType(), "", "");

        }
    }
    //从控件上传文件2
    public void fileUpload2()
    {
        fileDataStream = FileUpload2.PostedFile.InputStream;

        System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//从上传文件流中实例化Image类 
        float w = image.HorizontalResolution;//获取上传文件的水平分辨率(以“像素/英寸”为单位)。
        float w1 = image.Width;
        float h1 = image.Height;
        //float h = image.VerticalResolution;//获取上传文件的垂直分辨率(以“像素/英寸”为单位)。 
        if (w > 71.9)
        {
            if (FileUpload2.PostedFile != null && FileUpload2.PostedFile.ContentLength > 0 && FileUpload2.PostedFile.ContentLength < 1024 * 1024 * 5)
        {
            string ext = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName).ToLower();
            if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
            {
                return;
            }
            string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
            string path = "~/upload/tupian2/" + filename;
            FileUpload2.PostedFile.SaveAs(Server.MapPath(path));
            string imageurl = "upload/tupian2/" + filename;
                string newimageurl = imageurl;
                newimageurl = newimageurl.Replace("Image_", "yasuoImage_");
                string insql = "insert into prd_file(typeid,comid,imagename,imageurl,newimageurl,isok,imagetitle) "
                + " values(2,'" + HiddenField1.Value + "','" + filename + "','" + imageurl + "','" + newimageurl + "',0,'"+HiddenField2.Value+ "_包装及说明书')";
            executeSql.UpdateHrd_qxCount(insql);
                string spath = Server.MapPath(path);
                yasuo(spath, w1, h1);
                band2();
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "");
           // Response.Write("");
        }
        }
        else
        {

            ClientScript.RegisterStartupScript(this.GetType(), "", "");

        }
    }
    //从控件上传文件3
    public void fileUpload3()
    {
        fileDataStream = FileUpload3.PostedFile.InputStream;

        System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//从上传文件流中实例化Image类 
        float w = image.HorizontalResolution;//获取上传文件的水平分辨率(以“像素/英寸”为单位)。 
        float w1 = image.Width;
        float h1 = image.Height;
        //float h = image.VerticalResolution;//获取上传文件的垂直分辨率(以“像素/英寸”为单位)。 
        if (w > 71.9)
        {
         if (FileUpload3.PostedFile != null && FileUpload3.PostedFile.ContentLength > 0 && FileUpload3.PostedFile.ContentLength < 1024 * 1024 * 5)
        {
            string ext = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName).ToLower();
            if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
            {
                return;
            }
            string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
            string path = "~/upload/tupian3/" + filename;
            FileUpload3.PostedFile.SaveAs(Server.MapPath(path));
            string imageurl = "upload/tupian3/" + filename;
                string newimageurl = imageurl;
                newimageurl = newimageurl.Replace("Image_", "yasuoImage_");
                string insql = "insert into prd_file(typeid,comid,imagename,imageurl,newimageurl,isok,imagetitle) "
                + " values(3,'" + HiddenField1.Value + "','" + filename + "','" + imageurl + "','" + newimageurl + "',0,'"+HiddenField2.Value+ "_质量标准')";
                executeSql.UpdateHrd_qxCount(insql);
                string spath = Server.MapPath(path);
                yasuo(spath, w1, h1);
                band3();
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "");
            //Response.Write("");
        }
        }
        else
        {

            ClientScript.RegisterStartupScript(this.GetType(), "", "");

        }

    }
    //从控件上传文件4 
    public void fileUpload4()
    {
        fileDataStream = FileUpload4.PostedFile.InputStream;

        System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//从上传文件流中实例化Image类 
        float w = image.HorizontalResolution;//获取上传文件的水平分辨率(以“像素/英寸”为单位)。 
        float w1 = image.Width;
        float h1 = image.Height;
        //float h = image.VerticalResolution;//获取上传文件的垂直分辨率(以“像素/英寸”为单位)。 
        if (w > 71.9 )
        {
            if (FileUpload4.PostedFile != null && FileUpload4.PostedFile.ContentLength > 0 && FileUpload4.PostedFile.ContentLength < 1024 * 1024 * 5)
        {
            string ext = System.IO.Path.GetExtension(FileUpload4.PostedFile.FileName).ToLower();
            if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
            {
                return;
            }
            string filename = "Image_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
            string path = "~/upload/tupian4/" + filename;
            FileUpload4.PostedFile.SaveAs(Server.MapPath(path));
            string imageurl = "upload/tupian4/" + filename;
                string newimageurl = imageurl;
                newimageurl = newimageurl.Replace("Image_", "yasuoImage_");
                string insql = "insert into prd_file(typeid,comid,imagename,imageurl,newimageurl,isok,imagetitle) "
                + " values(4,'" + HiddenField1.Value + "','" + filename + "','" + imageurl + "','" + newimageurl + "',0,'"+HiddenField2.Value+ "_检验报告')";

                executeSql.UpdateHrd_qxCount(insql);
                string spath = Server.MapPath(path);
                yasuo(spath, w1, h1);
                band4();
        }
        else
        {
            // Response.Write("");
            ClientScript.RegisterStartupScript(this.GetType(), "", "");
        }
        }
        else
        {

            ClientScript.RegisterStartupScript(this.GetType(), "", "");

        }
    }

    protected void band1()
    {


        string sql1 = "select id,imagename,imageurl from prd_file where typeid=1 and comid='" + HiddenField1.Value + "'";
        DataTable dt1 = executeSql.ExecuteSelectQuery(sql1);
        this.CFlist.DataSource = dt1;
        this.CFlist.DataBind();


    }
 
    protected void band2()
    {


        string sql1 = "select id,imagename,imageurl from prd_file where typeid=2 and comid='" + HiddenField1.Value + "'";
        DataTable dt1 = executeSql.ExecuteSelectQuery(sql1);
        this.DataList2.DataSource = dt1;
        this.DataList2.DataBind();


    }
    protected void band3()
    {


        string sql1 = "select id,imagename,imageurl from prd_file where typeid=3 and comid='" + HiddenField1.Value + "'";
        DataTable dt1 = executeSql.ExecuteSelectQuery(sql1);
        this.DataList3.DataSource = dt1;
        this.DataList3.DataBind();


    }

    protected void band4()
    {


        string sql1 = "select id,imagename,imageurl from prd_file where typeid=4 and comid='" + HiddenField1.Value + "'";
        DataTable dt1 = executeSql.ExecuteSelectQuery(sql1);
        this.DataList4.DataSource = dt1;
        this.DataList4.DataBind();


    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName.ToString();
        string Id = e.CommandArgument.ToString();
        if (type == "De")
        {
            string sql1 = "delete from prd_file where id="+ Id;

            int num = executeSql.UpdateHrd_qxCount(sql1);
            if (num > 0)
            {

                band1();
            }
            else
            {
               

            }
        }
        else if (type == "Ed")
        {
            
        }
    }

    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName.ToString();
        string Id = e.CommandArgument.ToString();
        if (type == "De")
        {
            string sql1 = "delete from prd_file where id=" + Id;

            int num = executeSql.UpdateHrd_qxCount(sql1);
            if (num > 0)
            {

                band2();
            }
            else
            {


            }
        }
        else if (type == "Ed")
        {

        }
    }
    protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName.ToString();
        string Id = e.CommandArgument.ToString();
        if (type == "De")
        {
            string sql1 = "delete from prd_file where id=" + Id;

            int num = executeSql.UpdateHrd_qxCount(sql1);
            if (num > 0)
            {

                band3();
            }
            else
            {


            }
        }
        else if (type == "Ed")
        {

        }
    }
    protected void DataList4_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName.ToString();
        string Id = e.CommandArgument.ToString();
        if (type == "De")
        {
            string sql1 = "delete from prd_file where id=" + Id;

            int num = executeSql.UpdateHrd_qxCount(sql1);
            if (num > 0)
            {

                band4();
            }
            else
            {


            }
        }
        else if (type == "Ed")
        {

        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql1 = "IF NOT EXISTS(select * from prd_product_zizhi WHERE comid = '"+HiddenField1.Value+"') "
 + " BEGIN  "
  + " INSERT INTO prd_product_zizhi(comid, state, endtime) VALUES('" + HiddenField1.Value + "', 1, '"+ tendtime.Text+ "') "
 + "  END "
 + "  ELSE "
 + "  BEGIN "
 + "  UPDATE prd_product_zizhi SET endtime = '" + tendtime.Text + "', state =1 WHERE comid = '" + HiddenField1.Value + "' "
 + "  END ";

       int i= executeSql.UpdateHrd_qxCount(sql1);

        if(i>0)
        {

            string zhang = "~/upload/zhang/ceshizhang.png";
            string zhangpath = Server.MapPath(zhang);

            string selsql = "select id,newimageurl from prd_file where comid='"+HiddenField1.Value+"' and isok=0";

            DataTable dt1 = executeSql.ExecuteSelectQuery(selsql);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                for(int j=0;j< dt1.Rows.Count; j++)
                {
                    string id = dt1.Rows[j]["id"].ToString();
                    string newimageurl = dt1.Rows[j]["newimageurl"].ToString();
                    string yuan = "~/"+ newimageurl;
                    string yuanpath = Server.MapPath(yuan);
                    ImgWater iw = new ImgWater();
                    iw.gaizhang(yuanpath, zhangpath);

                    string newimageurl1 = newimageurl;
                    string daizhang= newimageurl1.Replace("yasuoImage_", "zhangImage_");
                    executeSql.UpdateHrd_qxCount("update prd_file set newimageurl='"+ daizhang + "',isok=1 where id="+id);

                }


            }


            ClientScript.RegisterStartupScript(this.GetType(), "", "");
            //Response.Write("");
        }else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "");
            //Response.Write("");
        }


    }

//图片统一处理代码,宽度大于1500像素改成1500,小于1500的不处理,处理后图片分辨率是96
    protected void yasuo(string sFile, float w, float h)
    {
        string sFile2 = sFile;
        string dFile = sFile2.Replace("Image_", "yasuoImage_");
        int flag = 100;
        float w2 = w;
        float h2 = h;
        if (w > 1500.0)
        {
            w2 = w * 1500.0f / w;
            h2 = h * 1500.0f / w;

        }

        GetPicThumbnail(sFile, dFile, (int)(w2), (int)(h2), 100);

    }


    /// 压缩图片公用方法    
    /// 原图片    
    /// 压缩后保存位置    
    /// 高度    
    ///    
    /// 压缩质量(数字越小压缩率越高) 1-100    
    ///    

    protected static bool GetPicThumbnail(string sFile, string dFile, int dWidth, int dHeight, int flag)
    {
        System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
        ImageFormat tFormat = iSource.RawFormat;
        int sW = 0, sH = 0;

        //按比例缩放  
        Size tem_size = new Size(iSource.Width, iSource.Height);

        if (tem_size.Width > dHeight || tem_size.Width > dWidth)
        {
            if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
            {
                sW = dWidth;
                sH = (dWidth * tem_size.Height) / tem_size.Width;
            }
            else
            {
                sH = dHeight;
                sW = (tem_size.Width * dHeight) / tem_size.Height;
            }
        }
        else
        {
            sW = tem_size.Width;
            sH = tem_size.Height;
        }

        Bitmap ob = new Bitmap(dWidth, dHeight);
        Graphics g = Graphics.FromImage(ob);

        g.Clear(Color.WhiteSmoke);
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

        g.Dispose();
        //以下代码为保存图片时,设置压缩质量    
        EncoderParameters ep = new EncoderParameters();
        long[] qy = new long[1];
        qy[0] = flag;//设置压缩的比例1-100    
        EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
        ep.Param[0] = eParam;
        try
        {
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICIinfo = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICIinfo = arrayICI[x];
                    break;
                }
            }
            if (jpegICIinfo != null)
            {
                ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径    
            }
            else
            {
                ob.Save(dFile, tFormat);
            }
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            iSource.Dispose();
            ob.Dispose();
        }
    }

}

3、相关公共类,加水印公共类

using System;
using System.Data;
using System.Configuration;
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 System.Drawing;
using System.IO;
using System.Drawing.Imaging;
///


///ImgWater 的摘要说明
///

public class ImgWater
{
    public ImgWater()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public void gaizhang(string yuanpath,string zhangpath)
    {
        string yuanpath1 = yuanpath;
        string daizhangpath = yuanpath1.Replace("yasuoImage_", "zhangImage_");

        zzsImgWater(yuanpath, zhangpath, daizhangpath, 0, 1, 500);

    }

    ///


     /// 图片水印
     ///

     /// 原图文件地址
     /// 水印图片地址
     /// 水印图片保存地址
     /// 水印透明度设置
     /// 水印图片在原图上的显示比例
     /// 水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效
    public bool zzsImgWater(
   string ImgFile
   , string WaterImg
   , string sImgPath
   , float Alpha
   , float iScale
   , int intDistance
   )
    {
        try
        {
            //装载图片
            FileStream fs = new FileStream(ImgFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
            int imgPhotoWidth = imgPhoto.Width;
            int imgPhotoHeight = imgPhoto.Height;
            System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
            int imgWatermarkWidth = imgWatermark.Width;
            int imgWatermarkHeight = imgWatermark.Height;
            //计算水印图片尺寸
            decimal aScale = Convert.ToDecimal(iScale);
            decimal pScale = 0.05M;
            decimal MinScale = aScale - pScale;
            decimal MaxScale = aScale + pScale;
            int imgWatermarkWidthNew = imgWatermarkWidth;
            int imgWatermarkHeightNew = imgWatermarkHeight;
            if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
                if (imgWatermarkWidth > imgWatermarkHeight)
                    if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                    {
                    }
                    else
                    {
                        imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                        imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
                    }
                else
                if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                {
                }
                else
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
                }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            //if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
               //if (imgWatermarkWidth*2 >= imgWatermarkHeight) {
                //    if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                //    {
                //    }
                //    else
                //    {
                //        imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                //        imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                //    }
                //}
                //else
                //{ 
                //if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                //{
                //}
                //else
                //{
                //    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                //    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                //}
                //}
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            //将原图画出来
            Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(96, 96);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gbmPhoto.Clear(Color.White);
            gbmPhoto.DrawImage(
            imgPhoto
            , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
            , 0
            , 0
            , imgPhotoWidth
            , imgPhotoHeight
            , GraphicsUnit.Pixel
            );
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            Graphics gWatermark = Graphics.FromImage(bmWatermark);
            //指定高质量显示水印图片质量
            gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            ImageAttributes imageAttributes = new ImageAttributes();
            //设置两种颜色,达到合成效果
            ColorMap colorMap = new ColorMap();
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };
            imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
            //用矩阵设置水印图片透明度
            float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, Alpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
            System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
            //确定水印边距
            int xPos = imgPhotoWidth - imgWatermarkWidthNew;
            int yPos = imgPhotoHeight - imgWatermarkHeightNew;
            int xPosOfWm = 0;
            int yPosOfWm = 0;
            if (xPos > intDistance)
                xPosOfWm = xPos - intDistance;
            else
                xPosOfWm = xPos;
            if (yPos > intDistance)
                yPosOfWm = yPos - intDistance;
            else
                yPosOfWm = yPos;
            gWatermark.DrawImage(
            imgWatermark
            , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
            , 0
            , 0
            , imgWatermarkWidth
            , imgWatermarkHeight
            , GraphicsUnit.Pixel
            //, imageAttributes
            );
            imgPhoto = bmWatermark;
            //以jpg格式保存图片
            imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //销毁对象
            gbmPhoto.Dispose();
            gWatermark.Dispose();
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            return true;
        }
        catch
        {
            return false;
        }
    }
    /**////
         /// 文字水印
         ///

         /// 原图文件地址
         /// 水印文字
         /// 文字水印图片保存地址
         /// 高度位置
         /// 文字块在图片中所占宽度比例
         /// 文字透明度 其数值的范围在0到255
    public bool zzsTextWater(
   string ImgFile
   , string TextFont
   , string sImgPath
   , float hScale
   , float widthFont
   , int Alpha
   )
    {
        try
        {
            FileStream fs = new FileStream(ImgFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
            int imgPhotoWidth = imgPhoto.Width;
            int imgPhotoHeight = imgPhoto.Height;
            Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gbmPhoto.DrawImage(
            imgPhoto
            , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
            , 0
            , 0
            , imgPhotoWidth
            , imgPhotoHeight
            , GraphicsUnit.Pixel
            );
            //建立字体大小的数组,循环找出适合图片的水印字体
            //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
            int[] sizes = new int[] { 28, 26, 24, 20, 16, 14, 12 };
            System.Drawing.Font crFont = null;
            System.Drawing.SizeF crSize = new SizeF();
            for (int i = 0; i < 7; i++)
            {
                crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
                crSize = gbmPhoto.MeasureString(TextFont, crFont);
                if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont)
                    break;
            }
            //设置水印字体的位置
            //int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
            //float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
            //float xCenterOfImg = (imgPhotoWidth * 1 / 2);
            float yPosFromBottom = imgPhotoHeight * 0.85f;
            float xCenterOfImg = imgPhotoWidth * 0.8f;
            //if (xCenterOfImg             // xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth));
            System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
            StrFormat.Alignment = System.Drawing.StringAlignment.Center;
            //
            System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 0, 0, 0));
            gbmPhoto.DrawString(
            TextFont
            , crFont
            , semiTransBrush2
            , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
            , StrFormat
            );
            System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 255, 255, 255));
            //gbmPhoto.FillRectangle(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - crSize.Width / 2, yPosFromBottom - 4), crSize));
            gbmPhoto.DrawString(
         TextFont
         , crFont
         , semiTransBrush
         , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
         , StrFormat
         );
            bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            gbmPhoto.Dispose();
            imgPhoto.Dispose();
            bmPhoto.Dispose();
            return true;
        }
        catch
        {
            return false;
        }
    }
    /**////
         /// 文字和Logo图片水印
         ///

         /// 原图文件地址
         /// 水印图片地址
         /// 水印文字信息
         /// 生存水印图片后的保存地址
         /// 水印图片的透明度
         /// 水印图片在原图上的显示比例
         /// 水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效
         /// 水印文字高度位置,从图片底部开始计算,0-1
         /// 文字块在图片中所占宽度比例 0-1
         /// 文字透明度 其数值的范围在0到255
    public void zzsImgTextWater(
   string ImgFile
   , string WaterImg
   , string TextFont
   , string sImgPath
   , float ImgAlpha
   , float imgiScale
   , int intimgDistance
   , float texthScale
   , float textwidthFont
   , int textAlpha
   )
    {
        try
        {
            FileStream fs = new FileStream(ImgFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
            int imgPhotoWidth = imgPhoto.Width;
            int imgPhotoHeight = imgPhoto.Height;
            Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gbmPhoto.DrawImage(
            imgPhoto
            , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
            , 0
            , 0
            , imgPhotoWidth
            , imgPhotoHeight
            , GraphicsUnit.Pixel
            );
            //建立字体大小的数组,循环找出适合图片的水印字体
            int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
            System.Drawing.Font crFont = null;
            System.Drawing.SizeF crSize = new SizeF();
            for (int i = 0; i < 43; i++)
            {
                crFont = new Font("arial", sizes[i], FontStyle.Bold);
                crSize = gbmPhoto.MeasureString(TextFont, crFont);
                if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont)
                    break;
            }
            //设置水印字体的位置
            int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
            float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
            float xCenterOfImg = (imgPhotoWidth * 1 / 2);
            System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
            StrFormat.Alignment = System.Drawing.StringAlignment.Center;
            //
            System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 0, 0, 0));
            gbmPhoto.DrawString(
            TextFont
            , crFont
            , semiTransBrush2
            , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
            , StrFormat
            );
            System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 255, 255, 255));
            gbmPhoto.DrawString(
            TextFont
            , crFont
            , semiTransBrush
            , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
            , StrFormat
            );
            System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
            int imgWatermarkWidth = imgWatermark.Width;
            int imgWatermarkHeight = imgWatermark.Height;
            //计算水印图片尺寸
            decimal aScale = Convert.ToDecimal(imgiScale);
            decimal pScale = 0.05M;
            decimal MinScale = aScale - pScale;
            decimal MaxScale = aScale + pScale;
            int imgWatermarkWidthNew = imgWatermarkWidth;
            int imgWatermarkHeightNew = imgWatermarkHeight;
            if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
                if (imgWatermarkWidth > imgWatermarkHeight)
                    if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                    {
                    }
                    else
                    {
                        imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                        imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
                    }
                else
                if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                {
                }
                else
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
                }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
                if (imgWatermarkWidth > imgWatermarkHeight)
                    if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                    {
                    }
                    else
                    {
                        imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                        imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                    }
                else
                if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                {
                }
                else
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
            {
                imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }
            if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
            {
                imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }
            //将原图画出来
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            Graphics gWatermark = Graphics.FromImage(bmWatermark);
            //指定高质量显示水印图片质量
            gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
            //设置两种颜色,达到合成效果
            System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
            imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
            //用矩阵设置水印图片透明度
            float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, ImgAlpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
            System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
            //确定水印边距
            int xPos = imgPhotoWidth - imgWatermarkWidthNew;
            int yPos = imgPhotoHeight - imgWatermarkHeightNew;
            int xPosOfWm = 0;
            int yPosOfWm = 0;
            if (xPos > intimgDistance)
                xPosOfWm = xPos - intimgDistance;
            else
                xPosOfWm = xPos;
            if (yPos > intimgDistance)
                yPosOfWm = yPos - intimgDistance;
            else
                yPosOfWm = yPos;
            gWatermark.DrawImage(
            imgWatermark
            , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
            , 0
            , 0
            , imgWatermarkWidth
            , imgWatermarkHeight
            , GraphicsUnit.Pixel
            , imageAttributes
            );
            imgPhoto = bmWatermark;
            //以jpg格式保存图片
            imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //销毁对象
            gbmPhoto.Dispose();
            gWatermark.Dispose();
            bmPhoto.Dispose();
            imgPhoto.Dispose();
            imgWatermark.Dispose();
        }
        catch
        {
        }
    }
    /**////
         /// 缩略图
         ///

         /// 原图文件地址
         /// 缩略图保存地址
         /// 缩略图宽度
         /// 缩略图高度
         /// 缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等
    public bool zzsResizeImg(string ImgFile, string sImgPath, int ResizeWidth, int ResizeHeight, string BgColor)
    {
        try
        {
            FileStream fs = new FileStream(ImgFile, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
            int imgPhotoWidth = imgPhoto.Width;
            int imgPhotoHeight = imgPhoto.Height;
            int startX = 0;
            int StartY = 0;
            int NewWidth = 0;
            int NewHeight = 0;
            if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
            {
                NewWidth = ResizeWidth;
                NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
                startX = 0;
                StartY = (ResizeHeight - NewHeight) / 2;
            }
            if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
            {
                NewHeight = ResizeHeight;
                NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), 10));
                startX = (ResizeWidth - NewWidth) / 2;
                StartY = 0;
            }
            if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
            {
                NewWidth = ResizeWidth;
                NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
                startX = 0;
                StartY = (ResizeHeight - NewHeight) / 2;
            }
            if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
            {
                NewWidth = imgPhotoWidth;
                NewHeight = imgPhotoHeight;
                startX = (ResizeWidth - imgPhotoWidth) / 2;
                StartY = (ResizeHeight - imgPhotoHeight) / 2;
            }
            //计算缩放图片尺寸
            Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.Clear(Color.FromName(BgColor));
            gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gbmPhoto.DrawImage(
            imgPhoto
            , new Rectangle(startX, StartY, NewWidth, NewHeight)
            , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
            , GraphicsUnit.Pixel
            );
            bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            imgPhoto.Dispose();
            gbmPhoto.Dispose();
            bmPhoto.Dispose();
            ms.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }
    /**////
         /// 图片剪切
         ///

         /// 原图文件地址
         /// 缩略图保存地址
         /// 剪切起始点 X坐标
         /// 剪切起始点 Y坐标
         /// 剪切宽度
         /// 剪切高度
    public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
    {
        FileStream fs = new FileStream(ImgFile, FileMode.Open);
        BinaryReader br = new BinaryReader(fs);
        try
        {
            byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
            //此处只能用filestream,用 System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件
            Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gbmPhoto.DrawImage(
            imgPhoto
            , new Rectangle(0, 0, CutWidth, CutHeight)
            , new Rectangle(PointX, PointY, CutHeight, CutHeight)
            , GraphicsUnit.Pixel
            );
            bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            imgPhoto.Dispose();
            gbmPhoto.Dispose();
            bmPhoto.Dispose();
            ms.Close();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
        }
    }
}

 

 

 

 

你可能感兴趣的:(个人感想,asp.net,C#,图片批量上传,asp.net图片浏览即上传)