C automatically generates beautiful crystal effect avatar implementation code
- 2020-05-26 10:01:30
- OfStack
Like other microblogging systems, users of the duoke Intranet microblogging system can also upload their own avatars, which can be cropped.
However, the image of the "duokei Intranet weibo system" can be more beautiful, because the system realizes the crystal effect of the image.
The C# program implements the crystal effect avatar as follows:
(1) thumbnail image to width or height =90 head;
(2) the final avatar of 90x90 is cropped at the appropriate position selected by the user;
(3) add crystal effect;
Code dedication:
/// <summary>
/// Draw a crystal effect head
/// </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);
// Reset the background color , You can customize it
g.Clear(Color.White);
Color clr = backColor;
g.SmoothingMode = SmoothingMode.AntiAlias;// anti-alias
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// Create button graphic - brush
int btnOff = 0;// Button in the margin
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);
// Create button shadow - brush
int shadowOff = 1;// Shadow margin
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
// To make it more realistic, we set the end color of the gradient to the foreground color of the window, which can be adjusted according to the foreground color of the window
// Create a white gradient - brush at the top of the button
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);
// Drawing graphics
// Draw the shadow
if (s > 0)
{
g.FillPath(br2, gpath2);
}
// Draw button
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);
// Draw white bubbles on top
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
{
// Will this The original image Saves to the specified file in the specified format and with the specified encoding and decoding parameters
string sFile = HttpContext.Current.Server.MapPath("/") + new_avatar;
btnbmp.Save(sFile);
}
catch (System.Exception e)
{
throw e;
}
}
// The output image 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());
}