通过图片颜色校正。在.NET中有一个5*5的 调色矩阵ColorMatrix,可以使用这个矩阵很方便并且高效的校正图片的颜色。 这里只提供一个简单的使用ColorMatrix进行颜色校正的例子:Color color = SkinDrawMethod.ColorManage.BaseColor; float R = (float)Convert.ToDouble(color.R) / 255; float G = (float)Convert.ToDouble(color.G) / 255; float B = (float)Convert.ToDouble(color.B) / 255; float[][] matrixItems ={ new float[] {R, 0, 0, 0, 0}, new float[] {0, G, 0, 0, 0}, new float[] {0, 0, B, 0, 0}, new float[] {0, 0, 0, 0.38f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imageAtt = new ImageAttributes(); imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int iWidth = bitmap.Width; int iHeight = bitmap.Height; graphics.DrawImage(bitmap, rect, 0.0f, 0.0f, iWidth, iHeight, GraphicsUnit.Pixel, imageAtt);用这段代码调整出来的颜色还是有点偏,但基本上说得过去。
今天研究了一天,终于想到了解决方法,贴出代码分享一下,原理就是画一个填入渐变颜色的矩形覆盖图片边缘,如果渐变的颜色和窗体的背景色相同的话就可以模拟QQ里面背景图片的效果. public enum BLURSTYLE { TOP,LEFT,RIGHT,BOTTOM } public Bitmap SetEdgeBlur(string imagePath, Color backColor,int blurRange,BLURSTYLE blurStyle) { Bitmap b = new Bitmap(imagePath); Graphics g = Graphics.FromImage(b); Rectangle rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height);
switch (blurStyle) { case BLURSTYLE.TOP: { rect = new System.Drawing.Rectangle(0, 0, b.Width, blurRange); using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Vertical)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.LEFT: { rect = new System.Drawing.Rectangle(0, 0, blurRange, b.Height); using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Horizontal)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.RIGHT: { rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height); using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Horizontal)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.BOTTOM: { rect = new System.Drawing.Rectangle(0, b.Height - blurRange, b.Width, blurRange); using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Vertical)) { g.FillRectangle(brush, rect); } } break; default: break; } return b; } public Bitmap SetEdgeBlur(Bitmap bitmap, Color backColor, int blurRange, BLURSTYLE blurStyle) { Bitmap b = bitmap; Graphics g = Graphics.FromImage(b); Rectangle rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height); switch (blurStyle) { case BLURSTYLE.TOP: { rect = new System.Drawing.Rectangle(0, 0, b.Width, blurRange); using (LinearGradientBrush brush = new LinearGradientBrush(rect,backColor, Color.FromArgb(0, backColor), LinearGradientMode.Vertical)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.LEFT: { rect = new System.Drawing.Rectangle(0, 0, blurRange, b.Height); using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Horizontal)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.RIGHT: { rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height); using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Horizontal)) { g.FillRectangle(brush, rect); } } break; case BLURSTYLE.BOTTOM: { rect = new System.Drawing.Rectangle(0, b.Height - blurRange, b.Width, blurRange); using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Vertical)) { g.FillRectangle(brush, rect); } } break; default: break; } return b; } 谢谢大家的关注
http://www.atalasoft.comhttp://www.accusoft.com
WPF
背景+渐变遮罩层就行了,GDI+没深入研究过,但是WPF实现起来很简单
http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx
调色矩阵ColorMatrix,可以使用这个矩阵很方便并且高效的校正图片的颜色。
这里只提供一个简单的使用ColorMatrix进行颜色校正的例子:Color color = SkinDrawMethod.ColorManage.BaseColor; float R = (float)Convert.ToDouble(color.R) / 255; float G = (float)Convert.ToDouble(color.G) / 255; float B = (float)Convert.ToDouble(color.B) / 255; float[][] matrixItems ={ new float[] {R, 0, 0, 0, 0}, new float[] {0, G, 0, 0, 0}, new float[] {0, 0, B, 0, 0}, new float[] {0, 0, 0, 0.38f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imageAtt = new ImageAttributes(); imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int iWidth = bitmap.Width; int iHeight = bitmap.Height; graphics.DrawImage(bitmap, rect, 0.0f, 0.0f, iWidth, iHeight, GraphicsUnit.Pixel, imageAtt);用这段代码调整出来的颜色还是有点偏,但基本上说得过去。
public enum BLURSTYLE
{
TOP,LEFT,RIGHT,BOTTOM
}
public Bitmap SetEdgeBlur(string imagePath, Color backColor,int blurRange,BLURSTYLE blurStyle)
{
Bitmap b = new Bitmap(imagePath);
Graphics g = Graphics.FromImage(b);
Rectangle rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height);
switch (blurStyle)
{
case BLURSTYLE.TOP:
{
rect = new System.Drawing.Rectangle(0, 0, b.Width, blurRange);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Vertical))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.LEFT:
{
rect = new System.Drawing.Rectangle(0, 0, blurRange, b.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Horizontal))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.RIGHT:
{
rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Horizontal))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.BOTTOM:
{
rect = new System.Drawing.Rectangle(0, b.Height - blurRange, b.Width, blurRange);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Vertical))
{
g.FillRectangle(brush, rect);
}
}
break;
default:
break;
}
return b;
}
public Bitmap SetEdgeBlur(Bitmap bitmap, Color backColor, int blurRange, BLURSTYLE blurStyle)
{
Bitmap b = bitmap;
Graphics g = Graphics.FromImage(b);
Rectangle rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height); switch (blurStyle)
{
case BLURSTYLE.TOP:
{
rect = new System.Drawing.Rectangle(0, 0, b.Width, blurRange);
using (LinearGradientBrush brush = new LinearGradientBrush(rect,backColor, Color.FromArgb(0, backColor), LinearGradientMode.Vertical))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.LEFT:
{
rect = new System.Drawing.Rectangle(0, 0, blurRange, b.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor, Color.FromArgb(0, backColor), LinearGradientMode.Horizontal))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.RIGHT:
{
rect = new System.Drawing.Rectangle(b.Width - blurRange, 0, blurRange, b.Height);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Horizontal))
{
g.FillRectangle(brush, rect); }
}
break;
case BLURSTYLE.BOTTOM:
{
rect = new System.Drawing.Rectangle(0, b.Height - blurRange, b.Width, blurRange);
using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, backColor), backColor, LinearGradientMode.Vertical))
{
g.FillRectangle(brush, rect);
}
}
break;
default:
break;
}
return b;
}
谢谢大家的关注