• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c# 添加图片水印,可以指定水印位置+生成缩略图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace BLL
{
    
/**//// <summary>
    
/// 水印的类型
    
/// </summary>
    public enum WaterMarkType
    {
        
/**//// <summary>
        
/// 文字水印
        
/// </summary>
        TextMark,
        
/**//// <summary>
        
/// 图片水印
        
/// </summary>
        //ImageMark // 暂时只能添加文字水印
    };

    
/**//// <summary>
    
/// 水印的位置
    
/// </summary>
    public enum WaterMarkPosition
    {
        
/**//// <summary>
        
/// 左上角
        
/// </summary>
        WMP_Left_Top,
        
/**//// <summary>
        
/// 左下角
        
/// </summary>
        WMP_Left_Bottom,
        
/**//// <summary>
        
/// 右上角
        
/// </summary>
        WMP_Right_Top,
        
/**//// <summary>
        
/// 右下角
        
/// </summary>
        WMP_Right_Bottom
    };

    
/**//// <summary>
    
/// 处理图片的类(包括加水印,生成缩略图)
    
/// </summary>
    public class ImageWaterMark
    {
        
public ImageWaterMark()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        给图片加水印
#region 给图片加水印
        
/**//// <summary>
        
/// 添加水印(分图片水印与文字水印两种)
        
/// </summary>
        
/// <param name="oldpath">原图片绝对地址</param>
        
/// <param name="newpath">新图片放置的绝对地址</param>
        
/// <param name="wmtType">要添加的水印的类型</param>
        
/// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
        
/// 若要添加图片水印,此为图片的路径</param>
        public void addWaterMark(string oldpath, string newpath, 
            WaterMarkType wmtType, 
string sWaterMarkContent)
        {
            
try
            {
                Image image 
= Image.FromFile(oldpath);

                Bitmap b 
= new Bitmap(image.Width, image.Height, 
                    PixelFormat.Format24bppRgb);

                Graphics g 
= Graphics.FromImage(b);
                g.Clear(Color.White);
                g.SmoothingMode 
= SmoothingMode.HighQuality;
                g.InterpolationMode 
= InterpolationMode.High;

                g.DrawImage(image, 
00, image.Width, image.Height);

                
switch (wmtType)
                {
                    
/**//*case WaterMarkType.ImageMark:
                        //图片水印
                        this.addWatermarkImage(g, 
                            Page.Server.MapPath(Watermarkimgpath), 
                            WatermarkPosition,image.Width,image.Height);
                        break;
*/
                    
case WaterMarkType.TextMark:
                        
//文字水印
                        this.addWatermarkText(g, sWaterMarkContent, "WM_BOTTOM_RIGHT"
                            image.Width, image.Height);
                        
break;
                }

                b.Save(newpath);
                b.Dispose();
                image.Dispose();
            }
            
catch
            {
                
if(File.Exists(oldpath))
                {
                    File.Delete(oldpath);
                }
            }
            
finally
            {
                
if(File.Exists(oldpath))
                {
                    File.Delete(oldpath);
                }
            }  
        }

        
/**//// <summary>
        
///  加水印文字
        
/// </summary>
        
/// <param name="picture">imge 对象</param>
        
/// <param name="_watermarkText">水印文字内容</param>
        
/// <param name="_watermarkPosition">水印位置</param>
        
/// <param name="_width">被加水印图片的宽</param>
        
/// <param name="_height">被加水印图片的高</param>
        private void addWatermarkText(Graphics picture, string _watermarkText, 
            
string _watermarkPosition, int _width, int _height)
        {
            
// 确定水印文字的字体大小
            int[] sizes = new int[]{323028262422201816141210864};
            Font crFont 
= null;
            SizeF crSize 
= new SizeF();
            
for (int i = 0;i < sizes.Length; i++)
            {
                crFont 
= new Font("Arial Black", sizes[i], FontStyle.Bold);
                crSize 
= picture.MeasureString(_watermarkText, crFont);

                
if((ushort)crSize.Width < (ushort)_width)
                {
                    
break;
                }
            }

            
// 生成水印图片(将文字写到图片中)
            Bitmap floatBmp = new Bitmap((int)crSize.Width + 3
                           (
int)crSize.Height + 3, PixelFormat.Format32bppArgb);
            Graphics fg
=Graphics.FromImage(floatBmp);
            PointF pt
=new PointF(0,0);

            
// 画阴影文字
            Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
            Brush TransparentBrush1 
= new SolidBrush(Color.FromArgb(255, Color.Black));
            fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y 
+ 1); 
            fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X 
+ 1, pt.Y); 

            fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X 
+ 1, pt.Y + 1); 
            fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y 
+ 2); 
            fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X 
+ 2, pt.Y); 

            TransparentBrush0.Dispose(); 
            TransparentBrush1.Dispose();

            
// 画文字
            fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
            fg.DrawString(_watermarkText, 
                crFont, 
new SolidBrush(Color.White), 
                pt.X, pt.Y, StringFormat.GenericDefault);

            
// 保存刚才的操作
            fg.Save(); 
            fg.Dispose();
            
// floatBmp.Save("d:\\WebSite\\DIGITALKM\\ttt.jpg");

            
// 将水印图片加到原图中
            this.addWatermarkImage(
                picture, 
                
new Bitmap(floatBmp), 
                
"WM_BOTTOM_RIGHT"
                _width, 
                _height);
        }

        
