C#图片加水印图片和文字

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;
using  System.Threading;
using  System.Drawing.Imaging;

namespace  WaterImage
{
    
public   partial   class  Form2 : Form
    {
        Image imgWeight;

        
public  Form2()
        {
            InitializeComponent();
        }
        
///   <summary>
        
///  从数据库中加载二进制图片
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         private   void  button1_Click( object  sender, EventArgs e)
        {
            
string  strSql  =   " Select Top 1 FileContent From Sys_FileSave " ;

            Byte[] byteImage 
=   new  Byte[ 0 ];

            byteImage 
=  (Byte[])(DbHelperSQL.GetSingle(strSql));

            MemoryStream stmBLOBData 
=   new  MemoryStream(byteImage);

            imgWeight 
=  Image.FromStream(stmBLOBData); pictureBox1.Image  =  imgWeight;
        }
        
///   <summary>
        
///  在原图片基础上加载文字水印
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         private   void  button2_Click( object  sender, EventArgs e)
        {
            Graphics GImage 
=  Graphics.FromImage(imgWeight);

            addWatermarkText(GImage, 
" 重量为60.00吨 " " WM_BOTTOM_RIGHT " , imgWeight.Width, imgWeight.Height);

            pictureBox1.Image 
=  imgWeight;
        }
        
///   <summary>
        
///  在原图片基础上加载图片水印
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         private   void  button3_Click( object  sender, EventArgs e)
        {
            Graphics GImage 
=  Graphics.FromImage(imgWeight);

            addWatermarkImage(GImage, 
@" C:\Documents and Settings\Administrator\桌面\Mark.png " " WM_TOP_LEFT " , imgWeight.Width, imgWeight.Height);

            pictureBox1.Image 
=  imgWeight;
        }
        
///   <summary>
        
///  生成图片缩略图
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         private   void  button4_Click( object  sender, EventArgs e)
        {
            GreateMiniImage(
@" C:\Documents and Settings\Administrator\桌面\Source.jpg " @" C:\Documents and Settings\Administrator\桌面\Small.png " 100 200 );
        }

        
///   <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 [] {  16 14 12 10 8 6 4  };
            Font crFont 
=   null ;
            SizeF crSize 
=   new  SizeF();
            
for  ( int  i  =   0 ; i  <   7 ; i ++ )
            {
                crFont 
=   new  Font( " arial " , sizes[i], FontStyle.Bold);
                crSize 
=  picture.MeasureString(_watermarkText, crFont);

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

            
float  xpos  =   0 ;
            
float  ypos  =   0 ;

            
switch  (_watermarkPosition)
            {
                
case   " WM_TOP_LEFT " :
                    xpos 
=  (( float )_width  *  ( float ). 01 +  (crSize.Width  /   2 );
                    ypos 
=  ( float )_height  *  ( float ). 01 ;
                    
break ;
                
case   " WM_TOP_RIGHT " :
                    xpos 
=  (( float )_width  *  ( float ). 99 -  (crSize.Width  /   2 );
                    ypos 
=  ( float )_height  *  ( float ). 01 ;
                    
break ;
                
case   " WM_BOTTOM_RIGHT " :
                    xpos 
=  (( float )_width  *  ( float ). 99 -  (crSize.Width  /   2 );
                    ypos 
=  (( float )_height  *  ( float ). 99 -  crSize.Height;
                    
break ;
                
case   " WM_BOTTOM_LEFT " :
                    xpos 
=  (( float )_width  *  ( float ). 01 +  (crSize.Width  /   2 );
                    ypos 
=  (( float )_height  *  ( float ). 99 -  crSize.Height;
                    
break ;
            }

            StringFormat StrFormat 
=   new  StringFormat();
            StrFormat.Alignment 
=  StringAlignment.Center;

            SolidBrush semiTransBrush2 
=   new  SolidBrush(Color.FromArgb( 153 0 0 0 ));
            picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos 
+   1 , ypos  +   1 , StrFormat);

            SolidBrush semiTransBrush 
=   new  SolidBrush(Color.FromArgb( 153 255 255 255 ));
            picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);


            semiTransBrush2.Dispose();
            semiTransBrush.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);

            ImageAttributes imageAttributes 
=   new  ImageAttributes();
            ColorMap colorMap 
=   new  ColorMap();

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

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            
float [][] colorMatrixElements  =  {
                                                
new   float [] { 1.0f ,   0.0f ,   0.0f ,   0.0f 0.0f },
                                                
new   float [] { 0.0f ,   1.0f ,   0.0f ,   0.0f 0.0f },
                                                
new   float [] { 0.0f ,   0.0f ,   1.0f ,   0.0f 0.0f },
                                                
new   float [] { 0.0f ,   0.0f ,   0.0f ,   0.3f 0.0f },
                                                
new   float [] { 0.0f ,   0.0f ,   0.0f ,   0.0f 1.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="oldpath"> 原图片地址 </param>
        
///   <param name="newpath"> 新图片地址 </param>
        
///   <param name="tWidth"> 缩略图的宽 </param>
        
///   <param name="tHeight"> 缩略图的高 </param>
         private   void  GreateMiniImage( string  oldpath,  string  newpath,  int  tWidth,  int  tHeight)
        {

            
try
            {

                System.Drawing.Image image 
=  System.Drawing.Image.FromFile(oldpath);
                
double  bl  =  1d;
                
if  ((image.Width  <=  image.Height)  &&  (tWidth  >=  tHeight))
                {
                    bl 
=  Convert.ToDouble(image.Height)  /  Convert.ToDouble(tHeight);
                }
                
else   if  ((image.Width  >  image.Height)  &&  (tWidth  <  tHeight))
                {
                    bl 
=  Convert.ToDouble(image.Width)  /  Convert.ToDouble(tWidth);

                }
                
else

                    
if  ((image.Width  <=  image.Height)  &&  (tWidth  <=  tHeight))
                    {
                        
if  (image.Height  /  tHeight  >=  image.Width  /  tWidth)
                        {
                            bl 
=  Convert.ToDouble(image.Width)  /  Convert.ToDouble(tWidth);

                        }
                        
else
                        {
                            bl 
=  Convert.ToDouble(image.Height)  /  Convert.ToDouble(tHeight);
                        }
                    }
                    
else
                    {
                        
if  (image.Height  /  tHeight  >=  image.Width  /  tWidth)
                        {
                            bl 
=  Convert.ToDouble(image.Height)  /  Convert.ToDouble(tHeight);

                        }
                        
else
                        {
                            bl 
=  Convert.ToDouble(image.Width)  /  Convert.ToDouble(tWidth);

                        }

                    }


                Bitmap b 
=   new  Bitmap(image, Convert.ToInt32(image.Width  /  bl), Convert.ToInt32(image.Height  /  bl));

                b.Save(newpath);
                b.Dispose();
                image.Dispose();


            }
            
catch
            {


            }

        }
    }
}


转载于:https://www.cnblogs.com/zhangpengshou/archive/2010/06/23/1763465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; 问题: C#批量添图片水印 回答: 在C#中,可以使用System.Drawing命名空间中的类来实现批量添图片水印的功能。首先,你需要引用System.Drawing命名空间,并使用System.IO命名空间来处理文件操作。然后,你可以使用Bitmap类来图片,使用Graphics类来绘制水印,并使用Image类来保存修改后的图片。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.IO; namespace WatermarkExample { class Program { static void Main(string[] args) { string watermarkText = "Watermark"; // 水印文本 string imagePath = "image.jpg"; // 图片路径 string outputDirectory = "output"; // 输出目录 // 创建输出目录 Directory.CreateDirectory(outputDirectory); // 图片 using (Bitmap image = new Bitmap(imagePath)) { // 创建Graphics对象 using (Graphics graphics = Graphics.FromImage(image)) { // 设置水印字体和颜色 Font font = new Font("Arial", 12); Brush brush = new SolidBrush(Color.Red); // 计算水印位置 SizeF textSize = graphics.MeasureString(watermarkText, font); PointF position = new PointF(image.Width - textSize.Width - 10, image.Height - textSize.Height - 10); // 绘制水印 graphics.DrawString(watermarkText, font, brush, position); } // 保存修改后的图片 string outputImagePath = Path.Combine(outputDirectory, "watermarked_image.jpg"); image.Save(outputImagePath); } Console.WriteLine("水印完成!"); } } } ``` 在上述示例代码中,你可以修改`watermarkText`变量来设置水印文本,`imagePath`变量来设置要添水印图片路径,`outputDirectory`变量来设置输出目录。运行代码后,修改后的图片将保存在输出目录中。请确保你已经正确引用了System.Drawing和System.IO命名空间,并且已经将图片文件放置在正确的路径下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值