一个关于图片处理的代码

转载 2006年06月06日 15:25:00

一个关于图片处理的代码

/*
 * 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

}

相关文章推荐

picasa-一个很好的图片处理工具

  • 2007年07月20日 08:41
  • 4.48MB
  • 下载

分享一个Android中的图片处理工具

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terryblog.blog.51cto.com/1764499/791516 ...
  • fso918
  • fso918
  • 2013年12月25日 19:04
  • 793

一个小的图片处理程序

  • 2008年09月16日 22:56
  • 388KB
  • 下载

图片处理代码

  • 2013年11月15日 15:41
  • 4KB
  • 下载

阿里云OSS图片处理如何借助CDN将多个域名绑定一个bucket(channel)上

阿里云OSS 图片处理如何借助CDN将多个域名绑定一个bucket(channel)上无论是从优化浏览器行为上还是处于其他原因(比如说oss对外限制bucket个数为10个), 现在有很多用户想在...

网页图片处理代码全集整理

  • 2009年09月03日 11:51
  • 21KB
  • 下载

图片处理代码

  • 2014年05月23日 11:55
  • 2KB
  • 下载

Glide-----一个新的android图片处理开源项目

英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google 首发地址  http://jc...
  • nnmmbb
  • nnmmbb
  • 2016年01月25日 15:38
  • 1109

压缩感知图片处理代码

  • 2014年05月20日 16:30
  • 50KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:一个关于图片处理的代码
举报原因:
原因补充:

(最多只允许输入30个字)