当前位置:编程学习 > C#/ASP.NET >>

C# , GDI+ 能不能画出,一个段落内,含有多种字体呢?

如题 --------------------编程问答-------------------- 要是自己画,应该是随心所欲啊! --------------------编程问答-------------------- 不太好办,画了第一段文字,很难计算出,第二段文字的开始位置。
并且,英文单词还要换行问题,复杂啊 --------------------编程问答-------------------- --------------------编程问答--------------------

//投影文字
                    Graphics g = this.CreateGraphics();
                    //设置文本输出质量
                    g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    Font newFont = new Font("Times New Roman", 48);
                    Matrix matrix = new Matrix();
                    //投射
                    matrix.Shear(-1.5f, 0.0f);
                    //缩放
                    matrix.Scale(1, 0.5f);
                    //平移
                    matrix.Translate(130, 88);
                    //对绘图平面实施坐标变换、、
                    g.Transform = matrix;
                    SolidBrush grayBrush = new SolidBrush(Color.Gray);
                    SolidBrush colorBrush = new SolidBrush(Color.BlueViolet);

                    //绘制阴影
                    g.DrawString(text, newFont, grayBrush, new PointF(0, 30));
                    g.ResetTransform();
                    //绘制前景
                    g.DrawString(text, newFont, colorBrush, new PointF(0, 30)); ;



//浮雕文字
                    Brush backBrush = Brushes.Black;
                    Brush foreBrush = Brushes.White;
                    Font font = new Font("宋体", Convert.ToInt16(40), FontStyle.Regular);
                    g = this.CreateGraphics();
                    SizeF size = g.MeasureString(text, font);
                    Single posX = (this.Width - Convert.ToInt16(size.Width)) / 2;
                    Single posY = (this.Height - Convert.ToInt16(size.Height)) / 2;
                    g.DrawString(text, font, backBrush, posX + 1, posY + 1);
                    g.DrawString(text, font, foreBrush, posX, posY);


 int i = 0;
                    backBrush = Brushes.Black;
                    foreBrush = Brushes.Violet;
                    font = new Font("Times New Roman", System.Convert.ToInt16(40), FontStyle.Regular);
                    g = this.CreateGraphics();
                    g.Clear(Color.White);
                    size = g.MeasureString(text, font);
                    posX = (this.Width - Convert.ToInt16(size.Width)) / 2;
                    posY = (this.Height - Convert.ToInt16(size.Height)) / 3;
                    while (i < Convert.ToInt16(20))
                    {
                        g.DrawString(text, font, backBrush, posX - i, posY + i);
                        i = i + 1;
                    }
                    g.DrawString(text, font, foreBrush, posX, posY);



//倒影文字
                    backBrush = Brushes.Gray;
                    foreBrush = Brushes.Black;
                    font = new Font("幼圆", Convert.ToInt16(40), FontStyle.Regular);
                    g = this.CreateGraphics();
                    size = g.MeasureString(text, font);
                    posX = (this.Width - Convert.ToInt16(size.Width)) / 2;
                    posY = (this.Height - Convert.ToInt16(size.Height)) / 2;
                    g.TranslateTransform(posX, posY);
                    int ascent = font.FontFamily.GetCellAscent(font.Style);
                    int spacing = font.FontFamily.GetLineSpacing(font.Style);
                    int lineHeight = System.Convert.ToInt16(font.GetHeight(g));
                    int height = lineHeight * ascent / spacing;
                    GraphicsState state = g.Save();
                    g.ScaleTransform(1, -1.0F);
                    g.DrawString(text, font, backBrush, 0, -height);
                    g.Restore(state);
                    g.DrawString(text, font, foreBrush, 0, -height);

--------------------编程问答--------------------
都不是我需要的,我要的是这种效果。

一个段落内,含有多种字体 --------------------编程问答-------------------- 可以,不过速度会很慢,因为需要不断地计算测量不同文字样式时的尺寸,还要不断地计算折行....建议直接使用RichTextBox或者Webbrowser --------------------编程问答-------------------- 可是,我这个是画图用的。
画图过程中需要画文字,段落。 --------------------编程问答-------------------- 编辑文字时在Richtextbox内编辑,失去焦点时直接RichTextBox1.DrawToBitmap,当然最好是先maketransparent.相当于把RICHTEXTBOX挖空再画到底图上. --------------------编程问答-------------------- C#里,找不到RichTextBox1.DrawToBitmap --------------------编程问答-------------------- 找不到可以手工写 --------------------编程问答-------------------- 字体被定义成了一个个的像素点,字体的确切大小取决于显示器的大小
MeasureString 和MeasureCharacterRanges   
字体样式
改变字体
Font f=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
string str="";
if (richTextBox1.Find(str)>0)
{
int pos=richTextBox1.Find(str);
richTextBox1.SelectionStart=pos;
richTextBox1.SelectionLength=str.Length;   
richTextBox1.SelectionFont=f;
richTextBox1.SelectionColor=Color.Red;
}   
http://www.codeproject.com/KB/static/BorderLabel.aspx --------------------编程问答-------------------- public int PrintToImage(int charFrom, int charTo, Rectangle border, Rectangle pageSize, Graphics g)

这个方法,总是从Graphics的左上方x,y 都为0开始画。
找不到控制X,Y坐标的参数。


        public int PrintToImage(int charFrom, int charTo, Rectangle border, Rectangle pageSize, Graphics g, int x,int y)
        {
            //Calculate the area to render and print
            RECT rectToPrint;
            rectToPrint.Top = (int)(pageSize.Top * anInch);
            rectToPrint.Bottom = (int)(pageSize.Bottom * anInch);
            rectToPrint.Left = (int)(pageSize.Left * anInch);
            rectToPrint.Right = (int)(pageSize.Right * anInch);

            //Calculate the size of the page
            RECT rectPage;
            rectPage.Top = (int)(pageSize.Top * anInch);
            rectPage.Bottom = (int)(pageSize.Bottom * anInch);
            rectPage.Left = (int)(pageSize.Left * anInch);
            rectPage.Right = (int)(pageSize.Right * anInch);

            IntPtr hdc = g.GetHdc();

            FORMATRANGE fmtRange;
            fmtRange.chrg.cpMax = charTo;                //Indicate character from to character to 
            fmtRange.chrg.cpMin = charFrom;
            fmtRange.hdc = hdc;                    //Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc;              //Point at printer hDC
            fmtRange.rc = rectToPrint;             //Indicate the area on page to print
            fmtRange.rcPage = rectPage;            //Indicate size of page 

            IntPtr res = IntPtr.Zero;

            IntPtr wparam = IntPtr.Zero;
            wparam = new IntPtr(1);

            //Get the pointer to the FORMATRANGE structure in memory
            IntPtr lparam = IntPtr.Zero;
            lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            Marshal.StructureToPtr(fmtRange, lparam, false);

            //Send the rendered data for printing 
            res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);

            //Free the block of memory allocated
            Marshal.FreeCoTaskMem(lparam);

            //Release the device context handle obtained by a previous call
            g.ReleaseHdc(hdc);

            //Return last + 1 character printer
            return res.ToInt32();
        } 
--------------------编程问答-------------------- 当然了。。。 --------------------编程问答-------------------- 那怎样控制RichTextBox的内容,出现在Graphics上的位置呢
它总是x,y为0
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,