GDI+图片毛玻璃效果的实现
来源:互联网 发布:张韶涵妈妈 知乎 编辑:程序博客网 时间:2024/06/06 05:26
分享一个图片毛玻璃效果的算法
原理:
从原像素的某个范围内随机选取一部分像素,讲其进行Alpha混合模糊得到目标像素值
下面上代码:
原理:
从原像素的某个范围内随机选取一部分像素,讲其进行Alpha混合模糊得到目标像素值
下面上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// <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;
}
给个效果图
- GDI+图片毛玻璃效果的实现
- GDI+ 毛玻璃效果
- 如何在 iPhone 中实现图片的毛玻璃效果
- android实现的毛玻璃效果
- iOS毛玻璃效果的实现
- Android 毛玻璃效果的实现
- CSS3实现毛玻璃的效果
- 关于毛玻璃效果的实现
- 关于毛玻璃效果的实现
- CSS3实现毛玻璃(图片模糊)效果
- Android Fresco实现图片毛玻璃效果
- iOS毛玻璃效果的实现及图片模糊效果的三种方法
- iOS 图片毛玻璃效果
- 显示毛玻璃效果图片
- android 图片毛玻璃效果
- iOS开发之实现毛玻璃效果及图片模糊效果
- iOS毛玻璃效果的简单实现
- Android中毛玻璃效果的实现
- 银行业务调度系统
- usaco-1.3总结
- JVM分代垃圾回收策略的基础概念
- 母亲永远不会抛弃你
- python 中的三个读read(),readline()和readlines()
- GDI+图片毛玻璃效果的实现
- bootchart--检测linux启动性能的软件
- 蓝桥杯2011年决赛c++高职
- 天勤OJ 1098 大数处理之《N的阶乘》
- Struts2自定义拦截器实例—登陆权限验证
- 开发者拒绝写技术博客的常见理由
- html网页字体颜色设置大全
- 权限管理系统 页面权限粒度控制设计 GUID在数据库系统的应用
- Android中framework中的类如何获取development/samples下一个应用的Context?