添加水印(文字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);
??????????? }
轉載于:https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133306.html
總結
以上是生活随笔為你收集整理的添加水印(文字and图片)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的KT库之-----扩展方法
- 下一篇: Comperhend the OP-si