欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#自动生成漂亮的水晶效果头像的实现代码

程序员文章站 2024-02-19 20:46:22
与其他的微博系统相同,在“多可内网微博系统”的用户也可上传自己的头像,并支持头像裁剪。 但“多可内网微博系统”的头像可以更漂亮,因为系统实现了水晶效果的头像。c#程序实现...

与其他的微博系统相同,在“多可内网微博系统”的用户也可上传自己的头像,并支持头像裁剪。

但“多可内网微博系统”的头像可以更漂亮,因为系统实现了水晶效果的头像。
c#程序实现水晶效果头像的过程是:

(1)图像缩略到宽度或高度=90的头像;

(2)由用户选择合适的位置裁剪90x90的最终头像;

(3)添加水晶效果;

代码奉献:

复制代码 代码如下:

/// <summary>
/// 绘制水晶效果的头像
/// </summary>
/// <param name="containspage"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <param name="memostring"></param>
public static void avatar(page containspage, string filename, int r, int m, int s, int x, int y, bool save, string new_avatar)
{
system.drawing.image imagesrc = system.drawing.image.fromfile(httpcontext.current.server.mappath("/") + filename);
int w = imagesrc.width;
int h = imagesrc.height;

if (r == 1 || r == 3)
{
h = imagesrc.width;
w = imagesrc.height;
}

if (save)
{
w = h = 90;
}

if (w > 300) w = 300;
if (h > 300) h = 300;

color backcolor = color.darkturquoise;
size size = new size(w, h);

system.drawing.bitmap btnbmp = new bitmap(w, h);
graphics g = graphics.fromimage(btnbmp);

//重置背景颜色,可以自定义
g.clear(color.white);

color clr = backcolor;

g.smoothingmode = smoothingmode.antialias;//消除锯齿
g.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;

// 创建按钮图形-刷子
int btnoff = 0;//按钮边距
rectangle rc1 = new rectangle(btnoff, btnoff, size.width - 1 - btnoff, size.height - 1 - btnoff);
graphicspath gpath1 = getgraphicspath(rc1, 10);
graphicspath gpath1a = getgraphicspath(rc1, 15);
lineargradientbrush br1 = new lineargradientbrush(new point(0, 0), new point(0, rc1.height + 6), clr, color.white);

// 创建按钮阴影-刷子
int shadowoff = 1;//阴影边距
rectangle rc2 = rc1;
rc2.offset(0, shadowoff);
graphicspath gpath2 = getgraphicspath(rc2, 10);
pathgradientbrush br2 = new pathgradientbrush(gpath2);
br2.centercolor = color.fromargb(0, 0, 0); // color.black;
br2.surroundcolors = new color[] { color.fromargb(64, 64, 64, 64), color.fromargb(64, 128, 128, 128) }; // systemcolors.buttonface

// 为了更逼真,我们将渐变结束颜色设定为窗体前景色,可以根据窗口的前景颜色适当调整
// 创建按钮顶部白色渐变-刷子
rectangle rc3 = rc1;
rc3.inflate(-1, -1);
rc3.height = 15;
graphicspath gpath3 = getgraphicspath(rc3, 10);
lineargradientbrush br3 = new lineargradientbrush(rc3, color.fromargb(255, color.white), color.fromargb(0, color.white), lineargradientmode.vertical);

//绘制图形
//绘制阴影
if (s > 0)
{
g.fillpath(br2, gpath2);
}

//绘制按钮
if (s > 0)
{
g.fillpath(br1, gpath1);
}

//if (s > 0)
//{
g.setclip(gpath1a, combinemode.replace);
//}

switch (m)
{
case 1:
_currentbitmap = (bitmap)imagesrc;
rotateflip(rotatefliptype.rotatenoneflipx);
imagesrc = (system.drawing.image)_currentbitmap;
break;
case 2:
_currentbitmap = (bitmap)imagesrc;
rotateflip(rotatefliptype.rotatenoneflipy);
imagesrc = (system.drawing.image)_currentbitmap;
break;
default:
break;
}

switch (r)
{
case 1:
_currentbitmap = (bitmap)imagesrc;
rotateflip(rotatefliptype.rotate90flipnone);
imagesrc = (system.drawing.image)_currentbitmap;
break;
case 2:
_currentbitmap = (bitmap)imagesrc;
rotateflip(rotatefliptype.rotate180flipnone);
imagesrc = (system.drawing.image)_currentbitmap;
break;
case 3:
_currentbitmap = (bitmap)imagesrc;
rotateflip(rotatefliptype.rotate270flipnone);
imagesrc = (system.drawing.image)_currentbitmap;
break;
default:
break;
}

g.drawimage(imagesrc, -x, -y);

//绘制顶部白色泡泡
if (s > 0)
{
g.fillpath(br3, gpath3);
}

imagesrc.dispose();
g.dispose();

memorystream stream = new memorystream();
btnbmp.save(stream, system.drawing.imaging.imageformat.png);

if (save)
{
try
{
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
string sfile = httpcontext.current.server.mappath("/") + new_avatar;
btnbmp.save(sfile);
}
catch (system.exception e)
{
throw e;
}
}

//输出图片containspage
containspage.response.expires = 0;
containspage.response.expiresabsolute = system.datetime.now.addseconds(-1);
containspage.response.addheader("pragma", "no-cache");
containspage.response.addheader("cache-control", "private");
containspage.response.cachecontrol = "no-cache";
containspage.response.clear();
containspage.response.contenttype = "image/png";
containspage.response.binarywrite(stream.toarray());

}