当前位置:编程学习 > C#/ASP.NET >>

asp.net如何实现图片压缩!如何实现文件传输!

大家好:
    请问asp.net中如何实现图片压缩!如何实现文件传输!
谢谢大家,请大家多多帮忙! --------------------编程问答-------------------- 我写的上传控件看看对你有没有帮助

/*
 * 控件参数
 * 
 * UploadPath       为图片保存的相对路径(使用该控件的页面相对于根目录的位置)
 * IsSuccess        返回图片是否保存成功,在使用页面中可以捕获该变量来决定是否进行下一步操作
 * FileName         存储上传的图片名
 * ViewPicWidth     预览图片宽度
 * ViewPicHeight    预览图片高度
 * ImgMaxSize       允许上传图片的大小(以K为单位,默认15K)
 * upload()         在使用页面中调用该方法即可保存图片
 * MakeSmallPicture(...) 生成缩略图
 * 
 * ---------------------------------------------------------------------------------
 * 功能说明:
 * 
 * 1.图片保存格式为:2007\1-19\49be57a0-2b2b-4e43-bd55-bf61f61cb559.JPG
 * 2.上传图片时有预览的功能
 * 3.修改时可以判断用户是否对图片做了修改,以防重覆上传
 * 
 * ---------------------------------------------------------------------------------
 * 使用例子:
 * 
 *  [test.aspx.cs]
 * 
 *  //保存图片
 *  protected void Button1_Click(object sender, EventArgs e)
    {
        //缩略图名(路径+文件名)
        string strSmallPictureName = "D:/test.jpg";
        //配制上传控件的保存图片路径(该页面相对于根目录的路径为 ../ 图片路径为 images/FocusPic )
        this.ucUpload.UploadPath = "../" + "images/FocusPic";
        //保存图片
        this.ucUpload.upload();
        //判断是否保存成功
        if (this.ucUpload.IsSuccess == "false")
        {
            ShowClientMessegeBox("图片上传失败");
        }
        //写进数据库
        //-----------------------------------------
        //保存 this.ucUpload.FileName 到数据库相应字段即可
        //-----------------------------------------

        //生成缩略图(高度以给定值200为准,宽度按比例)
        this.ucUpload.MakeSmallPicture(Server.MapPath("../" + PicConfigPath + this.ucUpload.FileName), strSmallPictureName, 150, 200);
        //生成缩略图(宽度以给定值150为准,高度按比例)
        this.ucUpload.MakeSmallPicture(Server.MapPath("../" + PicConfigPath + this.ucUpload.FileName), strSmallPictureName, 150, 200,"W");
        //生成缩略图(以指定宽高缩略)
        this.ucUpload.MakeSmallPicture(Server.MapPath("../" + PicConfigPath + this.ucUpload.FileName), strSmallPictureName, 150, 200, "HW");
        //生成缩略图并添加文字水印
        this.ucUpload.MakeSmallPicture(Server.MapPath("../" + PicConfigPath + this.ucUpload.FileName), strSmallPictureName, 150, 200, "H", "价值中国网", "");
        //生成缩略图并添加图片水印
        this.ucUpload.MakeSmallPicture(Server.MapPath("../" + PicConfigPath + this.ucUpload.FileName), strSmallPictureName, 150, 200, "H", "", "D:/BtSearch.gif");
    }
 * 
 *  //修改图片
 *  this.ucUpload.FileName = gdoFocusPic.PicUrl;  //将数据库中图片的路径传给用户控件
 *  
 * 
 * [test.aspx]
 * 
 *  <%@ Register Src="../UserControls/upload.ascx" TagName="upload" TagPrefix="uc1" %>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <uc1:upload id="ucUpload" runat="server" ImgMaxSize="10" ViewPicWidth="150" ViewPicHeight="200"></uc1:upload>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            </div>
        </form>
    </body>
    </html>
 * 
 */ --------------------编程问答-------------------- 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.Configuration;
using System.Drawing;

