上传图片 并生成缩略图 并添加文字水印

来源:互联网 发布:淘宝有监控软件 编辑:程序博客网 时间:2024/04/29 04:55

 string imgdate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Ticks.ToString();
        //上传图片并生成缩略图150*150并添加文字水印
        if (FUImg.PostedFile.FileName != string.Empty)
        {
            string imgtype = FUImg.PostedFile.FileName.Substring(FUUserImg.PostedFile.FileName.LastIndexOf("."));
            string imgname = imgdate + imgtype;
            FUImg.PostedFile.SaveAs(Server.MapPath("../*****") + @"/" + imgname);
            System.Drawing.Image image, aNewImage;
            image = System.Drawing.Image.FromStream(FUImg.PostedFile.InputStream);
            decimal width = image.Width;
            decimal height = image.Height;
            int newwidth, newheight;
            if (width > height)
            {
                newwidth = 150;
                newheight = (int)(height / width * 150);
            }
            else
            {
                newheight = 150;
                newwidth = (int)(width / height * 150);
            }
            aNewImage = image.GetThumbnailImage(newwidth, newheight, null, IntPtr.Zero);
            Bitmap output = new Bitmap(aNewImage);
            Graphics g = Graphics.FromImage(output);
            g.DrawString("水印文字", new Font("Courier New", 14), new SolidBrush(Color.Red), 60, 60);//写版权信息及文本格式及位置
            output.Save(Server.MapPath("../*****") + @"/s_" + imgname, System.Drawing.Imaging.ImageFormat.Jpeg);
        }


原创粉丝点击