显示图片和文字
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7![]()
8
namespace DrawPictureString
9
{
10
/// <summary>
11
/// 显示图片和文字。
12
/// </summary>
13
public class Form1 : System.Windows.Forms.Form
14
{
15
private System.Windows.Forms.Button button1;
16
private System.Windows.Forms.OpenFileDialog openFileDialog1;
17
/// <summary>
18
/// 必需的设计器变量。
19
/// </summary>
20
private System.ComponentModel.Container components = null;
21![]()
22
public Form1()
23
{
24
// Windows 窗体设计器支持所必需的
25
InitializeComponent();
26
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
27
}
28![]()
29
/// <summary>
30
/// 清理所有正在使用的资源。
31
/// </summary>
32
protected override void Dispose( bool disposing )
33
{
34
if( disposing )
35
{
36
if (components != null)
37
{
38
components.Dispose();
39
}
40
}
41
base.Dispose( disposing );
42
}
43![]()
44
Windows Form Designer generated code
80![]()
81
/// <summary>
82
/// 应用程序的主入口点。
83
/// </summary>
84
[STAThread]
85
static void Main()
86
{
87
Application.Run(new Form1());
88
}
89
public string m_FileName;
90
public Image m_Image;
91
public bool m_Init = false;
92
// 打开图片文件。
93
private void button1_Click(object sender, System.EventArgs e)
94
{
95
openFileDialog1.Filter = "图形文件(*.bmp;*.jpg;*.jpeg)|*.bmp;*.jpg;*.jpeg";
96
openFileDialog1.FilterIndex = 1;
97
if(openFileDialog1.ShowDialog() == DialogResult.OK)
98
{
99
m_FileName = openFileDialog1.FileName;
100
m_Image = Image.FromFile(openFileDialog1.FileName);
101
m_Init = true;
102
this.Validate();
103
}
104
}
105
// 处理窗体显示事件。
106
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
107
{
108
if(m_Init)
109
{
110
Rectangle rect = new Rectangle(0,0,this.ClientRectangle.Width,
111
this.ClientRectangle.Height-button1.Height-20);
112
Graphics g = e.Graphics;
113
// 显示图片。
114
g.DrawImage(m_Image, rect);
115
// 显示图片对应文件名信息。
116
SolidBrush redBrush = new SolidBrush(Color.Red);
117
g.DrawString(m_FileName, new Font("宋体", 10), redBrush,
118
new Point(0,this.ClientRectangle.Height-30), StringFormat.GenericDefault);
119
}
120
}
121
}
122
}
123![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7

8
namespace DrawPictureString9
{10
/// <summary>11
/// 显示图片和文字。12
/// </summary>13
public class Form1 : System.Windows.Forms.Form14
{15
private System.Windows.Forms.Button button1;16
private System.Windows.Forms.OpenFileDialog openFileDialog1;17
/// <summary>18
/// 必需的设计器变量。19
/// </summary>20
private System.ComponentModel.Container components = null;21

22
public Form1()23
{24
// Windows 窗体设计器支持所必需的25
InitializeComponent();26
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码27
}28

29
/// <summary>30
/// 清理所有正在使用的资源。31
/// </summary>32
protected override void Dispose( bool disposing )33
{34
if( disposing )35
{36
if (components != null) 37
{38
components.Dispose();39
}40
}41
base.Dispose( disposing );42
}43

44
Windows Form Designer generated code80

81
/// <summary>82
/// 应用程序的主入口点。83
/// </summary>84
[STAThread]85
static void Main() 86
{87
Application.Run(new Form1());88
}89
public string m_FileName;90
public Image m_Image;91
public bool m_Init = false;92
// 打开图片文件。93
private void button1_Click(object sender, System.EventArgs e)94
{95
openFileDialog1.Filter = "图形文件(*.bmp;*.jpg;*.jpeg)|*.bmp;*.jpg;*.jpeg";96
openFileDialog1.FilterIndex = 1;97
if(openFileDialog1.ShowDialog() == DialogResult.OK)98
{99
m_FileName = openFileDialog1.FileName;100
m_Image = Image.FromFile(openFileDialog1.FileName);101
m_Init = true;102
this.Validate();103
}104
}105
// 处理窗体显示事件。106
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)107
{108
if(m_Init)109
{110
Rectangle rect = new Rectangle(0,0,this.ClientRectangle.Width,111
this.ClientRectangle.Height-button1.Height-20);112
Graphics g = e.Graphics;113
// 显示图片。114
g.DrawImage(m_Image, rect);115
// 显示图片对应文件名信息。116
SolidBrush redBrush = new SolidBrush(Color.Red);117
g.DrawString(m_FileName, new Font("宋体", 10), redBrush,118
new Point(0,this.ClientRectangle.Height-30), StringFormat.GenericDefault);119
}120
}121
}122
}123



浙公网安备 33010602011771号