图像处理类

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public class Consts
{
    
// grayscale values, these provide a better result then the weights 
    
// defined by the NTSC (North America Television Standards Committee) 
    
// which are 0.299, 0.587, 0.114
    public const float GrayRed = 0.3086F;
    
public const float GrayGreen = 0.6094F;
    
public const float GrayBlue = 0.082F;

    
// sepia values
    public const float SepiaRed = 0.2F;
    
public const float SepiaGreen = 0.14F;
    
public const float SepiaBlue = 0.08F;

    
//resize quality
    public const System.Drawing.Drawing2D.InterpolationMode ResizeQuality = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
}

public class GDI
{
    
public enum Dimensions
    
{
        Width,
        Height
    }

    
public enum AnchorPosition
    
{
        Top,
        Center,
        Bottom,
        Left,
        Right
    }


    
public static Bitmap ScaleByPercent(Image imgPhoto, int Percent)
    
{
        
float nPercent = ((float)Percent / 100);

        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;

        
int destX = 0;
        
int destY = 0;
        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap ReSize(Image imgPhoto, int Size, Dimensions Dimension, out float percent)
    
{
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;
        
int destX = 0;
        
int destY = 0;
        
float nPercent = 0;

        
switch (Dimension)
        
{
            
case Dimensions.Width:
                nPercent 
= ((float)Size / (float)sourceWidth);
                
break;
            
default:
                nPercent 
= ((float)Size / (float)sourceHeight);
                
break;
        }


        percent 
= nPercent;
        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap ReSize(Image imgPhoto, int Size, Dimensions Dimension)
    
{
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;
        
int destX = 0;
        
int destY = 0;
        
float nPercent = 0;

        
switch (Dimension)
        
{
            
case Dimensions.Width:
                nPercent 
= ((float)Size / (float)sourceWidth);
                
break;
            
default:
                nPercent 
= ((float)Size / (float)sourceHeight);
                
break;
        }

        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap ReSize(Image imgPhoto, float percent)
    
{
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;
        
int destX = 0;
        
int destY = 0;
        
float nPercent = percent;

        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap FixedSize(Image imgPhoto, int Width, int Height)
    
{
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;
        
int destX = 0;
        
int destY = 0;

        
float nPercent = 0;
        
float nPercentW = 0;
        
float nPercentH = 0;

        nPercentW 
= ((float)Width / (float)sourceWidth);
        nPercentH 
= ((float)Height / (float)sourceHeight);

        
//if we have to pad the height pad both the top and the bottom
        
//with the difference between the scaled height and the desired height
        if (nPercentH < nPercentW)
        
{
            nPercent 
= nPercentH;
            destX 
= (int)((Width - (sourceWidth * nPercent)) / 2);
        }

        
else
        
{
            nPercent 
= nPercentW;
            destY 
= (int)((Height - (sourceHeight * nPercent)) / 2);
        }


        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.Black);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
    
{
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int sourceX = 0;
        
int sourceY = 0;
        
int destX = 0;
        
int destY = 0;

        
float nPercent = 0;
        
float nPercentW = 0;
        
float nPercentH = 0;

        nPercentW 
= ((float)Width / (float)sourceWidth);
        nPercentH 
= ((float)Height / (float)sourceHeight);

        
if (nPercentH < nPercentW)
        
{
            nPercent 
= nPercentW;
            
switch (Anchor)
            
{
                
case AnchorPosition.Top:
                    destY 
= 0;
                    
break;
                
case AnchorPosition.Bottom:
                    destY 
= (int)(Height - (sourceHeight * nPercent));
                    
break;
                
default:
                    destY 
= (int)((Height - (sourceHeight * nPercent)) / 2);
                    
break;
            }

        }

        
else
        
{
            nPercent 
= nPercentH;
            
switch (Anchor)
            
{
                
case AnchorPosition.Left:
                    destX 
= 0;
                    
break;
                
case AnchorPosition.Right:
                    destX 
= (int)(Width - (sourceWidth * nPercent));
                    
break;
                
default:
                    destX 
= (int)((Width - (sourceWidth * nPercent)) / 2);
                    
break;
            }

        }


        
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
        
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));

        Bitmap bmPhoto 
= new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap Croping(Image imgPhoto, Rectangle rect)
    
{
        
int destX = 0;
        
int destY = 0;
        
int destWidth = 0;
        
int destHeight = 0;
        
if (rect.X < 0)
        
{
            destX 
= -rect.X;
        }

        
if (rect.Y < 0)
        
{
            destY 
= -rect.Y;
        }

        destWidth 
= rect.Width + destX;
        destHeight 
= rect.Height + destY;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;
        grPhoto.DrawImage(imgPhoto, 
new Rectangle(00, destWidth, destHeight), rect, GraphicsUnit.Pixel);
        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap makenew(Image imgPhoto, int x, int y, int w, int h)
    
{
        
int ww = imgPhoto.Width;
        
int hh = imgPhoto.Height;
        Bitmap bmPhoto 
= new Bitmap(w, h, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(x, y, ww, hh), new Rectangle(00, imgPhoto.Width, imgPhoto.Height),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
/// <summary>
    
/// mask done
    
/// </summary>
    
/// <param name="image">剪裁图像</param>
    
/// <param name="imagebak">全图像</param>
    
/// <param name="x">横向边距</param>
    
/// <param name="y">竖向边距</param>
    
/// <param name="w">色罩宽</param>
    
/// <param name="h">色罩高</param>
    
/// <param name="pos1">全图像在扫描图像中的起点</param>
    
/// <returns></returns>
  
    
/// <summary>
    
/// 边缘拉伸处理
    
/// </summary>
    
/// <param name="imgPhoto">图像</param>
    
/// <param name="dir">1:up ,2:down ,3:left ,4:right</param>
    
/// <param name="where">位置</param>
    
/// <returns></returns>        

    public static Bitmap streth(Image imgPhoto, int dir, int where)
    
{
        
int ww = imgPhoto.Width;
        
int hh = imgPhoto.Height;
        Bitmap bmPhoto 
= (Bitmap)imgPhoto.Clone();
        
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        
switch (dir)
        
{
            
case 1://up
                grPhoto.DrawImage(imgPhoto,
                    
new Rectangle(00, ww, where), new Rectangle(0, where, ww, 10),
                    GraphicsUnit.Pixel);
                
break;
            
case 2://down
                grPhoto.DrawImage(imgPhoto,
                    
new Rectangle(0, where, ww, hh - where), new Rectangle(0, where - 10, ww, 10),
                    GraphicsUnit.Pixel);
                
break;
            
case 3://left
                grPhoto.DrawImage(imgPhoto,
                    
new Rectangle(00, where, hh), new Rectangle(where, 010, hh),
                    GraphicsUnit.Pixel);
                
break;
            
case 4://right
                grPhoto.DrawImage(imgPhoto,
                    
new Rectangle(where, 0, ww - where, hh), new Rectangle(where - 10010, hh),
                    GraphicsUnit.Pixel);
                
break;
        }

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap hsi(Image imgPhoto)//,int h,int s,int i)
    {
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;

        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;

        Bitmap bmPhoto 
= new Bitmap(imgPhoto.Width, imgPhoto.Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.Bicubic;

        ColorMatrix myColorMatrix 
= new ColorMatrix();
        myColorMatrix.Matrix00 
= 1.75f// Red
        myColorMatrix.Matrix11 = 1.00f// Green
        myColorMatrix.Matrix22 = 1.00f// Blue
        myColorMatrix.Matrix33 = 1.00f// alpha
        myColorMatrix.Matrix44 = 1.00f// w
        
// Create an ImageAttributes object and set the color matrix.
        ImageAttributes imageAttr = new ImageAttributes();
        imageAttr.SetColorMatrix(myColorMatrix);

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(00, destWidth, destHeight),
            
00, sourceWidth, sourceHeight,
            GraphicsUnit.Pixel, imageAttr);

        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap rota(Image imgPhoto, float angle)//,int h,int s,int i)
    {
        
int sourceWidth = imgPhoto.Width;
        
int sourceHeight = imgPhoto.Height;
        
int destWidth;
        
int destHeight;

        destWidth 
= sourceWidth;
        destHeight 
= sourceHeight;

        Bitmap bmPhoto 
= new Bitmap(imgPhoto.Width, imgPhoto.Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        Matrix myMatrix 
= new Matrix();
        myMatrix.RotateAt(angle, 
new Point((int)(sourceWidth / 2), (int)(sourceHeight / 2)), MatrixOrder.Append);
        grPhoto.Transform 
= myMatrix;

        
//g.Clip=new Region(new Rectangle(this.croping.X,this.croping.Y,this.croping.Width+1,this.croping.Height+1));
        
//g.Transform = myMatrix2;
        
//g.DrawImage(image,rectd,rects, GraphicsUnit.Pixel );
        
//g.Transform = myMatrix;

        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(00, destWidth, destHeight),
            
new Rectangle(00, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        
return bmPhoto;
    }


    
// return matrix for the specified saturation
    public static Bitmap satuation(Image imgPhoto, float percent)
    
{
        
//set the saturation by scaling the red, green and blue values
        
//a value of 0 is grayscale, 1.0 is the orginial (no change) and 
        
//above 2.0 starts to look unnatural

        
//calculate the scaling offset to apply to the red, green and blue colors
        float v;

        
if (percent > 0)
        
{
            
//range from 1 (no change) to 2.5 (a lot of saturation)
            v = 1.0F + (0.015F * percent);
        }

        
else
        
{
            
// range from 0.25 (almost grayscale) to 1 (no change)
            v = 1.0F - (0.0075F * -percent);
        }


        
float red = (1.0F - v) * Consts.GrayRed;
        
float green = (1.0F - v) * Consts.GrayGreen;
        
float blue = (1.0F - v) * Consts.GrayBlue;

        
int destX = 0;
        
int destY = 0;
        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        ColorMatrix cm 
= new ColorMatrix();

        cm.Matrix00 
= red + v;
        cm.Matrix01 
= red;
        cm.Matrix02 
= red;
        cm.Matrix03 
= 0;
        cm.Matrix04 
= 0;
        cm.Matrix10 
= green;
        cm.Matrix11 
= green + v;
        cm.Matrix12 
= green;
        cm.Matrix13 
= 0;
        cm.Matrix14 
= 0;
        cm.Matrix20 
= blue;
        cm.Matrix21 
= blue;
        cm.Matrix22 
= blue + v;
        cm.Matrix23 
= 0;
        cm.Matrix24 
= 0;
        cm.Matrix30 
= 0;
        cm.Matrix31 
= 0;
        cm.Matrix32 
= 0;
        cm.Matrix33 
= 1;
        cm.Matrix34 
= 0;
        cm.Matrix40 
= 0;
        cm.Matrix41 
= 0;
        cm.Matrix42 
= 0;
        cm.Matrix43 
= 0;
        cm.Matrix44 
= 1;

        ImageAttributes attr 
= new ImageAttributes();
        attr.SetColorMatrix(cm);
        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            destX, destY, destWidth, destHeight,
            GraphicsUnit.Pixel, attr);
        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap gamma(Image imgPhoto, float percent)
    
{
        
int destX = 0;
        
int destY = 0;
        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;
        
float pp = percent / 100.0f + 1;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        ImageAttributes attr 
= new ImageAttributes();
        attr.SetGamma(pp, ColorAdjustType.Bitmap);
        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            destX, destY, destWidth, destHeight,
            GraphicsUnit.Pixel, attr);
        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap grayscale(Image imgPhoto)
    
{
        
int destX = 0;
        
int destY = 0;
        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;
       
        ColorMatrix cm 
= new ColorMatrix(new float[][]
            
new float[]{0.5f,0.5f,0.5f,0,0},
            
new float[]{0.5f,0.5f,0.5f,0,0},
            
new float[]{0.5f,0.5f,0.5f,0,0},
            
new float[]{0,0,0,1,0,0},
            
new float[]{0,0,0,0,1,0},
            
new float[]{0,0,0,0,0,1}}
);       

        
/*
        ColorMatrix cm = new ColorMatrix(new float[][]{ 
            new float[]{ Consts.GrayRed , Consts.GrayRed ,Consts.GrayRed , 0 , 0 },
            new float[]{ Consts.GrayGreen , Consts.GrayBlue , Consts.GrayBlue , 0 , 0 },
            new float[]{ Consts.GrayBlue , Consts.GrayBlue , Consts.GrayBlue , 0 , 0 },
            new float[]{ 0 ,0 , 0 , 1 , 0 , 0 },
            new float[]{ 0 ,0 , 0 , 0 , 1 , 0 },
            new float[]{ 0 ,0 , 0 , 0 , 0 , 1 }});
        
*/

        ImageAttributes attr 
= new ImageAttributes();
        attr.SetColorMatrix(cm);
        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            destX, destY, destWidth, destHeight,
            GraphicsUnit.Pixel, attr);
        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap contrast(Image imgPhoto, float contrast)
    
{
        
float t = 0.5f * (1f - contrast);

        
int destX = 0;
        
int destY = 0;
        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        ColorMatrix cm 
= new ColorMatrix(new float[][]
            
new float[]{ contrast , 0 , 0 , 0 , 0 },
            
new float[]0 , contrast , 0 , 0 , 0 },
            
new float[]0 , 0 , contrast , 0 , 0 },
            
new float[]0 , 0 , 0        , 1 , 0 },
            
new float[]{ t , t , t        , 0 , 1 }}
);
        
        ImageAttributes attr 
= new ImageAttributes();
        attr.SetColorMatrix(cm);
        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            destX, destY, destWidth, destHeight,
            GraphicsUnit.Pixel, attr);
        grPhoto.Dispose();
        
return bmPhoto;
    }

    
public static Bitmap brightness(Image imgPhoto, float d)
    
{      
        
int destX = 0;
        
int destY = 0;
        
int destWidth = imgPhoto.Width;
        
int destHeight = imgPhoto.Height;
        Bitmap bmPhoto 
= new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        Graphics grPhoto 
= Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;

        ColorMatrix cm 
= new ColorMatrix(new float[][]
            
new float[]1 , 0 , 0 , 0 , 0 },
            
new float[]0 , 1 , 0 , 0 , 0 },
            
new float[]0 , 0 , 1 , 0 , 0 },
            
new float[]0 , 0 , 0 , 1 , 0 },
            
new float[]{ d , d , d , 0 , 1 }}
);

        ImageAttributes attr 
= new ImageAttributes();
        attr.SetColorMatrix(cm);
        grPhoto.DrawImage(imgPhoto,
            
new Rectangle(destX, destY, destWidth, destHeight),
            destX, destY, destWidth, destHeight,
            GraphicsUnit.Pixel, attr);
        grPhoto.Dispose();
        
return bmPhoto;
    }


}
原文地址:https://www.cnblogs.com/dreign/p/946232.html