using
System;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Drawing.Drawing2D;
![](/Images/OutliningIndicators/None.gif)
public
class
Consts
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 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;
![](/Images/OutliningIndicators/InBlock.gif)
// sepia values
public const float SepiaRed = 0.2F;
public const float SepiaGreen = 0.14F;
public const float SepiaBlue = 0.08F;
![](/Images/OutliningIndicators/InBlock.gif)
//resize quality
public const System.Drawing.Drawing2D.InterpolationMode ResizeQuality = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
}
public
class
GDI
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public enum Dimensions
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Width,
Height
}
public enum AnchorPosition
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Top,
Center,
Bottom,
Left,
Right
}
![](/Images/OutliningIndicators/InBlock.gif)
public static Bitmap ScaleByPercent(Image imgPhoto, int Percent)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
float nPercent = ((float)Percent / 100);
![](/Images/OutliningIndicators/InBlock.gif)
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
![](/Images/OutliningIndicators/InBlock.gif)
int destX = 0;
int destY = 0;
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap ReSize(Image imgPhoto, int Size, Dimensions Dimension, out float percent)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
![](/Images/OutliningIndicators/InBlock.gif)
switch (Dimension)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case Dimensions.Width:
nPercent = ((float)Size / (float)sourceWidth);
break;
default:
nPercent = ((float)Size / (float)sourceHeight);
break;
}
![](/Images/OutliningIndicators/InBlock.gif)
percent = nPercent;
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap ReSize(Image imgPhoto, int Size, Dimensions Dimension)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
![](/Images/OutliningIndicators/InBlock.gif)
switch (Dimension)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap ReSize(Image imgPhoto, float percent)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = percent;
![](/Images/OutliningIndicators/InBlock.gif)
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap FixedSize(Image imgPhoto, int Width, int Height)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
![](/Images/OutliningIndicators/InBlock.gif)
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
![](/Images/OutliningIndicators/InBlock.gif)
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
![](/Images/OutliningIndicators/InBlock.gif)
//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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
}
![](/Images/OutliningIndicators/InBlock.gif)
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
![](/Images/OutliningIndicators/InBlock.gif)
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
![](/Images/OutliningIndicators/InBlock.gif)
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
![](/Images/OutliningIndicators/InBlock.gif)
if (nPercentH < nPercentW)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nPercent = nPercentW;
switch (Anchor)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case AnchorPosition.Top:
destY = 0;
break;
case AnchorPosition.Bottom:
destY = (int)(Height - (sourceHeight * nPercent));
break;
default:
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
break;
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nPercent = nPercentH;
switch (Anchor)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case AnchorPosition.Left:
destX = 0;
break;
case AnchorPosition.Right:
destX = (int)(Width - (sourceWidth * nPercent));
break;
default:
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
int destWidth = (int)Math.Ceiling((sourceWidth * nPercent));
int destHeight = (int)Math.Ceiling((sourceHeight * nPercent));
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap Croping(Image imgPhoto, Rectangle rect)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int destX = 0;
int destY = 0;
int destWidth = 0;
int destHeight = 0;
if (rect.X < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
destX = -rect.X;
}
if (rect.Y < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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(0, 0, destWidth, destHeight), rect, GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap makenew(Image imgPhoto, int x, int y, int w, int h)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int ww = imgPhoto.Width;
int hh = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(x, y, ww, hh), new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int ww = imgPhoto.Width;
int hh = imgPhoto.Height;
Bitmap bmPhoto = (Bitmap)imgPhoto.Clone();
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
switch (dir)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 1://up
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, 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(0, 0, where, hh), new Rectangle(where, 0, 10, hh),
GraphicsUnit.Pixel);
break;
case 4://right
grPhoto.DrawImage(imgPhoto,
new Rectangle(where, 0, ww - where, hh), new Rectangle(where - 10, 0, 10, hh),
GraphicsUnit.Pixel);
break;
}
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap hsi(Image imgPhoto)//,int h,int s,int i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
![](/Images/OutliningIndicators/InBlock.gif)
int destWidth = imgPhoto.Width;
int destHeight = imgPhoto.Height;
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(imgPhoto.Width, imgPhoto.Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.Bicubic;
![](/Images/OutliningIndicators/InBlock.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, destWidth, destHeight),
0, 0, sourceWidth, sourceHeight,
GraphicsUnit.Pixel, imageAttr);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
public static Bitmap rota(Image imgPhoto, float angle)//,int h,int s,int i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth;
int destHeight;
![](/Images/OutliningIndicators/InBlock.gif)
destWidth = sourceWidth;
destHeight = sourceHeight;
![](/Images/OutliningIndicators/InBlock.gif)
Bitmap bmPhoto = new Bitmap(imgPhoto.Width, imgPhoto.Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
Matrix myMatrix = new Matrix();
myMatrix.RotateAt(angle, new Point((int)(sourceWidth / 2), (int)(sourceHeight / 2)), MatrixOrder.Append);
grPhoto.Transform = myMatrix;
![](/Images/OutliningIndicators/InBlock.gif)
//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;
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, destWidth, destHeight),
new Rectangle(0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
![](/Images/OutliningIndicators/InBlock.gif)
grPhoto.Dispose();
return bmPhoto;
}
![](/Images/OutliningIndicators/InBlock.gif)
// return matrix for the specified saturation
public static Bitmap satuation(Image imgPhoto, float percent)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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
![](/Images/OutliningIndicators/InBlock.gif)
//calculate the scaling offset to apply to the red, green and blue colors
float v;
![](/Images/OutliningIndicators/InBlock.gif)
if (percent > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//range from 1 (no change) to 2.5 (a lot of saturation)
v = 1.0F + (0.015F * percent);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// range from 0.25 (almost grayscale) to 1 (no change)
v = 1.0F - (0.0075F * -percent);
}
![](/Images/OutliningIndicators/InBlock.gif)
float red = (1.0F - v) * Consts.GrayRed;
float green = (1.0F - v) * Consts.GrayGreen;
float blue = (1.0F - v) * Consts.GrayBlue;
![](/Images/OutliningIndicators/InBlock.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
ColorMatrix cm = new ColorMatrix();
![](/Images/OutliningIndicators/InBlock.gif)
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;
![](/Images/OutliningIndicators/InBlock.gif)
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ColorMatrix cm = new ColorMatrix(new float[][]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0.5f,0.5f,0.5f,0,0},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0.5f,0.5f,0.5f,0,0},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0.5f,0.5f,0.5f,0,0},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0,0,0,1,0,0},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0,0,0,0,1,0},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{0,0,0,0,0,1}});
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
float t = 0.5f * (1f - contrast);
![](/Images/OutliningIndicators/InBlock.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ColorMatrix cm = new ColorMatrix(new float[][]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ contrast , 0 , 0 , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , contrast , 0 , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , 0 , contrast , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , 0 , 0 , 1 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![](/Images/OutliningIndicators/InBlock.gif)
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ColorMatrix cm = new ColorMatrix(new float[][]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 1 , 0 , 0 , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , 1 , 0 , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , 0 , 1 , 0 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ 0 , 0 , 0 , 1 , 0 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new float[]
{ d , d , d , 0 , 1 }});
![](/Images/OutliningIndicators/InBlock.gif)
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;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
转载于:https://www.cnblogs.com/dreign/archive/2007/11/01/946232.html