|

在 网上看到 转来的 可有高手翻译到易语言或者C++ C#没怎么学
/// <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].A*colors[i].G);
bsum += (ulong)(colors[i].A*colors[i].B);
}
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;
} |
|