返回顶部

收藏

C# string生成图片

更多
public static Bitmap GetLink(string Wordstr)
        {
            #region older
            //arial
            //StrForImg sf = new StrForImg();
            //sf.Adaptable = false;

            //sf.BackgroundImage = "";
            //sf.BgColor = Color.White;
            //sf.FontFamily = "arial";
            //sf.FontSize = 10;
            //sf.FontStyle = FontStyle.Regular;
            //sf.Height = 15;
            //sf.Text = Wordstr;
            //sf.Width = Wordstr.Length * 10;
            //sf.ResultImage = "c:\\\\a.bmp";
            //sf.Top = 0;
            //sf.Left = 0;

            //Color bl = Color.Green;

            //sf.Alpha = bl.A;
            //sf.Red = bl.R;
            //sf.Green = bl.G;
            //sf.Blue = bl.B;
            //try
            //{
            //    return sf.Create();
            //}
            //catch { return null; }
            #endregion

            try
            {
                Size TextSize = TextRenderer.MeasureText(Wordstr, new Font(new FontFamily("Arial"), 10, FontStyle.Regular));
                Bitmap b = StrForImg_New.DrawTextBmp(Wordstr, new Font(new FontFamily("Arial"), 10, FontStyle.Regular), Color.FromArgb(0,128,0), TextSize, 0, 0, 0, 0);
                b = StrForImg_New.ClearWhite(b);
                return b;
            }
            catch { return null; }
        }

public static Bitmap DrawTextBmp(string ch, Font font, Color color, Size TextSize, int x, int y, int w, int h)
        {
            //创建此大小的图片
            Bitmap bmp = new Bitmap(TextSize.Width - x, TextSize.Height - y);
            //使用GDI+绘制
            Graphics g = Graphics.FromImage(bmp);
            bmp = new Bitmap(TextSize.Width - x, TextSize.Height - y, PixelFormat.Format64bppArgb);
            g = Graphics.FromImage(bmp);
            g.Clear(Color.White);

            g.DrawString(ch, font, new SolidBrush(color), new PointF(w, h));
            g.Save();
            g.Dispose();
            //返回图像
            return bmp;
        }
//去白边
        public static Bitmap ClearWhite(Bitmap bm)
        {
            int y_l = 0;//左边
            int y_r = 0;//右边
            int i_h = 0;//上边
            int i_d = 0;//下边
            #region 计算----
            for (int i = 0; i < bm.Width; i++)
            {
                for(int y=0;y<bm.Height;y++)
                {
                    if (bm.GetPixel(i, y).R != 255 || bm.GetPixel(i, y).B != 255 || bm.GetPixel(i, y).G != 255)
                    {
                        y_l = i;
                        goto yl;
                    }
                }
            }
            yl:
            for (int i = 0; i < bm.Width; i++)
            {
                for (int y = 0; y < bm.Height; y++)
                {
                    if (bm.GetPixel(bm.Width - i - 1, y).R != 255 || bm.GetPixel(bm.Width - i - 1, y).B != 255 || bm.GetPixel(bm.Width - i - 1, y).G != 255)
                    {
                        y_r = i;
                        goto yr;
                    }
                }
            }
            yr:
            for (int i = 0; i < bm.Height; i++)
            {
                for (int y = 0; y < bm.Width; y++)
                {
                    if (bm.GetPixel(y, i).R != 255 || bm.GetPixel(y, i).B != 255 || bm.GetPixel(y, i).G != 255)
                    {
                        i_h = i;
                        goto ih;
                    }
                }
            }
            ih:
            for (int i = 0; i < bm.Height; i++)
            {
                for (int y = 0; y < bm.Width; y++)
                {
                    if (bm.GetPixel(y, bm.Height - i - 1).R != 255 || bm.GetPixel(y, bm.Height - i - 1).B != 255 || bm.GetPixel(y, bm.Height - i - 1).G != 255)
                    {
                         i_d = i;
                         goto id;
                    }
                }
            }
        id:
            #endregion

            //创建此大小的图片
            Bitmap bmp = new Bitmap(bm.Width - y_l - y_r, bm.Height - i_h - i_d);
            Graphics g = Graphics.FromImage(bmp);
            //(new Point(y_l, i_h), new Point(0, 0), new Size(bm.Width - y_l - y_r, bm.Height - i_h - i_d));
            Rectangle sourceRectangle = new Rectangle(y_l, i_h, bm.Width - y_l - y_r, bm.Height - i_h - i_d);
            Rectangle resultRectangle = new Rectangle(0, 0, bm.Width - y_l - y_r, bm.Height - i_h - i_d);
            g.DrawImage(bm, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
            return bmp;
        }
//该片段来自于http://outofmemory.cn

标签:c#,.net,图形

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 汪宇杰 发表 2015-04-22 07:08:56 如何高逼格读取Web.config中的AppSettings
  2. 姚 广远 发表 2015-06-22 07:01:45 C++ 中的封送处理概述
  3. 姚 广远 发表 2015-04-15 05:20:48 C#语言规范—语句
  4. 姚 广远 发表 2015-04-14 06:17:41 C# 语言规范 —类型、变量和值
  5. OWenT 发表 2017-02-15 15:27:51 atapp的c binding和c#适配
  6. IT路人 发表 2013-07-05 05:00:00 利用C# .net在WP8里面异步加载任务
  7. 姚 广远 发表 2015-04-13 05:03:47 .NET运行机制
  8. itcodes 发表 2014-07-22 17:00:00 C#计算时间差。
  9. 姚 广远 发表 2015-04-13 02:54:10 C# 与 C++ 数据类型对照表
  10. Herb Sutter 发表 2012-04-07 06:13:01 We want await! A C# talk that’s applicable to C++
  11. 汪宇杰 发表 2015-04-22 07:08:56 如何高逼格读取Web.config中的AppSettings
  12. Herb Sutter 发表 2012-04-13 01:23:12 Talk + panel online: “(Not Your Father’s) C++” + “

发表评论