GDI+图片毛玻璃效果的实现

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


C# code
?
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;
         }
给个效果图


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在C++中使用GDI+进行图片缩放可以通过以下步骤实现: 1. 引入GDI+库:首先需要在代码中引入GDI+库,可以使用以下代码: ```cpp #include <gdiplus.h> using namespace Gdiplus; ``` 2. 初始化GDI+:在使用GDI+之前,需要初始化GDI+库。可以在程序的入口处调用`GdiplusStartup`函数进行初始化,如下所示: ```cpp GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); ``` 3. 加载图片:使用`Bitmap`类加载需要进行缩放的图片,可以使用以下代码: ```cpp Bitmap* originalImage = new Bitmap(L"image.jpg"); ``` 4. 创建缩放后的图片:使用`Graphics`类创建一个新的`Bitmap`对象,并指定缩放后的尺寸,如下所示: ```cpp int newWidth = 200; // 新的宽度 int newHeight = 200; // 新的高度 Bitmap* resizedImage = new Bitmap(newWidth, newHeight); Graphics* graphics = Graphics::FromImage(resizedImage); ``` 5. 执行缩放操作:使用`Graphics`类的`DrawImage`函数将原始图片绘制到新创建的图片上,并指定缩放后的尺寸,如下所示: ```cpp graphics->DrawImage(originalImage, 0, 0, newWidth, newHeight); ``` 6. 保存缩放后的图片:使用`Bitmap`类的`Save`函数保存缩放后的图片到指定路径,如下所示: ```cpp resizedImage->Save(L"resized_image.jpg", ImageFormatJPEG); ``` 7. 清理资源:在程序结束时,需要释放使用的资源,可以使用以下代码: ```cpp delete originalImage; delete resizedImage; delete graphics; GdiplusShutdown(gdiplusToken); ``` 这样就完成了使用GDI+进行图片缩放的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值