C#关于GDI+绘图(三)

来源:互联网 发布:天马网络分销平台 编辑:程序博客网 时间:2024/04/29 18:01

一、创建复杂文字效果

    使用GDI+不仅可以创建各种各样的图形,还可以创建文字的显示。与窗体控件中的Label控件不同,GDI+是通过绘制函数将文字绘制到窗体中的。在GDI+中创建文本的主要方法是使用DrawString方法,并定义要绘制文本的字体即可。

在上一节代码中加入如下语句即可:

TextBox t;

case  Style.Text:

       t=new TextBox();

       t.KeyPress +=new KeyPressEventHandler(t_KeyPress);

       t.Top=p2.Y;

       t.Left=p2.X;

       splitContainer1.panel2.Controls.Add(t);

       this.Refresh();

       break;

以及:

///<summary>

///主要用于处理在输入文本后按下回车键

///</summary>

void t_KeyPress(object sender,KeyPressEventArgs e)

{

     g=splitContainer1.Panel2.CreateGraphics();

     if(e.KeyChar.ToString() == "\r")

     {

         splitContainer1.Panel2.Controls.Remove(t);

         g.DrawString(t.Text,new Font("宋体",24),new SolidBrush(color),p2);

     }

}


二、绘制图片文件

  常见的绘图程序都提供了读取图片文件并进行显示的功能,绘图板也可以进行图片的读取和显示。在C#中,使用GDI+中的Bitmap类即可表示大多数图片文件,然后通过Graphic类的DrawImage方法即可显示该图片文件。

继续上一节创建的项目,添加按钮控件和打开文件对话框控件,编写“图片”按钮的单击事件代码如下:

///<summary>

///图片

///</summary>

private void button9_Click(object sender,EventArgs e)

{

    openFileDialog.InitialDirectory = @"C:\";

    openFileDialog.Filter = "Jpeg文件|*.jpg";

    if(openFileDialog.ShowDialog() == DialogResult.OK)

     {

          g=splitContainer1.Panel2.CreateGraphics();

          Bitmap  b=new Bitmap(openFileDialog.FileName);

          g.DrawImage(b,0,0);

     }

}

0 0
原创粉丝点击