zoukankan      html  css  js  c++  java
  • 添加水印(文字and图片)

    /// <summary>
            /// 返回新的后缀
            /// </summary>
            /// <param name="fromFileStream">文件流</param>
            /// <param name="fileSaveUrl">保存地址,后缀为gif,png,jpg,所有其它后缀会统一改成jpg</param>
            /// <param name="templateWidth">最大宽度</param>
            /// <param name="templateHeight">最大高度</param>
            /// <param name="jpgQuality">压缩质量</param>
            /// <returns></returns>
            public static string MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight, int jpgQuality)
            {
                //从文件取得图片对象,并使用流中嵌入的颜色管理信息
                System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
                //缩略图宽、高
                System.Double newWidth = myImage.Width, newHeight = myImage.Height;
                //宽大于模版的横图
                if (myImage.Width >= myImage.Height)
                {
                    if (myImage.Width > templateWidth)
                    {
                        //宽按模版,高按比例缩放
                        newWidth = templateWidth;
                        newHeight = myImage.Height * (newWidth / myImage.Width);
                    }
                }
                //高大于模版的竖图
                else
                {
                    if (myImage.Height > templateHeight)
                    {
                        //高按模版,宽按比例缩放
                        newHeight = templateHeight;
                        newWidth = myImage.Width * (newHeight / myImage.Height);
                    }
                }
                //取得图片大小
                System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
                //新建一个bmp图片
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
                //新建一个画板
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空一下画布
                g.Clear(Color.Transparent);
                //在指定位置画图
                g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
                System.Drawing.GraphicsUnit.Pixel);
                ///文字水印
                //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
                //System.Drawing.Font f=new Font("宋体",10);
                //System.Drawing.Brush b=new SolidBrush(Color.Black);
                //G.DrawString("myohmine",f,b,10,10);
                //G.Dispose();
                ///图片水印
                //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
                //Graphics a = Graphics.FromImage(bitmap);
                //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                //copyImage.Dispose();
                //a.Dispose();
                //copyImage.Dispose();
                //保存缩略图
                int i = fileSaveUrl.LastIndexOf('.');
                string name = fileSaveUrl.Substring(0, i);
                string exten = fileSaveUrl.Substring(i + 1).ToLower();
                ImageFormat format = ImageFormat.Jpeg;
                switch (exten)
                {
                    case "png": format = ImageFormat.Png; break;
                    case "gif": format = ImageFormat.Gif; break;
                    default: exten = "jpg"; fileSaveUrl = name + ".jpg"; break;
                }
                if (File.Exists(fileSaveUrl)) { File.Delete(fileSaveUrl); }
                if (format == ImageFormat.Jpeg)
                {
                    EncoderParameters encoderParams = new EncoderParameters();
                    long[] quality = new long[1];
                    quality[0] = jpgQuality;
                    EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                    encoderParams.Param[0] = encoderParam;
                    //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
                    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo jpegICI = null;
                    for (int x = 0;
                    x < arrayICI.Length;
                    x++)
                    {
                        if (arrayICI[x].FormatDescription.Equals("JPEG"))
                        {
                            jpegICI = arrayICI[x];
                            //设置JPEG编码
                            break;
                        }
                    }

                    bitmap.Save(fileSaveUrl, jpegICI, encoderParams);
                }
                else
                {
                    bitmap.Save(fileSaveUrl, format);
                }
                g.Dispose();
                myImage.Dispose();
                bitmap.Dispose();
                return exten;
            }

    //处理图片上传
                var fileCtl = FV.Row.FindControl("fuPicture") as FileUpload;
                if (fileCtl.HasFile)
                {
                    //上传图片

                    string path = (EditPersonalCode == "New" ? EditOrgCode : e.Keys["OrgCode"].ToString()).Replace('.', '/');
                    string fix = fileCtl.FileName.Substring(fileCtl.FileName.LastIndexOf('.') + 1).ToLower();
                    if (fix != "jpg" && fix != "png" && fix != "bmp" && fix != "gif")
                    {
                        e.Cancel = true;
                        ShowAlert("请重新选择照片上传\\n错误的图片格式:" + fix);
                        return;
                    }

                    string photoFilePath = Server.MapPath("~/OrgData/") + path;
                    if (!Directory.Exists(photoFilePath)) //如果不存在就建立
                    {
                        Directory.CreateDirectory(photoFilePath);
                    }

                    string fname = string.Format("s_{0}_{1}", e.Keys["PersonalCode"], DateTime.Now.TimeOfDay.TotalSeconds);
                    string newName = string.Format("{0}.{1}", fname, fix);//提示:必须用e.Keys["PersonalCode"]而不是EditPersonalCode

                    string fullpath = photoFilePath + "\\" + newName;
                    try
                    {
                        fix = Web.Base.ExtendClass.MakeSmallImg(fileCtl.FileContent, fullpath, 180, 240, 100);
                    }
                    catch
                    {
                        e.Cancel = true;
                        ShowAlert("无法识别的图片");
                        return;
                    }
                    newName = string.Format("{0}.{1}", fname, fix);//重新改变了后缀
                    e.NewValues.Add("PhotoFile", path + "/" + newName);
                }

  • 相关阅读:
    Jenkins安装(一)
    Ansible(一) 安装与简单测试
    zabbix中文乱码
    mysql5.7免密登录
    Zabbix 监控 Nginx 模板
    zabbix通过snmp监控主机
    zabbix5.0+grafana 使用脚本安装
    Eth-trunk配置-LACP模式
    Eth-Trunk配置-手动模式
    文件系统简单理解与实操(ext4)
  • 原文地址:https://www.cnblogs.com/yuhanzhong/p/2133306.html
Copyright © 2011-2022 走看看