图片逻辑处理层,实现抓图,上传图

本文介绍了一个用于处理图片上传的类UpLoadImageData,该类能够从远程URL读取图片并保存到本地指定路径,并进行一系列的图片处理操作如压缩、添加水印等,最后将图片信息保存到数据库。

 using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Drawing.Imaging;
using ImageTool;
using System.Drawing.Drawing2D;
using System.Drawing;

/// <summary>
/// UpLoadImage 的摘要说明
/// </summary>
public class UpLoadImageData
{
  public string down_path;
  public string timestr;
  public string SqlType;
  public string ProID;
  
  #region //读取远程图片到本地
  public void ReadNetImage(System.Web.UI.Page page,string URLStr,string down_path,string SqlType,string

id,string NOTSml)
  {
   WebRequest wreq=WebRequest.Create(URLStr); 
   HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse(); 
   Stream s=wresp.GetResponseStream();    
   System.Drawing.Image img;
   img = System.Drawing.Image.FromStream(s); 
            
            CreatePath(down_path);

            string timestr = GetTimeStr();          //
   string[] ImgName=URLStr.Split('.');     //
            string extension = ImgName[ImgName.Length - 1];

            string filenamestr = timestr + "." + extension;

   img.Save(down_path+filenamestr);  //保存   
   img.Dispose();

   if (NOTSml!="NOTSml")
   {
                PublicMethod(page, down_path, filenamestr);
   }

   //保存到数据库
            SaveToDataBase(SqlType, id, filenamestr);
  }

    private static void PublicMethod(System.Web.UI.Page page, string down_path, string filenamestr)
    {
        //图片水印
        WaterMark.SetWaterMark(page, down_path, filenamestr, "big_");
        //压缩图片小图
        CompressImage.SetCompressImage(120, "sml_", down_path, "big_" + filenamestr);
        //压缩中图高清晰
        CompressImage.SetGoodImage(down_path + "/" + filenamestr, down_path + "/mid1_" + filenamestr, 580, 580);
        WaterMark.SetWaterMark(page, down_path, "mid1_" + filenamestr, "mid_");
        if (File.Exists(down_path + filenamestr))
        {
            File.Delete(down_path + filenamestr);
            File.Delete(down_path + "/mid1_" + filenamestr);
        }

    }
  #endregion

  #region //保存附加文件 
        public System.Boolean ReadNativeImage(System.Web.UI.Page page, FileUpload File1, string down_path, string SqlType,

string id, string NOTSml)
  {
            CreatePath(down_path);
            string fileName = GetTimeStr();
   string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();         
            string filenamestr = fileName + extension;

            File1.PostedFile.SaveAs(down_path + "/" + filenamestr); 
            //如果图片过大 大于800*600 压缩到800*600
   if (NOTSml!="NOTSml")
   {
                PublicMethod(page, down_path, filenamestr);
   }
   //保存到数据库
            SaveToDataBase(SqlType,id,filenamestr);
   return true;
  }
  #endregion  

  #region //保存到数据库

    public void SaveToDataBase(string SqlType, string id, string filenamestr)
  {
      heima.BuindDateClass doc= new heima.BuindDateClass();
   switch(SqlType)
   {
    case "wwww":
                        SQLExecute("");     
     break;
    case "wwww":
                        SQLExecute("");     
     break;


   }
  }
  #endregion

        private static void CreatePath(string down_path)
        {
            if (!System.IO.Directory.Exists(down_path))
            {
                System.IO.Directory.CreateDirectory(down_path);
            }
        }

        private string GetTimeStr()
        {
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string timestr = currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() +

currentTime.Hour.ToString() + currentTime.Minute.ToString() + currentTime.Second.ToString() +

currentTime.Millisecond.ToString();
            return timestr;
        } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值