一个关于图片处理的代码

转载 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

}

如何读项目代码

你有个任务,需要用到某个开源项目;或者老大交代你一个事情,让你去了解某个东西。怎么下手呢?如何开始呢? 先了解软件业务流程,弄清楚软件是干什么的,怎么干? 先得理清楚业务逻辑是怎样的~ 俺一般是照...
  • zhhz418418
  • zhhz418418
  • 2016年10月18日 14:06
  • 297

从把3000行代码重构成15行代码谈起

如果你认为这是一个标题党,那么我真诚的恳请你耐心的把文章的第一部分读完,然后再下结论。如果你认为能够戳中您的G点,那么请随手点个赞。 把三千行代码重构为15行 那年我刚毕业,进了现在这个公司。公司...
  • lz201234
  • lz201234
  • 2015年01月12日 09:34
  • 3601

我们就差一个写代码的了,记一次不靠谱的创业冲动

一个真实的故事。那年我24岁,他也24岁。 这是去年的事情吧,当时自己还年轻不懂事。过年时候突然接到之前公司一个同事的电话,他想做一个斗地主的游戏,想让我加入团队去帮忙算是技术入股。然后给我讲了一大堆...
  • fanyun7654
  • fanyun7654
  • 2014年03月13日 17:56
  • 1755

重构代码的一个例子

概述:重构无止境,重构你的代码,精雕细琢,千锤百炼。   我相信不少人都有这种感觉:某天闲暇下来回顾整理以前所写的代码,发现很多地方写得都不够好,代码中竟然存在着如此之多的“坏味道”。 ...
  • u012764241
  • u012764241
  • 2015年04月13日 10:25
  • 658

一个DVD的小代码

之前发的一个小代码,忘了写注释,就又把他删了 题目要求:DVD主界面                               0:DVD排行榜                        ...
  • ye876426110
  • ye876426110
  • 2016年11月29日 09:14
  • 137

编译一个c++代码

c++经典
  • hailiang70303
  • hailiang70303
  • 2018年01月15日 15:58
  • 17

分享一个水印代码

  • zuowensheng
  • zuowensheng
  • 2013年04月11日 15:05
  • 469

一个代码滥用的例子

原文地址:Code Abuse 吉姆的一个客户的
  • fendouzhe123
  • fendouzhe123
  • 2014年08月16日 15:03
  • 456

一个现金流量表的代码

* program SOURCE HEADER  : 现金流量表 * Program Name: * Description: * Date/Author: * Table Update: ...
  • paobing
  • paobing
  • 2013年07月15日 20:01
  • 327

一个现金流量表的代码

* program SOURCE HEADER  : 现金流量表 * Program Name: * Description: * Date/Author: * Table Update: * Spe...
  • zhongguomao
  • zhongguomao
  • 2015年06月02日 09:34
  • 726
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:一个关于图片处理的代码
举报原因:
原因补充:

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