using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class admin_uppic : System.Web.UI.Page
{
    public DataSet ds, dsusernameid;
    public string newname, filename, cpath, picture, newext, searchstr, pictureneme, duopicname, fileExtension;
    public string newname2, filename2, cpath2, picture2, newext2;
    public string strsql = "";
    public int id = 0,pid=0;
    public string pic;
    public string productspic = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["lid"] != null && Request.QueryString["pic"] != null)
        {
            id = Convert.ToInt32(Request.QueryString["lid"].ToString());
            pic = Request.QueryString["pic"].ToString();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Savecaption();
        if (pid == 0)
        {
            Response.Write("<script type=text/javascript>alert('修改成功');self.opener.location.reload(); top.close(); </script>");
        }
    }
    private void Savecaption()
    { 
        HttpFileCollection files = HttpContext.Current.Request.Files;
        try
        {

            string extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);  //判断用户上传文件扩展名
            if (extension != ".jpg" && extension != ".gif" && extension != ".png" && extension != ".bmp" && extension != ".JPEG" && extension != ".JPG" && extension != ".BMP" && extension != ".GIF" && extension != ".PNG")//判断图片扩展名
            {
                this.lb_info.Text = "图片格式错误,您只能上传jpg,gif,png,bmp格式的图片";
                pid = 1;
            }
            if (FileUpload1.PostedFile.FileName == "") //判断图片文件名
            {
                this.lb_info.Text = "不能为空！";
                pid = 1;
            }
            else
            {
                string serverpath = Server.MapPath("../newsupfile/") + pic;
                if (File.Exists((System.Web.HttpContext.Current.Request.MapPath("../newsupfile/") + pic)))
                {
                    File.Delete(System.Web.HttpContext.Current.Request.MapPath("../newsupfile/") + pic);
                }
                if (File.Exists((System.Web.HttpContext.Current.Request.MapPath("../newsupfile/s_") + pic)))
                {
                    File.Delete(System.Web.HttpContext.Current.Request.MapPath("../newsupfile/s_") + pic);
                }
                FileUpload1.PostedFile.SaveAs(serverpath);
                tosmallImage(pic);
            }
        }
        catch (Exception ex)
        {
            pid = 1;
            this.lb_info.Text = "图片正在被使用或写保护!请重新载入修改页面";
        }
       }
      private void tosmallImage(string myFile)
      {
          int i = myFile.LastIndexOf(".");
          string newext = myFile.Substring(i);
          string myFile1 = myFile.Replace(newext, "");
          string serverpath = Server.MapPath("../newsupfile/s_") + myFile1 +  newext;
          imagezoom(myFile, 400, 300, serverpath);
      }
    private void imagezoom(string myFile, int width, int height, string filepath)
    {
        System.Drawing.Image myImage = System.Drawing.Image.FromFile(Server.MapPath("../newsupfile/") + myFile);
        decimal MAX_WIDTH = width;
        decimal MAX_HEIGHT = height;
        decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
        int newWidth, newHeight;
        decimal originalWidth = myImage.Width;
        decimal originalHeight = myImage.Height;

        if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
        {
            decimal factor;
            if (originalWidth / originalHeight > ASPECT_RATIO)
            {
                factor = originalWidth / MAX_WIDTH;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            }
            else
            {
                factor = originalHeight / MAX_HEIGHT;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            }
        }
        else
        {
            newWidth = myImage.Width;
            newHeight = myImage.Height;
        }
        ThumbnailImage(Server.MapPath("../newsupfile/") + myFile, filepath, newWidth, newHeight);
    }
    private void ThumbnailImage(string oldImagePath, string newImagePath, int newWidth, int newHeight)
    {
        string originalFilename = oldImagePath;
        string strGoodFile = newImagePath;
        System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
        System.Drawing.Size size = new Size(newWidth, newHeight);
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel);
        image.Dispose();
        bitmap.Save(strGoodFile, GetFormat(strGoodFile));
        image.Dispose();
        g.Dispose();

    }
    private ImageFormat GetFormat(string name)
    {
        string ext = name.Substring(name.LastIndexOf(".") + 1);
        switch (ext.ToLower())
        {
            case "jpg":
            case "jpeg":
                return ImageFormat.Jpeg;
            case "bmp":
                return ImageFormat.Bmp;
            case "png":
                return ImageFormat.Png;
            case "gif":
                return ImageFormat.Gif;
            default:
                return ImageFormat.Jpeg;
        }
    }
}
