C # automatically generate beautiful crystal effect portraits
Similar to other Weibo systems,
However, the avatar of "more intranets and microblogs" can be more beautiful, because the system achieves the crystal effect.
C # The program implements the crystal effect Avatar process:
(1) scale down an image to a portrait with a width or height of 90;
(2) The user selects a suitable position to crop the final portrait of 90x90;
(3) Add the crystal effect;
Code dedication:
/// <Summary>
/// Create an avatar for the crystal Effect
/// </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 & gt; 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, which can be customized
G. Clear (Color. White );
Color clr = backColor;
G. SmoothingMode = SmoothingMode. AntiAlias; // eliminate the Sawtooth
G. CompositingQuality = System. Drawing. Drawing2D. CompositingQuality. HighQuality;
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
// Create button image-brush
Int btnOff = 0; // button 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 a 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), Color. FromArgb (64,128,128,128)}; // SystemColors. ButtonFace
// To be more realistic, we set the gradient end color to the foreground color of the form. You can adjust the color according to the foreground color of the window.
// Create a white gradient-brush on 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 );
// Draw the image
// 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 the top white bubble
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
{
// Save the original image in the specified format and use the specified codec parameter to the specified file
String sFile = HttpContext. Current. Server. MapPath ("/") + new_avatar;
Btnbmp. Save (sFile );
}
Catch (System. Exception e)
{
Throw e;
}
}
// 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 ());
}