当前位置: 首页>>代码示例>>C#>>正文


C# Bitmap.ToStream方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.ToStream方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.ToStream方法的具体用法?C# Bitmap.ToStream怎么用?C# Bitmap.ToStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了Bitmap.ToStream方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ToUrl

        public static string ToUrl(this ImageFile file, int width = 0)
        {
            if (width > 0)
            {
                var fileName = GetFileName(file, width);

                if (File.Exists(FileHelper.GetPath(fileName)))
                {
                    return ImageHelper.GetUrl(fileName);
                }

                try
                {
                    var source = FileHelper.GetPath(file.FileName);
                    var img = Image.FromFile(source);

                    if (img.Width > width)
                    {
                        var bitmap = new Bitmap(width, GetTargetHeight(img.Width, width, img.Height));
                        var g = Graphics.FromImage(bitmap);

                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.DrawImage(img, 0, 0, width, GetTargetHeight(img.Width, width, img.Height));
                        g.Dispose();

                        FileHelper.SaveToFile(bitmap.ToStream(ImageFormat.Jpeg), fileName);

                        return ImageHelper.GetUrl(fileName);
                    }
                }
                catch { }
            }

            return ImageHelper.GetUrl(file.FileName);
        }
开发者ID:nescalante,项目名称:fulbaso-api,代码行数:35,代码来源:ImageHelper.cs

示例2: Install


//.........这里部分代码省略.........

                cgb.CreateCommand(Prefix + "hs")
                  .Description("Searches for a Hearthstone card and shows its image. Takes a while to complete.\n**Usage**:~hs Ysera")
                  .Parameter("name", ParameterType.Unparsed)
                  .Do(async e =>
                  {
                      var arg = e.GetArg("name");
                      if (string.IsNullOrWhiteSpace(arg))
                      {
                          await e.Channel.SendMessage("💢 Please enter a card name to search for.").ConfigureAwait(false);
                          return;
                      }
                      await e.Channel.SendIsTyping().ConfigureAwait(false);
                      var headers = new Dictionary<string, string> { { "X-Mashape-Key", NadekoBot.Creds.MashapeKey } };
                      var res = await SearchHelper.GetResponseStringAsync($"https://omgvamp-hearthstone-v1.p.mashape.com/cards/search/{Uri.EscapeUriString(arg)}", headers)
                                                  .ConfigureAwait(false);
                      try
                      {
                          var items = JArray.Parse(res);
                          var images = new List<Image>();
                          if (items == null)
                              throw new KeyNotFoundException("Cannot find a card by that name");
                          var cnt = 0;
                          items.Shuffle();
                          foreach (var item in items.TakeWhile(item => cnt++ < 4).Where(item => item.HasValues && item["img"] != null))
                          {
                              images.Add(
                                  Image.FromStream(await SearchHelper.GetResponseStreamAsync(item["img"].ToString()).ConfigureAwait(false)));
                          }
                          if (items.Count > 4)
                          {
                              await e.Channel.SendMessage("⚠ Found over 4 images. Showing random 4.").ConfigureAwait(false);
                          }
                          await e.Channel.SendFile(arg + ".png", (await images.MergeAsync()).ToStream(System.Drawing.Imaging.ImageFormat.Png))
                                         .ConfigureAwait(false);
                      }
                      catch (Exception ex)
                      {
                          await e.Channel.SendMessage($"💢 Error {ex.Message}").ConfigureAwait(false);
                      }
                  });

                cgb.CreateCommand(Prefix + "osu")
                  .Description("Shows osu stats for a player.\n**Usage**:~osu Name")
                  .Parameter("usr", ParameterType.Unparsed)
                  .Do(async e =>
                  {
                      if (string.IsNullOrWhiteSpace(e.GetArg("usr")))
                          return;

                      using (WebClient cl = new WebClient())
                      {
                          try
                          {
                              cl.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                              cl.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.2; Win64; x64)");
                              cl.DownloadDataAsync(new Uri($"http://lemmmy.pw/osusig/sig.php?uname={ e.GetArg("usr") }&flagshadow&xpbar&xpbarhex&pp=2"));
                              cl.DownloadDataCompleted += async (s, cle) =>
                              {
                                  try
                                  {
                                      await e.Channel.SendFile($"{e.GetArg("usr")}.png", new MemoryStream(cle.Result)).ConfigureAwait(false);
                                      await e.Channel.SendMessage($"`Profile Link:`https://osu.ppy.sh/u/{Uri.EscapeDataString(e.GetArg("usr"))}\n`Image provided by https://lemmmy.pw/osusig`").ConfigureAwait(false);
                                  }
                                  catch { }
                              };
开发者ID:ZirconiumHacker,项目名称:NadekoBot,代码行数:67,代码来源:SearchesModule.cs

示例3: ResizeImage

        public static Stream ResizeImage(this Image imgToResize, Size size, ImageFormat imageFormat, bool crop)
        {
            if (!crop)
            {
            var sourceWidth = imgToResize.Width;
            var sourceHeight = imgToResize.Height;

            float nPercent;
            var nPercentW = (size.Width / (float)sourceWidth);
            var nPercentH = (size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            var destWidth = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);
            var testHeight = (int)destHeight;

            var b = new Bitmap(destWidth, destHeight);
            var g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, testHeight);
            g.Dispose();

            return b.ToStream(imageFormat);

            }else
            {
                var sourceWidth = imgToResize.Width;
                var sourceHeight = imgToResize.Height;

                float nPercent;
                var nPercentW = (size.Width / (float)sourceWidth);
                var nPercentH = (size.Height / (float)sourceHeight);
                var hStart = 0;
                var vStart = 0;

                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentW;
                    vStart = (int)(((float)sourceHeight / 2)*nPercent - (float)size.Height / 2 );
                }
                else
                {
                    nPercent = nPercentH;
                    hStart =(int)((((float)sourceWidth / 2)*nPercent) - (float)size.Width / 2 );
                }

                var scaleWidth = (int)(sourceWidth * nPercent);
                var scaleHeight = (int)(sourceHeight * nPercent);

                var b = new Bitmap(size.Width, size.Height);
                var g = Graphics.FromImage(b);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(imgToResize, -hStart, -vStart, scaleWidth, scaleHeight);
                g.Dispose();

                return b.ToStream(imageFormat);
            }
        }
开发者ID:slashP,项目名称:CIBER-NDC,代码行数:64,代码来源:ImageUtil.cs

示例4: ResizeImage

        public static Stream ResizeImage(Image imgToResize, Size size, ImageFormat imageFormat)
        {
            var sourceWidth = imgToResize.Width;
            var sourceHeight = imgToResize.Height;

            float nPercent;
            var nPercentW = (size.Width / (float)sourceWidth);
            var nPercentH = (size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            var destWidth = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);

            var b = new Bitmap(destWidth, destHeight);
            var g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return b.ToStream(imageFormat);
        }
开发者ID:slashP,项目名称:Solbakken,代码行数:26,代码来源:ImageUtil.cs


注:本文中的System.Drawing.Bitmap.ToStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。