Guest User

Updated Class

a guest
Sep 2nd, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.95 KB | None | 0 0
  1. using DSharpPlus.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.Drawing.Text;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Runtime.InteropServices.ComTypes;
  12. using System.Text;
  13.  
  14. namespace GBot.CardHandler
  15. {
  16.     public class CardHandle
  17.     {
  18.         public static Bitmap DrawLevelCard(string AvatarURL, ulong MemberID, string DisplayName, string Discriminator, int Level, double ExpNeeded, double CurrentExp, UserStatus Status, Color CustomColor, string BackgroundURL = null)
  19.         {
  20.  
  21.             Bitmap img = new Bitmap(935, 285);
  22.             if(BackgroundURL == null || BackgroundURL == "") {
  23.                 for (var x = 0; x < img.Width; x++)
  24.                 {
  25.                     for (var y = 0; y < img.Height; y++)
  26.                     {
  27.                         img.SetPixel(x, y, Color.FromArgb(255, 44, 47, 51));
  28.                     }
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 using (Graphics gr1 = Graphics.FromImage(img))
  34.                 {
  35.                     gr1.SmoothingMode = SmoothingMode.AntiAlias;
  36.                     gr1.InterpolationMode = InterpolationMode.High;
  37.                     gr1.DrawImage(GetBackgroundImage(BackgroundURL, ImageFormat.Png), new Point(0, 0));
  38.                 }
  39.             }
  40.             using (Graphics gr = Graphics.FromImage(img))
  41.             {
  42.                 gr.SmoothingMode = SmoothingMode.AntiAlias;
  43.                 gr.InterpolationMode = InterpolationMode.High;
  44.  
  45.                 GraphicsPath BackgroundCard = RoundedRect(new Rectangle(25, 35, 885, 215),10);
  46.                 GraphicsPath BackExpBar = RoundedRect(new Rectangle(261, 183, 632, 42),20);
  47.                 GraphicsPath FrontExpBar = RoundedRect(new Rectangle(262, 184, 630, 40),20);
  48.                 double ExpRatio = (CurrentExp / ExpNeeded);
  49.                 double widthOfExpBar = (630 * ExpRatio);
  50.                 Rectangle ExpBarRec = new Rectangle(262, 184, (int)widthOfExpBar, 40);
  51.                 GraphicsPath ExpBar = RoundedRect(ExpBarRec, 20);
  52.                 RectangleF rectName = new RectangleF(262, 130, 298, 55);
  53.                 RectangleF rectDiscriminator = new RectangleF(550, 130, 110, 54);
  54.                 if(BackgroundURL != null || BackgroundURL == "")
  55.                 {
  56.                     gr.FillPath(new SolidBrush(Color.FromArgb(185, 35, 39, 42)), BackgroundCard);
  57.                 }
  58.                 else
  59.                 {
  60.                     gr.FillPath(new SolidBrush(Color.FromArgb(100, 35, 39, 42)), BackgroundCard);
  61.                 }
  62.                 gr.FillPath(new SolidBrush(Color.FromArgb(255, 20, 20, 20)), BackExpBar);
  63.                 gr.FillPath(new SolidBrush(Color.FromArgb(255, 72, 75, 78)), FrontExpBar);
  64.                 if(CurrentExp != 0)
  65.                 {
  66.                     gr.FillPath(new SolidBrush(CustomColor), ExpBar);
  67.                 }
  68.                 gr.DrawImage(GetAvatarImage(AvatarURL, ImageFormat.Png), new Point(39, 57));
  69.                 //gr.FillEllipse(Brushes.Red, 39, 57, 173, 173);
  70.                 gr.FillEllipse(Brushes.Black, 164, 172, 51, 51);
  71.                 var getStatus = new Dictionary<string, Brush>(){
  72.                     { "Online", new SolidBrush(Color.FromArgb(255, 67, 181, 129))},
  73.                     { "Idle", new SolidBrush(Color.FromArgb(255, 250, 166, 26))},
  74.                     { "DoNotDisturb", new SolidBrush(Color.FromArgb(255, 240, 71, 71))},
  75.                     { "Offline", new SolidBrush(Color.FromArgb(255, 133, 144, 147))}
  76.                 };
  77.                 gr.FillEllipse(getStatus[Status.ToString()], 165, 173, 49, 49);
  78.  
  79.                 var measure = gr.MeasureString(DisplayName, GetFontName(36));
  80.                 int width = (int)measure.Width;
  81.                 int Beginheight = (int)measure.Height;
  82.                 int height = Beginheight;
  83.                 var measure2 = gr.MeasureString(Discriminator, GetFontName(24));
  84.                 int width2 = (int)measure2.Width;
  85.                 int Beginheight2 = (int)measure2.Height;
  86.                 int height2 = Beginheight2;
  87.                 var measure3 = gr.MeasureString(" / " + ExpNeeded + " Exp", GetFontName(16));
  88.                 int width3 = (int)measure3.Width;
  89.                 var measure4 = gr.MeasureString(CurrentExp.ToString(), GetFontName(16));
  90.                 int width4 = (int)measure4.Width;
  91.                 int i = 0;
  92.                 while (width + width2 >= 440)
  93.                 {
  94.                     i++;
  95.                     measure = gr.MeasureString(DisplayName, GetFontName(36-i));
  96.                     width = (int)measure.Width;
  97.                     height = (int)measure.Height;
  98.                     measure2 = gr.MeasureString(Discriminator, GetFontName(24-i));
  99.                     width2 = (int)measure2.Width;
  100.                     height2 = (int)measure2.Height;
  101.                 }
  102.                 gr.DrawString(DisplayName, GetFontName(36 - i), Brushes.White, 265, 125 + (Beginheight - height));
  103.                 gr.DrawString(Discriminator, GetFontName(24 - i), Brushes.Gray, 250 + (width), 141 + (Beginheight2 - height2));
  104.                 gr.DrawString(" / " + ExpNeeded + " Exp", GetFontName(16), new SolidBrush(Color.FromArgb(255, 72, 75, 78)), 262 + 630 - width3, 154);
  105.                 gr.DrawString(CurrentExp.ToString(), GetFontName(16), new SolidBrush(CustomColor), 262 + 630 - width3-width4, 154);
  106.             }
  107.  
  108.             img.Save(".\\Assets\\LevelCard_" + MemberID + ".png");
  109.             img.Dispose();
  110.  
  111.             return img;
  112.         }
  113.  
  114.         public static Bitmap DrawPointCard(string AvatarURL, ulong MemberID, string DisplayName, string Discriminator, int Points, DateTime DailyTime, int DailyCount, UserStatus Status, Color CustomColor, string BackgroundURL = null)
  115.         {
  116.             Font Namefont = GetFontName(36);
  117.             Font Discriminatorfont = GetFontName(24);
  118.  
  119.             Bitmap img = new Bitmap(935, 285);
  120.             if (BackgroundURL == null || BackgroundURL == "")
  121.             {
  122.                 for (var x = 0; x < img.Width; x++)
  123.                 {
  124.                     for (var y = 0; y < img.Height; y++)
  125.                     {
  126.                         img.SetPixel(x, y, Color.FromArgb(255, 44, 47, 51));
  127.                     }
  128.                 }
  129.             }
  130.             else
  131.             {
  132.                 using (Graphics gr1 = Graphics.FromImage(img))
  133.                 {
  134.                     gr1.SmoothingMode = SmoothingMode.AntiAlias;
  135.                     gr1.InterpolationMode = InterpolationMode.High;
  136.                     gr1.DrawImage(GetBackgroundImage(BackgroundURL, ImageFormat.Png), new Point(0, 0));
  137.                 }
  138.             }
  139.             using (Graphics gr = Graphics.FromImage(img))
  140.             {
  141.                 gr.SmoothingMode = SmoothingMode.AntiAlias;
  142.                 gr.InterpolationMode = InterpolationMode.High;
  143.                 var measure = gr.MeasureString(DisplayName, GetFontName(36));
  144.                 int width = (int)measure.Width;
  145.                 int Beginheight = (int)measure.Height;
  146.                 int height = Beginheight;
  147.                 var measure2 = gr.MeasureString(Discriminator, GetFontName(24));
  148.                 int width2 = (int)measure2.Width;
  149.                 int Beginheight2 = (int)measure2.Height;
  150.                 int height2 = Beginheight2;
  151.                 int i = 0;
  152.                 while (width + width2 >= 630)
  153.                 {
  154.                     i++;
  155.                     measure = gr.MeasureString(DisplayName, GetFontName(36 - i));
  156.                     width = (int)measure.Width;
  157.                     height = (int)measure.Height;
  158.                     measure2 = gr.MeasureString(Discriminator, GetFontName(24 - i));
  159.                     width2 = (int)measure2.Width;
  160.                     height2 = (int)measure2.Height;
  161.                 }
  162.  
  163.                 GraphicsPath BackgroundCard = RoundedRect(new Rectangle(25, 35, 885, 215),10);
  164.                 GraphicsPath BackUnderlineBar = RoundedRect(new Rectangle(261, 98, (width + width2) + 2, 12),5);
  165.                 GraphicsPath FrontUnderlineBar = RoundedRect(new Rectangle(262, 99, (width + width2), 10),5);
  166.                 RectangleF rectName = new RectangleF(262, 130, 298, 55);
  167.                 RectangleF rectDiscriminator = new RectangleF(550, 130, 110, 54);
  168.                 if (BackgroundURL != null || BackgroundURL == "")
  169.                 {
  170.                     gr.FillPath(new SolidBrush(Color.FromArgb(185, 35, 39, 42)), BackgroundCard);
  171.                 }
  172.                 else
  173.                 {
  174.                     gr.FillPath(new SolidBrush(Color.FromArgb(100, 35, 39, 42)), BackgroundCard);
  175.                 }
  176.                 gr.FillPath(new SolidBrush(Color.FromArgb(255, 20, 20, 20)), BackUnderlineBar);
  177.                 gr.FillPath(new SolidBrush((Color)CustomColor), FrontUnderlineBar);
  178.                 gr.DrawImage(GetAvatarImage(AvatarURL, ImageFormat.Png), new Point(39, 57));
  179.                 //gr.FillEllipse(Brushes.Red, 39, 57, 173, 173);
  180.                 gr.FillEllipse(Brushes.Black, 164, 172, 51, 51);
  181.                 var getStatus = new Dictionary<string, Brush>(){
  182.                     { "Online", new SolidBrush(Color.FromArgb(255, 67, 181, 129))},
  183.                     { "Idle", new SolidBrush(Color.FromArgb(255, 250, 166, 26))},
  184.                     { "DoNotDisturb", new SolidBrush(Color.FromArgb(255, 240, 71, 71))},
  185.                     { "Offline", new SolidBrush(Color.FromArgb(255, 133, 144, 147))}
  186.                 };
  187.                 gr.FillEllipse(getStatus[Status.ToString()], 165, 173, 49, 49);
  188.                 gr.DrawString(DisplayName, GetFontName(36 - i), Brushes.White, 265, 40 + (Beginheight - height));
  189.                 gr.DrawString(Discriminator, GetFontName(24 - i), Brushes.Gray, 250 + (width), 56 + (Beginheight2 - height2));
  190.             }
  191.  
  192.             img.Save(".\\Assets\\PointCard_" + MemberID + ".png");
  193.             img.Dispose();
  194.  
  195.             return img;
  196.         }
  197.  
  198.         private static Font GetFontName(float msSize)
  199.         {
  200.             PrivateFontCollection collection = new PrivateFontCollection();
  201.             collection.AddFontFile(@".\Fonts\Tommy Soft Regular.otf");
  202.             FontFamily fontFamily = new FontFamily("MADE Tommy Soft", collection);
  203.             collection.Dispose();
  204.             return new Font(fontFamily, msSize);
  205.         }
  206.  
  207.         public static Bitmap GetAvatarImage(string imageUrl, ImageFormat format)
  208.         {
  209.             WebClient client = new WebClient();
  210.             Stream stream = client.OpenRead(imageUrl);
  211.             Bitmap bitmap;
  212.             bitmap = new Bitmap(stream);
  213.  
  214.             stream.Flush();
  215.             stream.Close();
  216.             stream.Dispose();
  217.             client.Dispose();
  218.             return ResizeImage(bitmap, 173, 173);
  219.         }
  220.  
  221.         public static Bitmap GetBackgroundImage(string imageUrl, ImageFormat format)
  222.         {
  223.             WebClient client = new WebClient();
  224.             Stream stream = client.OpenRead(imageUrl);
  225.             Bitmap bitmap;
  226.             bitmap = new Bitmap(stream);
  227.  
  228.             stream.Flush();
  229.             stream.Close();
  230.             client.Dispose();
  231.             return ResizeImage(bitmap, 935, 285);
  232.         }
  233.  
  234.         public static Bitmap ResizeImage(Image image, int width, int height)
  235.         {
  236.             var destRect = new Rectangle(0, 0, width, height);
  237.             var destImage = new Bitmap(width, height);
  238.  
  239.             destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  240.  
  241.             using (var graphics = Graphics.FromImage(destImage))
  242.             {
  243.                 graphics.CompositingMode = CompositingMode.SourceCopy;
  244.                 graphics.CompositingQuality = CompositingQuality.HighQuality;
  245.                 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  246.                 graphics.SmoothingMode = SmoothingMode.HighQuality;
  247.                 graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  248.  
  249.                 using (var wrapMode = new ImageAttributes())
  250.                 {
  251.                     wrapMode.SetWrapMode(WrapMode.TileFlipXY);
  252.                     graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
  253.                 }
  254.             }
  255.  
  256.             return destImage;
  257.         }
  258.  
  259.         public static Bitmap ClipToCircle(Bitmap original, PointF center, float radius)
  260.         {
  261.             Bitmap copy = new Bitmap(original);
  262.             using (Graphics g = Graphics.FromImage(copy))
  263.             {
  264.                 RectangleF r = new RectangleF(center.X - radius, center.Y - radius, radius * 2, radius * 2);
  265.                 GraphicsPath path = new GraphicsPath();
  266.                 path.AddEllipse(r);
  267.                 g.Clip = new Region(path);
  268.                 g.DrawImage(original, 0, 0);
  269.                 return copy;
  270.             }
  271.         }
  272.         public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
  273.         {
  274.             int diameter = radius * 2;
  275.             Size size = new Size(diameter, diameter);
  276.             Rectangle arc = new Rectangle(bounds.Location, size);
  277.             GraphicsPath path = new GraphicsPath();
  278.            
  279.             if(bounds.Height >= bounds.Width)
  280.             {
  281.                 bounds.Width = bounds.Height;
  282.             }
  283.  
  284.             if (radius >= diameter) {
  285.                 path.AddRectangle(bounds);
  286.                 return path;
  287.             }
  288.  
  289.             // top left arc  
  290.             path.AddArc(arc, 180, 90);
  291.            
  292.             // top right arc  
  293.             arc.X = bounds.Right - diameter;
  294.             path.AddArc(arc, 270, 90);
  295.            
  296.             // bottom right arc  
  297.             arc.Y = bounds.Bottom - diameter;
  298.             path.AddArc(arc, 0, 90);
  299.            
  300.             // bottom left arc  
  301.             arc.X = bounds.Left;
  302.             path.AddArc(arc, 90, 90);
  303.             path.CloseFigure();
  304.             return path;
  305.         }
  306.     }
  307. }
  308.  
Advertisement
Add Comment
Please, Sign In to add comment