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


    代码
    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"100200);
            }

            
    /// <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[] { 16141210864 };
                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(153000));
                picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos 
    + 1, ypos + 1, StrFormat);

                SolidBrush semiTransBrush 
    = new SolidBrush(Color.FromArgb(153255255255));
                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(25502550);
                colorMap.NewColor 
    = Color.FromArgb(0000);
                ColorMap[] remapTable 
    = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                
    float[][] colorMatrixElements = {
                                                    
    new float[] {1.0f,  0.0f,  0.0f,  0.0f0.0f},
                                                    
    new float[] {0.0f,  1.0f,  0.0f,  0.0f0.0f},
                                                    
    new float[] {0.0f,  0.0f,  1.0f,  0.0f0.0f},
                                                    
    new float[] {0.0f,  0.0f,  0.0f,  0.3f0.0f},
                                                    
    new float[] {0.0f,  0.0f,  0.0f,  0.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), 00, 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
                {


                }

            }
        }
    }


  • 相关阅读:
    《java入门第一季》之Socket编程通信和TCP协议通信图解
    php __call 与 __callStatic
    js jquery提交表单不成功的问题原因
    js 获取对象属性的各种方法
    通达OA公共代码 php常用检测函数
    php中session的用法
    php中cookie的用法
    php session_id() session_name()
    禁用php函数的设置
    盐值加密-MD5
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/1763465.html
Copyright © 2020-2023  润新知