实际上,做GDI+的不外乎两种目的:1纯粹兴趣,不为客户使用 2因客户使用而做。
看到几个开源的绘图源码,感觉到这一块不再孤独了,也来开源一个吧,这次开源的是DSCoolLabel,彩色文字类。因为本人没有受过专业英语教学,代码中单词可能有些乱用的情况,请谅解!先看效果图:
部分场合使用:当然,可能这些东西并不实用,纯粹是兴趣所做,勿以实用性来判断其技术含量,本身也无任何技术含量。源码如下:C#
[code=C#]using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Drawing;
public class DSCoolLabel
{
#region "属性"
private byte[] Glob_Byts;
private int bpStra;
public Color TextNomalColor { get; set; }
/// <summary>
/// 存储图像列表(最终输出为16X16像素)
/// </summary>
/// <res></res>
public List<Bitmap> ImgList = new List<Bitmap>();
/// <summary>
/// 绘制文字描边
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>
public bool DrawTextOutLine { get; set; }
/// <summary>
/// 文字间距
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>
public int CharWidth { get; set; }
/// <summary>
/// 文字行距
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>
public int CharLine { get; set; }
/// <summary>
/// 文本显示质量
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>
public Drawing.Text.TextRenderingHint TextRenderHint { get; set; }
private string _Text;
private List<ColorCharItem> _C = new List<ColorCharItem>();
private string _OutputText;
/// <summary>
/// 是否绘制投影
/// </summary>
/// <res></res>
public bool DrawShadow = false;
/// <summary>
/// 去除颜色代码后的字串
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>
public string OutputText {
get { return _OutputText; }
}
/// <summary>
/// 包含颜色代码的字串
/// </summary>
/// <value></value>
/// <returns></returns>
/// <res></res>

解决方案 »

  1.   

    public string Text {
    get { return _Text; }
    set {
    _Text = value;
    SizeWidth = 0;
    SizeHeight = 0;
    MakeOutBitmap();
    }
    }
    public void ClearImgList()
    {
    ImgList.Clear();
    }
    private struct ColorCharItem
    {
    public Color Color;
    public string CharString;
    public Font Font;
    }
    /// <summary>
    /// 字串所在矩形
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <res></res>
    public Rectangle TextRect { get; set; }
    /// <summary>
    /// 最终输出图像
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <res></res>
    public Bitmap OutBitmap { get; set; }
    /// <summary>
    /// 字体
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <res></res>
    public Font TextFont { get; set; }
    private StringFormat StringFormat { get; set; }
    private int SizeWidth;
    private int SizeHeight;
    private Size _TextBitmapSize;
    /// <summary>
    /// 结果图尺寸
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <res></res>
    public Size TextBitmapSize {
    get { return _TextBitmapSize; }
    }
    /// <summary>
    /// 文字描边颜色
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    /// <res></res>
    public Color TextOutLineColor { get; set; }
    /// <summary>
    /// 获取每个文字的大小
    /// </summary>
    /// <param name="T"></param>
    /// <returns></returns>
    /// <res></res>
    private PointF GetTextRegions(string T)
    {
    try {
    using (System.Windows.Forms.Control C = new System.Windows.Forms.Control()) {
    using (Graphics G = C.CreateGraphics()) {
    return G.MeasureString(T, TextFont, 0).ToPointF();
    }
    }
    } catch {
    return null;
    }
    } #endregion
    private void MakeOutBitmap()
    {
    try {
    int ImgHeightLine = 0;
    Size SSz = new Size(TextRect.Width, 0);
    int N = 1;
    LoadStrings();
    GetLargeRect();
    int K = 0;
    if (OutBitmap != null)
    OutBitmap.Dispose();
    OutBitmap = new Bitmap(TextRect.Width, _TextBitmapSize.Height + 10);
    using (Graphics G = Graphics.FromImage(OutBitmap)) {
    PointF Sz = default(PointF);
    ColorCharItem CC = new ColorCharItem();
    _OutputText = "";
    bool IsCharLineImgHeight = false;
    for (int II = _C.Count - 1; II >= 0; II += -1) {
    CC = (ColorCharItem)_C[II];
    _OutputText += CC.CharString;
    for (int I = 0; I <= CC.CharString.Length - 1; I++) {
    Sz = GetTextRegions("M");
    if (CC.CharString.Substring(I, 1) == Strings.Chr(13) || CC.CharString.Substring(I, 1) == Strings.Chr(10)) {
    SizeHeight += Sz.Y + CharLine + ImgHeightLine;
    SizeWidth = 0;
    ImgHeightLine = 0;
    } else {
    G.TextRenderingHint = TextRenderHint;
    if (I <= CC.CharString.Length - 5) {
    if (CC.CharString.Substring(I, 5) == "<IMG>") {
    try {
    int A = 0;
    int B = 0;
    A = ImgList[K].Width;
    B = ImgList[K].Height;
    if (K < ImgList.Count) {
      

  2.   

    if (TextRect.Width - SizeWidth - 2 - A < 0) {
    SizeWidth = 0;
    SizeHeight += ImgHeightLine;
    }
    G.DrawImage(ImgList[K], new Rectangle(SizeWidth + 2, SizeHeight, A, B), new Rectangle(0, 0, A, B), GraphicsUnit.Pixel);
    K += 1;
    ImgHeightLine = (B > ImgHeightLine ? B - Sz.Y : ImgHeightLine);
    SizeWidth += A + 2;
    I += 5;
    }
    } catch {
    }
    }
    }
    try {
    Sz = GetTextRegions(CC.CharString.Substring(I, 1));
    if (DrawTextOutLine == true) {
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth - 1, SizeHeight - 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth - 1, SizeHeight + 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth - 1, SizeHeight + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth + 1, SizeHeight - 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth, SizeHeight - 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth + 1, SizeHeight + 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth, SizeHeight + 1 + 4, Sz.X, Sz.Y), StringFormat);
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(TextOutLineColor), new Rectangle(SizeWidth + 1, SizeHeight + 4, Sz.X, Sz.Y), StringFormat);
    }
    //--
    G.DrawString(CC.CharString.Substring(I, 1), CC.Font, new SolidBrush(CC.Color), new Rectangle(SizeWidth, SizeHeight + 4, Sz.X, Sz.Y), StringFormat);
    SizeWidth += (Sz.X <= 4 ? 4 : Sz.X - 4) + CharWidth;
    if (SizeWidth + Sz.X >= TextRect.Width - 1) {
    N += 1;
    SizeHeight += Sz.Y + CharLine + ImgHeightLine;
    SizeWidth = 0;
    ImgHeightLine = 0;
    }
    } catch {
    }
    }
    }
    }
    SSz.Height = SizeHeight + CharLine * (N + 1) - (CharLine * N - Sz.Y);
    }
    OutBitmap = OutBitmap.Clone(new Rectangle(0, 0, SSz.Width, SSz.Height + 10), PixelFormat.Format32bppArgb);
    if (DrawShadow == true) {
    OutBitmap = SDrawShadow(OutBitmap);
    }
    } catch (Exception ex) {
    Console.WriteLine("DrawError:" + ex.Message);
    }
    }
    private void LoadStrings()
    {
    _C.Clear();
    try {
    int T = _Text.Length;
    for (int I = _Text.Length - 1; I >= 0; I += -1) {
    if (I <= _Text.Length - 13 && Text.Substring(I, 13) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 13).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2].Replace(">", ""));
    L.CharString = _Text.Substring(I + 13, T - I - 13).Replace(Strings.Chr(10), "");
    L.Font = TextFont;
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 15 && Text.Substring(I, 15) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 15, T - I - 15).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 15 && Text.Substring(I, 15) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 15, T - I - 15).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 15 && Text.Substring(I, 15) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 15, T - I - 15).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 15 && Text.Substring(I, 15) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 15, T - I - 15).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 17 && Text.Substring(I, 17) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 17, T - I - 17).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold + FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 17 && Text.Substring(I, 17) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 17, T - I - 17).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold + FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 17 && Text.Substring(I, 17) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 17, T - I - 17).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold + FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    } else if (I <= _Text.Length - 17 && Text.Substring(I, 17) // ERROR: Unknown binary operator Like
    ) {
    ColorCharItem L = new ColorCharItem();
    string[] SP = _Text.Substring(I, 12).Split(",");
    L.Color = Color.FromArgb(255, SP[0].Replace("<", ""), SP[1], SP[2]);
    L.CharString = _Text.Substring(I + 17, T - I - 17).Replace(Strings.Chr(10), "");
    L.Font = new Font(TextFont.Name, TextFont.Size, FontStyle.Bold + FontStyle.Underline, GraphicsUnit.Point);
    _C.Add(L);
    T = I;
    }
    }
    } catch {
    }
    }
    private void GetLargeRect()
    {
    Size R = default(Size);
    using (System.Windows.Forms.Control CT = new System.Windows.Forms.Control()) {
    using (Graphics G = CT.CreateGraphics()) {
    R = G.MeasureString(_Text, TextFont, TextRect.Width).ToSize();
    R = new Size(R.Width, R.Height + 1000);
    }
    }
    _TextBitmapSize = R;
    } private Bitmap tmpBMP;
    private Bitmap SDrawShadow(Bitmap B)
    {
    double PI = 3.14159265358;
    Bitmap tBs = new Bitmap(B.Width, B.Height);
    using (Graphics G = Graphics.FromImage(tBs)) {
    G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    using (Bitmap tB = new Bitmap(SetOpacity(B, 0.008))) {
    for (int J = 2; J <= 180; J += 10) {
    for (int I = 5; I >= 1; I += -1) {
    G.DrawImage(tB, new Rectangle(I * Math.Cos(J), I * Math.Sin(J), tB.Width, tB.Height), new Rectangle(0, 0, tB.Width, tB.Height), GraphicsUnit.Pixel);
    }
    }
    }
    G.DrawImage(B, new Point(0, 0));
    return tBs;
    }
    }
    private Bitmap SetOpacity(Bitmap B, double D)
    {
    try {
    BitmapData bmpDATA = new BitmapData();
    if (tmpBMP != null)
    tmpBMP.Dispose();
    tmpBMP = new Bitmap(B);
    Rectangle Rct = new Rectangle(0, 0, B.Width, B.Height);
    bmpDATA = tmpBMP.LockBits(Rct, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    byte[] BTS = new byte[bmpDATA.Stride * bmpDATA.Height + 1];
    System.Runtime.InteropServices.Marshal.Copy(bmpDATA.Scan0, BTS, 0, BTS.Length - 1);
    double T = 0;
    for (int I = 0; I <= BTS.Length - 4; I += 4) {
    BTS[I + 0] = 0;
    BTS[I + 1] = 0;
    BTS[I + 2] = 0;
    T = BTS[I + 3];
    T = T * D;
    BTS[I + 3] = T;
    }
    System.Runtime.InteropServices.Marshal.Copy(BTS, 0, bmpDATA.Scan0, BTS.Length - 1);
    tmpBMP.UnlockBits(bmpDATA);
    return tmpBMP;
    } catch {
    return null;
    }
    }
    }[/code]VB2010源工程下载:
    DSCoolLabel下载
      

  3.   

    包含色彩代码的文字段示例:
    http://static.sdg-china.com/woool/pic/woool_act/201110_school/items/gold_zhuan.gif|http://static.sdg-china.com/woool/pic/woool_act/201110_school/items/wcloth10.gif<<>><255,255,255,b>欢迎使用DS消息面板。<255,000,000,b>[群内公告]<255,255,000>本群征聘美女管理,有意者请到<000,255,000>群中NPC群主处<255,255,000>报名!并可领取虚拟
    <255,128,000><IMG>100000金币<255,255,000>
    <050,255,255><IMG>管理高级套装<255,255,000><255,000,000,b>[群内公告]<255,255,000>欢迎新朋友来到<000,255,000,b>电脑软件学习交流群<000,200,000,b>145428539<255,255,000>。<255,255,100,b>[群内福利]
    <255,000,000,b>QQ会员特权试用卡兑换码:
    <255,255,255>AEDVLAAEXvumtJnd
    AEDVLAAEXvnaHMSW
    AEDVLAAEZVrVMTQa
    AEDVLAAETVrBDjhd
    AEDVLAAEXvVDZpHE
    AEDVLAAEZVYdPmEy
    AEDVLAAERwwYZUst<255,000,000,b>[群内公告]<000,255,000>本群拟推出群成员等级功能,以开启DS消息面板时间长短来累加经验值。等级越高者,威望越高,将在新版DS消息面板成员排行榜列出。
    <000,255,255>更多群内福利不断更新!
    <255,000,000,b>[群内公告]<000,255,000>本群管理员请注意:为确保DS消息面板等本群扩展软件的正常使用,但凡加群成员是空昵称,管理员有权修改其群内昵称(至少一个字符以上)。
    <255,000,000,b>[版本更新]<255,255,255>应群内优秀成员建议,本次消息面板更新如下:
    1 支持面板最小化(最小化至屏幕顶部)。
    2 扩大面板缩放最大尺寸到800X600。
    3 插件栏渐显隐藏。
    4 面板支持位置及大小记忆。
    <255,255,000>最新版本消息面板请到群共享内下载。
      

  4.   

    晕你
    你就不能选择下[code=C#]吗?
      

  5.   

    private PointF GetTextRegions(string T)
    {
    try {
    using (System.Windows.Forms.Control C = new System.Windows.Forms.Control()) {
    using (Graphics G = C.CreateGraphics()) {
    return G.MeasureString(T, TextFont, 0).ToPointF();
    }
    }
    } catch {
    return null;
    }
    }值类型可以返回null? .net 2005报错啊
      

  6.   

    做的很漂亮啊,O(∩_∩)O谢谢LZ