GDI+图片毛玻璃效果的实现
分享一个图片毛玻璃效果的算法
原理:
从原像素的某个范围内随机选取一部分像素,讲其进行Alpha混合模糊得到目标像素值
下面上代码:
效果图
原图:

效果图:

原理:
从原像素的某个范围内随机选取一部分像素,讲其进行Alpha混合模糊得到目标像素值
下面上代码:
/// <summary>
/// 对颜色数组进行混合
/// </summary>
/// <param name="colors"></param>
/// <returns></returns>
public static Color BlendColor(Color[] colors)
{
if (colors.Length <= 0)
return Color.Transparent;
ulong asum = 0, rsum = 0, gsum = 0, bsum = 0;
for (int i = 0, len = colors.Length; i < len; i++)
{
asum += colors[i].A;
rsum += (ulong)(colors[i].A*colors[i].R);
gsum += (ulong)(colors[i].G*colors[i].A);
bsum += (ulong)(colors[i].B*colors[i].A);
}
if (asum == 0)
return Color.Transparent;
rsum /= asum;
gsum /= asum;
bsum /= asum;
asum /= (ulong)colors.Length;
return Color.FromArgb((int)asum, (int)rsum, (int)gsum, (int)bsum);
}
/// <summary>
/// 毛玻璃效果
/// </summary>
/// <param name="srcBmp">源图片</param>
/// <param name="minRadius">最小离散半径</param>
/// <param name="maxRadius">最大离散半径</param>
/// <param name="samples">采样点数</param>
/// <returns></returns>
public static Bitmap FrostedEffect(Bitmap srcBmp, int minRadius, int maxRadius, int samples)
{
int width = srcBmp.Width;
int height = srcBmp.Height;
Bitmap targBmp = new Bitmap(width, height, srcBmp.PixelFormat);
BitmapData srcData = srcBmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, srcBmp.PixelFormat);
BitmapData targData = targBmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, targBmp.PixelFormat);
int pxsize = Image.GetPixelFormatSize(srcBmp.PixelFormat) / 8;//像素大小
bool bAlpha = Image.IsAlphaPixelFormat(srcBmp.PixelFormat);
int offset = srcData.Stride - srcData.Width * pxsize;
Random rand = new Random();
Color[] sampleColors = new Color[samples];
unsafe
{
byte* srcptr = (byte*)srcData.Scan0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int s = 0; s < samples; s++)
{
double d = rand.Next(minRadius, maxRadius);
double angle = rand.NextDouble() * Math.PI * 2;
double p = Math.Sin(angle);
int samh = (int)(i + Math.Sin(angle) * d);
int samw = (int)(j + Math.Cos(angle) * d);
samh = samh < 0 ? 0 : samh > height - 1 ? height - 1 : samh;
samw = samw < 0 ? 0 : samw > width ? width : samw;
byte* ptr = srcptr + samh * srcData.Stride + samw * pxsize;
if (bAlpha)
sampleColors[s] = Color.FromArgb(*(ptr + 3), *(ptr + 2), *(ptr + 1), *ptr);
else
sampleColors[s] = Color.FromArgb(*(ptr + 2), *(ptr + 1), *ptr);
}
Color col = BlendColor(sampleColors);
byte* targptr = (byte*)targData.Scan0 + srcData.Stride * i + j*pxsize;
*targptr = col.B;
*(targptr + 1) = col.G;
*(targptr + 2) = col.R;
if (bAlpha)
*(targptr + 3) = col.A;
}
}
}
srcBmp.UnlockBits(srcData);
targBmp.UnlockBits(targData);
return targBmp;
}
效果图
原图:
效果图:
...全文
相关推荐
VB在XP下速度超快的磨砂效果(毛玻璃效果)
VB在XP下速度超快的磨砂效果,速度超快,不占用很多资源,没有用任何其他的控件,花了好长时间找到的!
易语言毛玻璃特效源码分析与实战
本文还有配套的精品资源,点击获取 简介:易语言是一种中文编程语言,它简化了编程的学习和应用。提供的压缩包中包含了用易语言编写的毛玻璃特效源代码,这是一种在UI设计中常用的视觉效果。通过毛玻璃特效,背景图像经过模糊处理后能产生一种朦胧感,增强界面的现代感和层次感。本源码涉及的关键技术点包括窗口对象与控件的操作、图形处理技术、事件处理、内存位图使用、颜色空间转换、性能优化和资源...
易语言毛玻璃特效源码-易语言
易语言毛玻璃特效源码
易语言源码模块-图片按钮v2.0
易语言源码模块_图片按钮v2.0
实用代码脚本易语言源码闪屏王代码
实用代码脚本易语言源码闪屏王代码