public partial class UserControls_CVUpload : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgView.Width = ViewPicWidth;
        imgView.Height = ViewPicHeight;
    }

    protected void lbtnDelPic_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(UploadPath + FileName))
        {
            string fileName = UploadPath + FileName;
            string path = Server.MapPath(fileName);
            File.Delete(path);
        }
    }

    #region 处理文件上传

    public void upload()
    {
        if (this.upImage.Value != "" && this.upImage.Value != null)
        {
            string strPicturePath = this.GetMapPath();
            Guid g = Guid.NewGuid();
            DateTime time = DateTime.Now;
            string pictureName = time.Year + "/" + time.Month + "-" + time.Day + "/";
            string strFullPath = this.upImage.Value;
            string strFileName = strFullPath.Substring(strFullPath.LastIndexOf("\\") + 1);
            string strFileType = strFileName.Substring(strFileName.LastIndexOf(".") + 1);
            string strFileType_toLower = strFileType.ToLower();
            string filename = g.ToString() + "." + strFileType;
            FileName = pictureName + filename;
            if (strFileType_toLower == "jpg" || strFileType_toLower == "gif" || strFileType_toLower == "bmp")
            {
                this.upImage.PostedFile.SaveAs(Server.MapPath(strPicturePath) + g + "." + strFileType);
            }
        }
        else
        {
            IsSuccess = "false";
        }
    }
    #endregion

    #region 创建图片文件夹并生成图片路径

    public string GetMapPath()
    {
        string picTurePath = UploadPath;

        DateTime time = DateTime.Now;
        string year = time.Year.ToString();
        string day = time.Month + "-" + time.Day;
        string basePath = Server.MapPath(picTurePath).ToString();
        string CreatePath = Server.MapPath(picTurePath + "\\" + year).ToString();
        if (!Directory.Exists(CreatePath))
        {
            Directory.CreateDirectory(CreatePath);
            DirectoryInfo directory = new DirectoryInfo(basePath);
            string path = year + "\\" + day;
            directory.CreateSubdirectory(path);
        }
        else
        {
            string nextPath = Server.MapPath(picTurePath + "\\" + year + "\\" + day).ToString();
            if (!Directory.Exists(nextPath))
            {
                DirectoryInfo directory = new DirectoryInfo(basePath);
                string path = year + "\\" + day;
                directory.CreateSubdirectory(path);
            }
        }
        return picTurePath + year + "/" + day + "/";
    }
    #endregion --------------------编程问答-------------------- #region 控件参数

    //用于保存上传的文件
    public string FileName
    {
        set 
        { 
            Session["FileName"] = value;
            string strUrl = HttpContext.Current.Request.Url.PathAndQuery;
            strUrl = strUrl.Substring(0, strUrl.LastIndexOf("/")+1);
            if (strUrl.Length == 7)
            {
                strUrl = HttpContext.Current.Request.Url.PathAndQuery;
            }

            this.imgView.Src = strUrl + UploadPath + value;
        }
        get 
        { 
            return (string)Session["FileName"]; 
        }
    }

    //图片保存相对路径
    private string m_UplaodPath;
    public string UploadPath
    {
        set { m_UplaodPath = value; }
        get { return m_UplaodPath; }
    }

    //用于判断图片是否上传成功
    private string m_strIsSuccess;
    public string IsSuccess
    {
        get { return m_strIsSuccess; }
        set { m_strIsSuccess = value; }
    }

    //预览图片大小设置
    private int m_intViewPicHeight = 150;
    public int ViewPicHeight
    {
        get 
        {
                return m_intViewPicHeight;
        }
        set { m_intViewPicHeight = value; }
    }
    private int m_intViewPicWidth=120;
    public int ViewPicWidth
    {
        get 
        {
                return m_intViewPicWidth;
        }
        set { m_intViewPicWidth = value; }
    }

    //上传图片最大不能超过
    private int m_intImgMaxSize = 15;
    public int ImgMaxSize
    {
        get { return m_intImgMaxSize; }
        set { m_intImgMaxSize = value; }
    }
    #endregion

    #region  生成缩略图,添加水印

    /// <summary>
    /// 生成缩略图,添加文字/图片水印
    /// </summary>
    /// <param name="txtOriginalPicturePath">原图片路径</param>
    /// <param name="txtSmallPicturePath">缩略图路径</param>
    /// <param name="intWidth">缩略图宽度</param>
    /// <param name="intHeight">缩略图高度</param>
    /// <param name="strMode">缩略方式('H'--指定高,宽按比例,'W'--指定宽,高按比例,'HW'--按指定的宽高缩略)</param>
    /// <param name="strSignetWordContent">添加文字水印(文字内容)</param>
    /// <param name="strSignetPicturePath">添加图片水印(图片路径)</param>
    public void MakeSmallPicture(string txtOriginalPicturePath, string txtSmallPicturePath, int intWidth, int intHeight, string strMode, string strSignetWordContent, string strSignetPicturePath)
    {
        System.Drawing.Image imgOriginalImage = System.Drawing.Image.FromFile(txtOriginalPicturePath);
        int intToWidth = intWidth;
        int intToHeight = intHeight;

        int x = 0;
        int y = 0;
        int intOriginalWidth = imgOriginalImage.Width;
        int intOriginalHeight = imgOriginalImage.Height;

        switch (strMode)
        {
            case "HW"://指定高宽缩放(可能变形)                
                break;
            case "W"://指定宽,高按比例                    
                intToHeight = imgOriginalImage.Height * intWidth / imgOriginalImage.Width;
                break;
            case "H"://指定高,宽按比例
                intToWidth = imgOriginalImage.Width * intHeight / imgOriginalImage.Height;
                break;
            case "CUT"://指定高宽裁减(不变形)                
                if ((double)imgOriginalImage.Width / (double)imgOriginalImage.Height > (double)intToWidth / (double)intToHeight)
                {
                    intOriginalHeight = imgOriginalImage.Height;
                    intOriginalWidth = imgOriginalImage.Height * intToWidth / intToHeight;
                    y = 0;
                    x = (imgOriginalImage.Width - intOriginalWidth) / 2;
                }
                else
                {
                    intOriginalWidth = imgOriginalImage.Width;
                    intOriginalHeight = imgOriginalImage.Width * intHeight / intToWidth;
                    x = 0;
                    y = (imgOriginalImage.Height - intOriginalHeight) / 2;
                }
                break;
            default:
                break;
        }

        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(intToWidth, intToHeight);

        //新建一个画板
        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(imgOriginalImage, new Rectangle(0, 0, intToWidth, intToHeight), new Rectangle(x, y, intOriginalWidth, intOriginalHeight), GraphicsUnit.Pixel);

        //文字水印
        if (!string.IsNullOrEmpty(strSignetWordContent))
        {
            System.Drawing.Font f = new Font("宋体", 14);
            System.Drawing.Brush b = new SolidBrush(Color.White);
            g.DrawString(strSignetWordContent, f, b, (Convert.ToInt32(intToWidth / 2) - 20), Convert.ToInt32(intHeight / 2));
        }

        //图片水印 
        if (!string.IsNullOrEmpty(strSignetPicturePath))
        {
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(strSignetPicturePath);
            g.DrawImage(copyImage, new Rectangle(Convert.ToInt32((intToWidth - copyImage.Width) / 2), Convert.ToInt32((intToHeight - copyImage.Height) / 2), copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            copyImage.Dispose();
        }


        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(txtSmallPicturePath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            imgOriginalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    /// <summary>
    /// 生成缩略图,添加文字/图片水印
    /// </summary>
    /// <param name="txtOriginalPicturePath">原图片路径</param>
    /// <param name="txtSmallPicturePath">缩略图路径</param>
    /// <param name="intWidth">缩略图宽度</param>
    /// <param name="intHeight">缩略图高度</param>
    /// <param name="strMode">缩略方式('H'--指定高,宽按比例,'W'--指定宽,高按比例,'HW'--按指定的宽高缩略)</param>
    public void MakeSmallPicture(string txtOriginalPicturePath, string txtSmallPicturePath, int intWidth, int intHeight, string strMode)
    {
        MakeSmallPicture(txtOriginalPicturePath, txtSmallPicturePath, intWidth, intHeight, strMode, "", "");
    }

    /// <summary>
    /// 生成缩略图,添加文字/图片水印
    /// </summary>
    /// <param name="txtOriginalPicturePath">原图片路径</param>
    /// <param name="txtSmallPicturePath">缩略图路径</param>
    /// <param name="intWidth">缩略图宽度</param>
    /// <param name="intHeight">缩略图高度</param>
    public void MakeSmallPicture(string txtOriginalPicturePath, string txtSmallPicturePath, int intWidth, int intHeight)
    {
        MakeSmallPicture(txtOriginalPicturePath, txtSmallPicturePath, intWidth, intHeight, "HW", "", "");
    }

    #endregion
}
--------------------编程问答-------------------- 谢谢各位大哥帮助! --------------------编程问答-------------------- 我的问题是:图片压缩的方式上船(图片压缩后,上传,然后在解压) --------------------编程问答-------------------- MakeSmallPicture为什么是调用自己?

public   void   MakeSmallPicture(string   txtOriginalPicturePath,   string   txtSmallPicturePath,   int   intWidth,   int   intHeight) 
        { 
                MakeSmallPicture(txtOriginalPicturePath,   txtSmallPicturePath,   intWidth,   intHeight,   "HW ",   " ",   " "); 
        }

这么写能运行吗?
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,