/**//// <summary>
        
///  加水印图片
        
/// </summary>
        
/// <param name="picture">imge 对象</param>
        
/// <param name="iTheImage">Image对象(以此图片为水印)</param>
        
/// <param name="_watermarkPosition">水印位置</param>
        
/// <param name="_width">被加水印图片的宽</param>
        
/// <param name="_height">被加水印图片的高</param>
        private void addWatermarkImage( Graphics picture,Image iTheImage, 
            
string _watermarkPosition,int _width,int _height)
        {
            Image watermark 
= new Bitmap(iTheImage);
 
            ImageAttributes imageAttributes 
= new ImageAttributes();
            ColorMap colorMap 
= new ColorMap();

            colorMap.OldColor 
= Color.FromArgb(25502550);
            colorMap.NewColor 
= Color.FromArgb(0000);
            ColorMap[] remapTable 
= {colorMap};

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            
float[][] colorMatrixElements = {
                                                
new float[] {1.0f0.0f0.0f0.0f0.0f},
                                                
new float[] {0.0f1.0f0.0f0.0f0.0f},
                                                
new float[] {0.0f0.0f1.0f0.0f0.0f},
                                                
new float[] {0.0f0.0f0.0f0.3f0.0f},
                                                
new float[] {0.0f0.0f0.0f0.0f1.0f}
                                            };
 
            ColorMatrix colorMatrix 
= new ColorMatrix(colorMatrixElements);
 
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            
int xpos = 0;
            
int ypos = 0;
            
int WatermarkWidth = 0;
            
int WatermarkHeight = 0;
            
double bl = 1d;

            
//计算水印图片的比率
            
//取背景的1/4宽度来比较
            if ((_width > watermark.Width * 4&& (_height > watermark.Height * 4))
            {
                bl 
= 1;
            }
            
else if ((_width > watermark.Width * 4&& (_height<watermark.Height * 4))
            {
                bl 
= Convert.ToDouble(_height / 4/ Convert.ToDouble(watermark.Height);
            
            }
            
else if ((_width < watermark.Width * 4&& (_height > watermark.Height * 4))
            {
                bl 
= Convert.ToDouble(_width / 4/ Convert.ToDouble(watermark.Width);
            }
            
else
            {
                
if ((_width * watermark.Height) > (_height * watermark.Width))
                {
                    bl 
= Convert.ToDouble(_height / 4/ Convert.ToDouble(watermark.Height);
                    
                }
                
else
                {
                    bl 
= Convert.ToDouble(_width / 4/ Convert.ToDouble(watermark.Width);
                    
                }
            
            }

            WatermarkWidth 
= Convert.ToInt32(watermark.Width * bl);
            WatermarkHeight 
= Convert.ToInt32(watermark.Height * bl);

            
switch (_watermarkPosition)
            {
                
case "WM_TOP_LEFT":
                    xpos 
= 10;
                    ypos 
= 10;
                    
break;
                
case "WM_TOP_RIGHT":
                    xpos 
= _width - WatermarkWidth - 10;
                    ypos 
= 10;
                    
break;
                
case "WM_BOTTOM_RIGHT":
                    xpos 
= _width - WatermarkWidth - 10;
                    ypos 
= _height -WatermarkHeight - 10;
                    
break;
                
case "WM_BOTTOM_LEFT":
                    xpos 
= 10;
                    ypos 
= _height - WatermarkHeight - 10;
                    
break;
            }

            picture.DrawImage(
                watermark, 
                
new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 
                
0
                
0
                watermark.Width, 
                watermark.Height, 
                GraphicsUnit.Pixel, 
                imageAttributes);

            watermark.Dispose();
            imageAttributes.Dispose();
        }

        
/**//// <summary>
        
///  加水印图片
        
/// </summary>
        
/// <param name="picture">imge 对象</param>
        
/// <param name="WaterMarkPicPath">水印图片的地址</param>
        
/// <param name="_watermarkPosition">水印位置</param>
        
/// <param name="_width">被加水印图片的宽</param>
        
/// <param name="_height">被加水印图片的高</param>
        private void addWatermarkImage( Graphics picture,string WaterMarkPicPath, 
            
string _watermarkPosition,int _width,int _height)
        {
            Image watermark 
= new Bitmap(WaterMarkPicPath);
 
            
this.addWatermarkImage(picture, watermark, _watermarkPosition, _width, 
                _height);
        }
        
#endregion

        生成缩略图
#region 生成缩略图
        
/**//// <summary>
        
/// 保存图片
        
/// </summary>
        
/// <param name="image">Image 对象</param>
        
/// <param name="savePath">保存路径</param>
        
/// <param name="ici">指定格式的编解码参数</param>
        private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
        {
            
//设置 原图片 对象的 EncoderParameters 对象
            EncoderParameters parameters = new EncoderParameters(1);
            parameters.Param[
0= new EncoderParameter(
                System.Drawing.Imaging.Encoder.Quality, ((
long90));
            image.Save(savePath, ici, parameters);
            parameters.Dispose();
        }

        
/**//// <summary>
        
/// 获取图像编码解码器的所有相关信息
        
/// </summary>
        

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#事务发布时间:2022-07-10
下一篇:
不用现有方法,把string转换成int型[C#]发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap