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

 更新时间:2013年12月10日 16:14:32   作者:  
这篇文章主要介绍了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());

}

相关文章

  • C# 位运算符整理

    C# 位运算符整理

    在C#中可以对整型运算对象按位进行逻辑运算。按位进行逻辑运算的意义是:依次取被运算对象的每个位,进行逻辑运算,每个位的逻辑运算结果是结果值的每个位。
    2008-10-10
  • C#简单邮件群发通用类

    C#简单邮件群发通用类

    这篇文章主要为大家分享了简单的C#邮件群发通用类,代码简单实用,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • C#实现十五子游戏

    C#实现十五子游戏

    这篇文章主要为大家详细介绍了C#实现十五子游戏的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • c#语言入门类型和成员

    c#语言入门类型和成员

    这篇文章主要介绍了c#语言入门类型和成员,类 是最基本的 C# 类型。 类是一种数据结构,可在一个单元中就将状态和操作结合起来。 类为类实例提供了定义。类支持继承和多形性,即派生类 以扩展和专门针对基类的机制,下面来看看文章的详细介绍
    2021-12-12
  • 对C#中public、private、protect的区别说明

    对C#中public、private、protect的区别说明

    这篇文章主要介绍了对C#中public、private、protect的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#基础知识之字符串和正则表达式

    C#基础知识之字符串和正则表达式

    目前为止许多编程语言和工具都包含对正则表达式的支持,C#也不例外,下面这篇文章主要给大家介绍了关于C#基础知识之字符串和正则表达式的相关资料,需要的朋友可以参考下
    2022-10-10
  • C# WebApi Get请求方式传递实体参数的方法示例

    C# WebApi Get请求方式传递实体参数的方法示例

    这篇文章主要给大家介绍了关于C# WebApi Get请求方式传递实体参数的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • 详解C#读取Appconfig中自定义的节点

    详解C#读取Appconfig中自定义的节点

    我们往往需要在App.config中自定义一些节来满足实际需要,而不依赖于App.config的appSettings,下面通过一个简单的实例来说明自定义配置节点的设置与读取
    2015-06-06
  • C# 通过 inline-asm 解决嵌入x86汇编

    C# 通过 inline-asm 解决嵌入x86汇编

    此篇文章通过C#语言解决嵌入x86汇编,主要通过INline-asm方法来实现,下面我通过图片和代码的形式给大家分享下,需要的朋友可以参考下
    2015-07-07
  • c#判断输入的是不是数字的小例子

    c#判断输入的是不是数字的小例子

    c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
    2013-03-03

最新评论