/// <summary>
        /// 对图片添加文字水印
        /// </summary>
        /// <param name="sourcefile">图片源路径</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="desfile">图片目的路径</param>
        public static Stream AddImgWaterMark(string sourcefile, string watermarkText)
        {
            try
            {
                Stream stream            = new MemoryStream(System.IO.File.ReadAllBytes(sourcefile));
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                //System.Drawing.Image img = System.Drawing.Image.FromFile(sourcefile);
                Graphics            gr = Graphics.FromImage(img);
                System.Drawing.Font font = new System.Drawing.Font("Tahoma", (float)40, FontStyle.Bold, GraphicsUnit.Pixel);
                Color  color = Color.FromArgb(20, Color.Gray);//241, 235, 105 Color.Silver
                double tangent = (double)img.Height / (double)img.Width;
                double angle = Math.Atan(tangent) * (180 / Math.PI);
                double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width)) / 2;
                double sin, cos, opp1, adj1, opp2, adj2;

                for (int i = 100; i > 0; i--)
                {
                    font = new System.Drawing.Font("Tahoma", i, FontStyle.Bold);
                    SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue);

                    sin  = Math.Sin(angle * (Math.PI / 180));
                    cos  = Math.Cos(angle * (Math.PI / 180));
                    opp1 = sin * sizef.Width;
                    adj1 = cos * sizef.Height;
                    opp2 = sin * sizef.Height;
                    adj2 = cos * sizef.Width;

                    if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width)
                    {
                        break;
                    }
                }
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment     = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.RotateTransform((float)angle);
                gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat);

                try
                {
                    return(img.ToStream(ImageFormat.Jpeg));
                }
                finally
                {
                    if (img != null)
                    {
                        img.Dispose();
                        img = null;
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog(string.Format("{0},{1}", ex.Message, ex.StackTrace));
            }
            return(new MemoryStream(System.IO.File.ReadAllBytes(sourcefile)));
        }
 public void UploadImage(Image image, string blobLocation)
 {
     using (var stream = image.ToStream(ImageFormat.Jpeg))
     {
         var blob = _container.GetBlockBlobReference(blobLocation);
         blob.UploadFromStream(stream);
     }
 }
Example #3
0
 private async Task SendImageResultAsync(Image image)
 {
     await Context.Channel.SendFileAsync(
         image.ToStream(ImageFormat.Png), $"Loadout{Context.User.Username}.png",
         $"パイロット {Context.User.Username} の推奨装備です。");
 }