关闭

一个关于图片处理的代码

标签: imageborderfloatstringclasssystem
759人阅读 评论(0) 收藏 举报

一个关于图片处理的代码

/*
 * Create by Alex Bai @ 2005-04-11 23:05
 *
 * Description: a Image Dll about Operation to Image
 *
 * 1. Add Water Mark to Image
 * 2. Add Characters to Image
 * 3. Add Border to Image
 * 4. ReSize the Image                            doing
 * 5. Cut the Image                                Next
 * 6. Read Exif from Image                       Next
 *
 * 加上了可以选择水印图片透明度的功能,但是这样需要对水印图片进行索引,比较慢
 * 所以如果不选择透明度的话,默认用另外一种不索引的方法
 *
 * */
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Alex.ImageExt
{
    Public Class#region Public Class

    /**//// <summary>
    /// Image Ext.
    /// </summary>
    public class ImageExt
    {
        构造函数#region 构造函数
        public ImageExt()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        Add Water Mark to Image#region Add Water Mark to Image

        Image X, Y#region Image X, Y
        /**//// <summary>
        /// Draw WaterMark to Src Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="waterMark">WaterMark Image</param>
        /// <param name="x">WaterMark's Location X</param>
        /// <param name="y">WaterMark's Location Y</param>
        /// <returns>Image</returns>
        public Image DrawWaterMark(Image srcImage, Image waterMark, int x, int y)
        {
            Graphics g = Graphics.FromImage(srcImage);

            g.DrawImage(waterMark, x, y);

            g.Dispose();
            return srcImage;
        }
        #endregion

        Image X, Y + Alpha Rate#region Image X, Y + Alpha Rate
        /**//// <summary>
        /// Draw WaterMark to Src Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="waterMark">WaterMark Image</param>
        /// <param name="x">WaterMark's Location X</param>
        /// <param name="y">WaterMark's Location Y</param>
        /// <param name="alphaRate">WaterMark's alpha rate, 0 to 255, 0 means total transparent, 255 means don't transparent</param>
        /// <returns>Image</returns>
        public Image DrawWaterMark(Image srcImage, Image waterMark, int x, int y, int alphaRate)
        {
            Graphics g = Graphics.FromImage(srcImage);
       
            if(alphaRate < 0 || alphaRate > 255)
                alphaRate = 255;

            //Transfer Image to Bitmap to Get Pixel
            Bitmap bmWaterMark = new Bitmap(waterMark);

            for(int ix = 0; ix < waterMark.Width; ix ++)
            {
                for(int iy = 0; iy < waterMark.Height; iy ++)
                {
                    int ir = bmWaterMark.GetPixel(ix, iy).R;
                    int ig = bmWaterMark.GetPixel(ix, iy).G;
                    int ib = bmWaterMark.GetPixel(ix, iy).B;

                    g.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(alphaRate, ir, ig, ib))), x + ix, y + iy, 1, 1);
                }
            }

            g.Dispose();
            return srcImage;
        }

        #endregion

        Image Postion#region Image Postion
        /**//// <summary>
        /// Draw WaterMark to Src Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="waterMark">WaterMark Image</param>
        /// <param name="oPosition">WaterMark's position</param>
        /// <returns>Image</returns>
        public Image DrawWaterMark(Image srcImage, Image waterMark, ImagePostion oPosition)
        {
            int x = 0;
            int y = 0;

            Switch Postion get X and Y#region Switch Postion get X and Y
            switch(oPosition)
            {
                case ImagePostion.TopLeft :
                    x = 0;
                    y = 0;
                    break;
                case ImagePostion.TopCenter :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = 0;
                    break;
                case ImagePostion.TopRight :
                    x = srcImage.Width - waterMark.Width;
                    y = 0;
                    break;
                case ImagePostion.MiddleLeft :
                    x = 0;
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.MiddleMost :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.MiddleRight :
                    x = srcImage.Width - waterMark.Width;
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.BottomLeft :
                    x = 0;
                    y = srcImage.Height - waterMark.Height;
                    break;
                case ImagePostion.BottomCenter :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = srcImage.Height - waterMark.Height;
                    break;
                case ImagePostion.BottonRight :
                    x = srcImage.Width - waterMark.Width;
                    y = srcImage.Height - waterMark.Height;
                    break;
                default :
                    break;
            }
            #endregion

            srcImage = DrawWaterMark(srcImage, waterMark, x, y);

            return srcImage;
        }
        #endregion

        Image Postion + Alpha Rate#region Image Postion + Alpha Rate
        /**//// <summary>
        /// Draw WaterMark to Src Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="waterMark">WaterMark Image</param>
        /// <param name="oPosition">WaterMark's position</param>
        /// <param name="alphaRate">WaterMark's alpha rate, 0 to 255, 0 means total transparent, 255 means don't transparent</param>
        /// <returns>Image</returns>
        public Image DrawWaterMark(Image srcImage, Image waterMark, ImagePostion oPosition, int alphaRate)
        {
            int x = 0;
            int y = 0;

            Switch Postion get X and Y#region Switch Postion get X and Y
            switch(oPosition)
            {
                case ImagePostion.TopLeft :
                    x = 0;
                    y = 0;
                    break;
                case ImagePostion.TopCenter :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = 0;
                    break;
                case ImagePostion.TopRight :
                    x = srcImage.Width - waterMark.Width;
                    y = 0;
                    break;
                case ImagePostion.MiddleLeft :
                    x = 0;
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.MiddleMost :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.MiddleRight :
                    x = srcImage.Width - waterMark.Width;
                    y = (srcImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case ImagePostion.BottomLeft :
                    x = 0;
                    y = srcImage.Height - waterMark.Height;
                    break;
                case ImagePostion.BottomCenter :
                    x = (srcImage.Width / 2) - (waterMark.Width / 2);
                    y = srcImage.Height - waterMark.Height;
                    break;
                case ImagePostion.BottonRight :
                    x = srcImage.Width - waterMark.Width;
                    y = srcImage.Height - waterMark.Height;
                    break;
                default :
                    break;
            }
            #endregion

            srcImage = DrawWaterMark(srcImage, waterMark, x, y, alphaRate);

            return srcImage;
        }

        #endregion
       
        #endregion

        Add Characters To Image#region Add Characters To Image

        Text X, Y#region Text X, Y
        /**//// <summary>
        /// Draw Characters to Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="text">Text to add</param>
        /// <param name="textFont">Font of Text</param>
        /// <param name="textSize">Size of Text</param>
        /// <param name="textColor">Color of Text</param>
        /// <param name="x">Text's Location X</param>
        /// <param name="y">Text's Location Y</param>
        /// <returns>Image</returns>
        public Image DrawCharacter(Image srcImage, string text, string textFont, float textSize, Color textColor, float x, float y)
        {
            Graphics g = Graphics.FromImage(srcImage);
            Font f = new Font(textFont, textSize);
            Brush b = new SolidBrush(textColor);

            g.DrawString(text, f, b, x, y);
            g.Dispose();

            return srcImage;
        }

        #endregion

        Text Postion#region Text Postion
        /**//// <summary>
        /// Draw Characters to Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="text">Text to add</param>
        /// <param name="textFont">Font of Text</param>
        /// <param name="textSize">Size of Text</param>
        /// <param name="textColor">Color of Text</param>
        /// <param name="oPosition">Position of Text</param>
        /// <returns>Image</returns>
        public Image DrawCharacter(Image srcImage, string text, string textFont, float textSize, Color textColor, ImagePostion oPosition)
        {
            float x = 0;
            float y = 0;

            //Get the text's Width and Height
            Bitmap bm = new Bitmap(0, 0);
            Graphics g = Graphics.FromImage(bm);
            Font f = new Font(textFont, textSize);
            SizeF size =g.MeasureString(text, f);
            //float textLength = text.Length * textSize;
            float textWidth = size.Width;
            float textHeight = size.Height;

            Switch Postion get X and Y#region Switch Postion get X and Y
            switch(oPosition)
            {
                case ImagePostion.TopLeft :
                    x = 0;
                    y = 0;
                    break;
                case ImagePostion.TopCenter :
                    x = (srcImage.Width / 2) - (textWidth / 2);
                    y = 0;
                    break;
                case ImagePostion.TopRight :
                    x = srcImage.Width - textWidth;
                    y = 0;
                    break;
                case ImagePostion.MiddleLeft :
                    x = 0;
                    y = (srcImage.Height / 2) - (textHeight / 2);
                    break;
                case ImagePostion.MiddleMost :
                    x = (srcImage.Width / 2) - (textWidth / 2);
                    y = (srcImage.Height / 2) - (textHeight / 2);
                    break;
                case ImagePostion.MiddleRight :
                    x = srcImage.Width - textWidth;
                    y = (srcImage.Height / 2) - (textHeight / 2);
                    break;
                case ImagePostion.BottomLeft :
                    x = 0;
                    y = srcImage.Height - textSize;
                    break;
                case ImagePostion.BottomCenter :
                    x = (srcImage.Width / 2) - (textWidth / 2);
                    y = srcImage.Height - textHeight;
                    break;
                case ImagePostion.BottonRight :
                    x = srcImage.Width - textWidth;
                    y = srcImage.Height - textHeight;
                    break;
                default :
                    break;
            }
            #endregion

            srcImage = DrawCharacter(srcImage, text, textFont, textSize, textColor, x, y);

            return srcImage;
        }

        #endregion
       
        #endregion

        Add Border to Image#region Add Border to Image

        BorderStyle#region BorderStyle
        /**//// <summary>
        /// Add Border to Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="borderColor">Border Color</param>
        /// <param name="borderSize">Border Size</param>
        /// <param name="borderStyle">Border Style, Default All</param>
        /// <returns>Image</returns>
        public Image AddBorder(Image srcImage, Color borderColor, int borderWidth, BorderStyle borderStyle)
        {
            Graphics g = Graphics.FromImage(srcImage);
       
            Pen oPen = new Pen(borderColor, borderWidth);

            int x = 0;
            int y = 0;
            int width = 0;
            int height = 0;

            switch(borderStyle)
            {
                case BorderStyle.All:
                    AddBorder(srcImage, borderColor, borderWidth);
                    return srcImage;
                case BorderStyle.Top:
                    width = srcImage.Width;
                    height = borderWidth;
                    break;
                case BorderStyle.Left:
                    width = borderWidth;
                    height = srcImage.Height;
                    break;
                case BorderStyle.Right:
                    x = srcImage.Width - borderWidth;
                    width = borderWidth;
                    height = srcImage.Height;
                    break;
                case BorderStyle.Bottom:
                    y = srcImage.Height - borderWidth;
                    width = srcImage.Width;
                    height = borderWidth;
                    break;
            }

            g.DrawRectangle(oPen, x, y, width, height);

            return srcImage;
        }
        #endregion

        Border Style Default All#region Border Style Default All
        /**//// <summary>
        /// Add Border to Image
        /// </summary>
        /// <param name="srcImage">Src Image</param>
        /// <param name="borderColor">Border Color</param>
        /// <param name="borderSize">Border Size</param>
        /// <returns></returns>
        public Image AddBorder(Image srcImage, Color borderColor, int borderWidth)
        {
            Graphics g = Graphics.FromImage(srcImage);
       
            Pen oPen = new Pen(borderColor, borderWidth);

            int x = 0;
            int y = 0;
            int width = 0;
            int height = 0;

            //依次按照上左右下的顺序开始绘制边框

            //Top
            width = srcImage.Width;
            height = borderWidth;
            g.DrawRectangle(oPen, x, y, width, height);

            //Left
            width = borderWidth;
            height = srcImage.Height;
            g.DrawRectangle(oPen, x, y, width, height);

            //Right
            x = srcImage.Width - borderWidth;
            width = borderWidth;
            height = srcImage.Height;
            g.DrawRectangle(oPen, x, y, width, height);

            //Bottom
            x = 0;
            y = srcImage.Height - borderWidth;
            width = srcImage.Width;
            height = borderWidth;
            g.DrawRectangle(oPen, x, y, width, height);

            return srcImage;
        }
        #endregion

        #endregion

        ReSize the Image#region ReSize the Image
        public Image ReSizeImage(Image srcImage)
        {
           

            return srcImage;
        }
        #endregion

    }

    #endregion

    Public Enum#region Public Enum

    Positon Enum#region Positon Enum
    /**//// <summary>
    /// Water Mark Positon Enum, Total 9 positions
    /// </summary>
    public enum ImagePostion
    {
        TopLeft,
        TopCenter,
        TopRight,
        MiddleLeft,
        MiddleMost,
        MiddleRight,
        BottomLeft,
        BottomCenter,
        BottonRight
    }
    #endregion

    Border Style Enum#region Border Style Enum
    /**//// <summary>
    /// Border Style
    /// </summary>
    public enum BorderStyle
    {
        All,
        Top,
        Left,
        Right,
        Bottom
    }
    #endregion

    #endregion

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:38350次
    • 积分:495
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:21篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论
    javascript
    经典
    朋友