本来想写一个将图片处理成磨砂玻璃的函数,从网上找到一个C#的类,小日本的代码,功能较多,没有注释。处理出来的效果还可以,但不是我想要的
ImageUtils.cs
用这个类写的“磨砂玻璃”效果如下:
代码:
public
static
bool
FrostedGlass(
ref
Bitmap bmp, Rectangle rect,
int
alphaPercent,
int
blurZone)
{
if
(bmp.PixelFormat
!=
PixelFormat.Format24bppRgb)
return
false
;
int
w
=
bmp.Width;
int
h
=
bmp.Height;
Bitmap tmp
=
new
Bitmap(rect.Width, rect.Height,
PixelFormat.Format24bppRgb);
Graphics g
=
Graphics.FromImage(tmp);
g.DrawImage(bmp,
0
,
0
, rect, GraphicsUnit.Pixel);
g.Dispose();
ImgUtils.GaussianBlur(
ref
tmp, blurZone);
Bitmap tmp2
=
tmp.Clone()
as
Bitmap;
g
=
Graphics.FromImage(tmp2);
g.Clear(Color.FromArgb(
244
,
244
,
244
));
ImgUtils.JAlphaBlend(g, tmp, (
float
)alphaPercent
/
100f,
0
,
0
);
g.Dispose();
tmp.Dispose();
g
=
Graphics.FromImage(bmp);
g.DrawImageUnscaled(tmp2, rect.Left, rect.Top);
g.Dispose();
tmp2.Dispose();
return
true
;
}
private
void
button1_Click(
object
sender, EventArgs e)
{
Bitmap bmp
=
new
Bitmap(
@"
C:¥Home¥img2¥reika1.png
"
);
Graphics g
=
this
.CreateGraphics();
//
g.DrawImageUnscaled(bmp, 5, 5);
if
(FrostedGlass(
ref
bmp,
new
Rectangle(
88
,
50
,
219
,
550
),
50
,
3
))
{
g.DrawImageUnscaled(bmp,
5
,
5
);
Clipboard.SetImage(bmp);
}
g.Dispose();
bmp.Dispose();
}