• 关于asp.net上传图片自动生成缩略图


     /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="fileName">原文件</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        private void CreateThumbnailImage(string fileName, string smallPath, int width, int height)
        {
            //版权信息
            string strAdd = "www.wander.com";
            //生成的缩略图的名字=年月日+文件大小(防止文件名相同)
            string newFileName = smallPath;

            System.Drawing.Image image, newImage;
            //载入原图像
            image = System.Drawing.Image.FromFile(Server.MapPath(fileName));
            //回调
            System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(callBack);
            //生成缩略图
            newImage = image.GetThumbnailImage(width, height, callb, new System.IntPtr());

            AddInfo(newImage, strAdd, newFileName, 16);

            image.Dispose();
            newImage.Dispose();

            Image1.ImageUrl = newFileName;
        }

        /// <summary>
        /// 添加版权信息
        /// </summary>
        /// <param name="image"></param>
        /// <param name="strAdd"></param>
        /// <param name="newFileName"></param>
        /// <param name="fontSize"></param>
        private void AddInfo(System.Drawing.Image image, string strAdd, string newFileName, int fontSize)
        {
            Response.Clear();
            Bitmap b = new Bitmap(image);
            Graphics g = Graphics.FromImage(b);
            g.DrawString(strAdd, new Font("宋体", fontSize), new SolidBrush(Color.Red), image.Width / 2 - 80, image.Height - 30);
            b.Save(Server.MapPath(newFileName), System.Drawing.Imaging.ImageFormat.Gif);
        }

        private bool callBack()
        {
            return true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (loadFile.PostedFile != null)
            {
                //判断是不是图像文件
                if (loadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
                {
                    //文件类型错误
                }
                else
                {
                    string ext = loadFile.PostedFile.FileName.Substring(loadFile.PostedFile.FileName.LastIndexOf("."));
                    //生成新的原文件名 年月日+文件大小+扩展名
                    string path = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + ext;
                    //加上版权信息的文件
                    string newPath = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "new" + ext;
                    //缩略图文件名
                    string newFileName = "uploads\\" + System.DateTime.Now.Date.ToShortDateString() + loadFile.PostedFile.ContentLength.ToString() + "small.gif";
                    //上传原图
                    loadFile.PostedFile.SaveAs(Server.MapPath(path));
                    //生成缩略图
                    CreateThumbnailImage(path, newFileName, 200, 300);

                    //给原图加上版权信息
                    System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(path));
                    AddInfo(image, "www.wander.com", newPath, 20);
                    image.Dispose();
                }
            }

        }

  • 相关阅读:
    [WF4.0 实战] AutoResetEvent具体解释(线程独占訪问资源)
    linux下getrlimit与sysconf函数
    36.怎样使用定时任务
    1016. 部分A+B (15)
    找你妹+ipad+wifi,回顾那年的经典游戏
    Oracle 外键约束子表、父表
    字符串 上
    LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解
    jquery ajax參数加点号状态200进error
    泛型数组随机排列工具类
  • 原文地址:https://www.cnblogs.com/gengaixue/p/1311963.html
Copyright © 2020-2023  润新知