发布文章自动上传图片并生成水印

本文介绍了一个自动上传HTML文本中的图片到服务器的功能实现。通过解析HTML代码,自动抓取并上传图片,同时可为图片添加水印,确保版权归属。

前段时间在网上看到新浪有一个提交文本(Html代码),自动上传图片的功能,觉得倒是很[实用,稍一琢磨,自己也写了这么一个东东。
根据所传入的Html代码,获取标签中的图片标签,将图片复制到当前服务器,替换Html代码中图片标签的src属性值。如果需要,还可以打上水印,堂堂正正的说版权归本公司(个人)所有,哈!

以下是主程序代码:

None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Text.RegularExpressions;
None.gif
using System.Net;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.IO;
None.gif
using System.Drawing.Imaging;
None.gif
None.gif
namespace JL.SWSystem.SWCommon.Mrhgw
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 根据传入的文本自动上传图片。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ImageUploadAutomatic
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
静态构造器#region 静态构造器
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 静态构造器。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        static ImageUploadAutomatic()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            htmimes[
".jpe"= "image/jpeg";
InBlock.gif            htmimes[
".jpeg"= "image/jpeg";
InBlock.gif            htmimes[
".jpg"= "image/jpeg";
InBlock.gif            htmimes[
".png"= "image/png";
InBlock.gif            htmimes[
".gif"= "image/gif";
InBlock.gif            htmimes[
".tif"= "image/tiff";
InBlock.gif            htmimes[
".tiff"= "image/tiff";
InBlock.gif            htmimes[
".bmp"= "image/bmp";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
变量#region 变量
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 存放包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串的集合对象。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static Hashtable htmimes = new Hashtable();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 传入的文本。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string text = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 文件上传的路径(非虚拟路径)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string filePath = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 图片的引用路径(非虚拟路径)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string refersImagePath = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 是否标记图片水印。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static bool isSignWatermark = false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 水印文本(以字符“|”标记换行。图片水印和文字水印选其一,优先图片水印)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string watermarkChars = "66xq.com";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 水印图片(非虚拟路径。图片水印和文字水印选其一,优先图片水印)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string watermarkImage = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 集合对象。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static List<ParamItem> imgPathColl = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 水印图片。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static Image imgWatermark = null;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置传入的文本。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn text; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ text = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置文件上传的路径(非虚拟路径)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string FilePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn filePath; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ filePath = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置图片的引用路径(非虚拟路径)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string RefersImagePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn refersImagePath; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ refersImagePath = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置是否标记图片水印。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static bool IsSignWatermark
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn isSignWatermark; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ isSignWatermark = true; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置水印文本(以字符“|”标记换行。图片水印和文字水印选其一,优先图片水印)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string WatermarkChars
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn watermarkChars; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ watermarkChars = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置水印图片(非虚拟路径。图片水印和文字水印选其一,优先图片水印)。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string WatermarkImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn watermarkImage; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ watermarkImage = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取已上传图片的集合对象。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static List<ParamItem> ImgPathColl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn imgPathColl; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重置所有参数#region 重置所有参数
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 重置所有参数。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Reset()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            text 
= "";
InBlock.gif            filePath 
= "";
InBlock.gif            refersImagePath 
= "";
InBlock.gif            isSignWatermark 
= false;
InBlock.gif            watermarkChars 
= "";
InBlock.gif            watermarkImage 
= "";
InBlock.gif            imgPathColl 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
提交操作#region 提交操作
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 提交操作。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void Submit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (text.Trim().Length == 0return;
InBlock.gif            imgPathColl 
= new List<ParamItem>();
InBlock.gif
InBlock.gif            
const string PATTERN_IMG = "<img\\s+[^>]*?src=[\"']?([^'\"\\s>]+)[\"']?[^>]*?>";
InBlock.gif
            text = Regex.Replace(text, PATTERN_IMG, new MatchEvaluator(ImageUploadAutomatic.ReplaceCC), RegexOptions.IgnoreCase);
InBlock.gif
InBlock.gif            
if (null != imgWatermark)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                imgWatermark.Dispose();
InBlock.gif                imgWatermark 
= null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 搜索指定的匹配项,替换图片路径。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="match">捕获的正则匹配项</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string ReplaceCC(Match match)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!match.Success) return "";
InBlock.gif
InBlock.gif            HttpWebRequest req 
= null;
InBlock.gif            HttpWebResponse rep 
= null;
InBlock.gif            Image img 
= null;
InBlock.gif            
string result = match.Value;
InBlock.gif
InBlock.gif            
for(int i=0;i<match.Groups.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i == 0continue;
InBlock.gif                Group gc 
= match.Groups[i];
InBlock.gif
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (gc.Value.Trim().Length == 0return result;
InBlock.gif
InBlock.gif                    
string newFilename = CreateNewFilename(gc.Value);
InBlock.gif                    
if (newFilename == string.Empty) return result;
InBlock.gif
InBlock.gif                    
if (isSignWatermark && watermarkImage.Trim().Length > 0 && null == imgWatermark)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        imgWatermark 
= Image.FromFile(HttpContext.Current.Server.MapPath(watermarkImage));
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//首先查看该图片是不是本站内的图片。
InBlock.gif
                    if (File.Exists(HttpContext.Current.Server.MapPath(gc.Value)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        img 
= Image.FromFile(HttpContext.Current.Server.MapPath(gc.Value));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//其它网站的图片资源。
InBlock.gif                        
//获取图片资源。
InBlock.gif
                        req = (HttpWebRequest)HttpWebRequest.Create(gc.Value);
InBlock.gif                        rep 
= (HttpWebResponse)req.GetResponse();
InBlock.gif
InBlock.gif                        
//生成图片。
InBlock.gif
                        img = Image.FromStream(rep.GetResponseStream());
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//标记水印。
InBlock.gif
                    if (isSignWatermark)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (watermarkImage.Trim().Length > 0)
InBlock.gif                            img 
= ImageWatermark.MarkImage(img, imgWatermark);
InBlock.gif                        
else
InBlock.gif                            img 
= ImageWatermark.MarkText(img);
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//获取文件扩展名。
InBlock.gif
                    string sEx = newFilename.Substring(newFilename.LastIndexOf(".")).ToLower(); 
InBlock.gif                    
//保存图像。
InBlock.gif
                    SaveImage(img, System.Web.HttpContext.Current.Server.MapPath(filePath + "/" + newFilename), GetCodecInfo((string)htmimes[sEx]));
InBlock.gif                    
//添加上传路径到集合中,供其它地方调用。
InBlock.gif
                    imgPathColl.Add(new ParamItem(gc.Value, filePath + "/" + newFilename));
InBlock.gif                    
//替换图片路径。
InBlock.gif
                    result = result.Replace(gc.Value, refersImagePath + "/" + newFilename);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (System.UriFormatException)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//此处屏蔽由于错误的URI或相对地址的站内url所引发的异常,
InBlock.gif                    
//并复原返回值。
InBlock.gif
                    result = match.Value;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw ex;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (null != img)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        img.Dispose();
InBlock.gif                        img 
= null;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (null != rep)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        rep.Close();
InBlock.gif                        rep 
= null;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 保存图片
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="image">Image 对象</param>
InBlock.gif        
/// <param name="savePath">保存路径</param>
ExpandedSubBlockEnd.gif        
/// <param name="ici">指定格式的编解码参数</param>

InBlock.gif        private static void SaveImage(System.Drawing.Image image, string savePath, ImageCodecInfo ici)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//设置 原图片 对象的 EncoderParameters 对象
InBlock.gif
            EncoderParameters parameters = new EncoderParameters(1);
InBlock.gif            parameters.Param[
0= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)90));
InBlock.gif            image.Save(savePath, ici, parameters);
InBlock.gif            parameters.Dispose();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取图像编码解码器的所有相关信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回图像编码解码器的所有相关信息</returns>

InBlock.gif        private static ImageCodecInfo GetCodecInfo(string mimeType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ImageCodecInfo[] CodecInfo 
= ImageCodecInfo.GetImageEncoders();
InBlock.gif            
foreach (ImageCodecInfo ici in CodecInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ici.MimeType == mimeType) return ici;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
生成新的图片文件名称#region 生成新的图片文件名称
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 生成新的图片名称。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="preImagePath">原文件路径</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回新的图片名称</returns>

InBlock.gif        private static string CreateNewFilename(string preImagePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//遍历先前加入集合中的图片,看是否存在相同路径的图片,
InBlock.gif            
//如果存在,则返回以前加入集合的新图片名称。
InBlock.gif
            foreach (ParamItem item in imgPathColl)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (preImagePath.Trim().ToLower() == item.PreImagePath.Trim().ToLower())
InBlock.gif                    
return string.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            preImagePath 
= preImagePath.Trim();
InBlock.gif            
int index = preImagePath.LastIndexOf(".");
InBlock.gif            
if (index < 0return string.Empty;
InBlock.gif
InBlock.gif            
return Guid.NewGuid().ToString() + preImagePath.Substring(index, preImagePath.Length - index);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

以下是参数类代码:

None.gifusing System;
None.gif
using System.Text;
None.gif
None.gif
namespace JL.Automatic
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 参数类。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ParamItem
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认构造器。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ParamItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造器(重载二)。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="preImagePath">原图片地址</param>
ExpandedSubBlockEnd.gif        
/// <param name="currentImagePath">当前图片链接地址</param>

InBlock.gif        internal ParamItem(string preImagePath, string currentImagePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.preImagePath = preImagePath;
InBlock.gif            
this.currentImagePath = currentImagePath;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 原图片地址。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string preImagePath = "";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前图片链接地址。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string currentImagePath = "";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置原图片地址。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string PreImagePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this.preImagePath; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.preImagePath = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置当前图片链接地址。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string CurrentImagePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this.currentImagePath; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.currentImagePath = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

以下是生成水图的代码:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Drawing;
None.gif
using System.Drawing.Drawing2D;
None.gif
using System.Drawing.Imaging;
None.gif
None.gif
namespace JL.SWSystem.SWCommon.Mrhgw
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 标记水印。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ImageWatermark
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
标记文本水印#region 标记文本水印
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 标记文本水印。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="preImage">原图片</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static Image MarkText(Image imgPhoto)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string copyrightString = "66xq.com";
InBlock.gif            
int phWidth = imgPhoto.Width;
InBlock.gif            
int phHeight = imgPhoto.Height;
InBlock.gif
InBlock.gif            
//Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
InBlock.gif
            Bitmap bmPhoto = new Bitmap(imgPhoto);
InBlock.gif            Bitmap bmWatermark 
= new Bitmap(imgPhoto);
InBlock.gif            bmPhoto.SetResolution(
7272);
InBlock.gif            Graphics grPhoto 
= Graphics.FromImage(imgPhoto);
InBlock.gif
InBlock.gif            grPhoto.SmoothingMode 
= SmoothingMode.AntiAlias;
InBlock.gif            grPhoto.CompositingQuality 
= CompositingQuality.HighQuality;
InBlock.gif            grPhoto.DrawImage(
InBlock.gif                imgPhoto,
InBlock.gif                
new Rectangle(00, phWidth, phHeight),
InBlock.gif                
0,
InBlock.gif                
0,
InBlock.gif                phWidth,
InBlock.gif                phHeight,
InBlock.gif                GraphicsUnit.Pixel);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] sizes = new int[] dot.gif16141210864 };
InBlock.gif            Font crFont 
= null;
InBlock.gif            SizeF crSize 
= new SizeF();
InBlock.gif
InBlock.gif            crFont 
= new Font("Lucida Sans Udot.gif"45,
InBlock.gif                FontStyle.Bold);
InBlock.gif            crSize 
= grPhoto.MeasureString(copyrightString,
InBlock.gif                crFont);
InBlock.gif
InBlock.gif            
//            for (int i=0 ;i<7; i++)
InBlock.gif            
//            { 
InBlock.gif            
//                crFont = new Font("arial", sizes[i],
InBlock.gif            
//                    FontStyle.Bold);
InBlock.gif            
//                crSize = grPhoto.MeasureString(Copyright,
InBlock.gif            
//                    crFont);
InBlock.gif            
//
InBlock.gif            
//                if((ushort)crSize.Width < (ushort)phWidth)
InBlock.gif            
//                    break;
InBlock.gif            
//            }
InBlock.gif

InBlock.gif            
int yPixlesFromBottom = (int)(phHeight * .05);
InBlock.gif            
//float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
InBlock.gif
            float yPosFromBottom = (phHeight - 100);
InBlock.gif            
float xCenterOfImg = (phWidth - 180);
InBlock.gif
InBlock.gif            StringFormat StrFormat 
= new StringFormat();
InBlock.gif            StrFormat.Alignment 
= StringAlignment.Center;
InBlock.gif
InBlock.gif            SolidBrush semiTransBrush2 
=
InBlock.gif                
new SolidBrush(Color.FromArgb(100000));
InBlock.gif
InBlock.gif            grPhoto.DrawString(copyrightString,
InBlock.gif                crFont,
InBlock.gif                semiTransBrush2,
InBlock.gif                
new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
InBlock.gif                StrFormat);
InBlock.gif
InBlock.gif            SolidBrush semiTransBrush 
= new SolidBrush(
InBlock.gif                Color.FromArgb(
100255255255));
InBlock.gif
InBlock.gif            grPhoto.DrawString(copyrightString,
InBlock.gif                crFont,
InBlock.gif                semiTransBrush,
InBlock.gif                
new PointF(xCenterOfImg, yPosFromBottom),
InBlock.gif                StrFormat);
InBlock.gif
InBlock.gif            Graphics g 
= Graphics.FromImage(bmPhoto);
InBlock.gif            g.SmoothingMode 
= SmoothingMode.AntiAlias;
InBlock.gif            g.CompositingQuality 
= CompositingQuality.HighQuality;
InBlock.gif            g.DrawString(copyrightString, crFont, semiTransBrush, 
new PointF(25010), StrFormat);
InBlock.gif
InBlock.gif            
return imgPhoto;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
标记图片水印#region 标记图片水印
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 标记图片水印。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="imgPhoto">原图片</param>
InBlock.gif        
/// <param name="imgWatermark">背景图片</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static Image MarkImage(Image imgPhoto, Image imgWatermark)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int phWidth = imgPhoto.Width;
InBlock.gif            
int phHeight = imgPhoto.Height;
InBlock.gif
InBlock.gif            
int wmWidth = imgWatermark.Width;
InBlock.gif            
int wmHeight = imgWatermark.Height;
InBlock.gif
InBlock.gif
InBlock.gif            
//Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
InBlock.gif
            Bitmap bmPhoto = new Bitmap(imgPhoto);
InBlock.gif            Bitmap bmWatermark 
= new Bitmap(bmPhoto);
InBlock.gif            Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
InBlock.gif            grPhoto.SmoothingMode 
= SmoothingMode.AntiAlias;
InBlock.gif            grPhoto.CompositingQuality 
= CompositingQuality.HighQuality;
InBlock.gif
InBlock.gif
InBlock.gif            bmWatermark.SetResolution(
InBlock.gif                imgPhoto.HorizontalResolution,
InBlock.gif                imgPhoto.VerticalResolution);
InBlock.gif
InBlock.gif            Graphics grWatermark 
= Graphics.FromImage(bmWatermark);
InBlock.gif
InBlock.gif
InBlock.gif            ImageAttributes imageAttributes 
=
InBlock.gif                
new ImageAttributes();
InBlock.gif            ColorMap colorMap 
= new ColorMap();
InBlock.gif
InBlock.gif            colorMap.OldColor 
= Color.FromArgb(25502550);
InBlock.gif            colorMap.NewColor 
= Color.FromArgb(0000);
ExpandedSubBlockStart.gifContractedSubBlock.gif            ColorMap[] remapTable 
= dot.gif{ colorMap };
InBlock.gif
InBlock.gif            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
float[][] colorMatrixElements = dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                    
new float[] dot.gif{1.0f,  0.0f,  0.0f,  0.0f0.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                    
new float[] dot.gif{0.0f,  1.0f,  0.0f,  0.0f0.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                    
new float[] dot.gif{0.0f,  0.0f,  1.0f,  0.0f0.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                    
new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.3f0.0f},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                    
new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.0f1.0f}
ExpandedSubBlockEnd.gif                                                }
;
InBlock.gif
InBlock.gif            ColorMatrix wmColorMatrix 
= new
InBlock.gif                ColorMatrix(colorMatrixElements);
InBlock.gif
InBlock.gif            
//imageAttributes.SetColorMatrix(wmColorMatrix,
InBlock.gif            
//    ColorMatrixFlag.Default,
InBlock.gif            
//    ColorAdjustType.Bitmap);
InBlock.gif

InBlock.gif            
int xPosOfWm = ((phWidth - wmWidth) - 10);
InBlock.gif            
int yPosOfWm = (phHeight - wmHeight) - 10;
InBlock.gif
InBlock.gif            grWatermark.DrawImage(imgWatermark,
InBlock.gif                
new Rectangle(xPosOfWm, yPosOfWm, wmWidth,
InBlock.gif                wmHeight),
InBlock.gif                
0,
InBlock.gif                
0,
InBlock.gif                wmWidth,
InBlock.gif                wmHeight,
InBlock.gif                GraphicsUnit.Pixel,
InBlock.gif                imageAttributes);
InBlock.gif
InBlock.gif            imgPhoto 
= bmWatermark;
InBlock.gif
InBlock.gif            grPhoto.Dispose();
InBlock.gif            grWatermark.Dispose();
InBlock.gif
InBlock.gif            
return imgPhoto;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


以下是程序引用代码:

None.gif        string content = ""//Html标签。
None.gif
        ImageUploadAutomatic.Text = content; //设置Html标签。
None.gif
        ImageUploadAutomatic.FilePath = "../Images/Uploads"//设置图片上传路径。
None.gif
        ImageUploadAutomatic.RefersImagePath= "/Images/Uploads"//设置图片引用路径。
None.gif
        ImageUploadAutomatic.IsSignWatermark = true//确定生成水印。
None.gif
        ImageUploadAutomatic.WatermarkImage = "../Images/Mark.png"//水印图片路径。
None.gif
        ImageUploadAutomatic.Submit(); //提交生成。
None.gif
        content = ImageUploadAutomatic.Text; //此处返回修改后的Html标签(仅修改了图片的链接地址)。
None.gif

转载于:https://www.cnblogs.com/mrhgw/archive/2006/10/17/531322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值