MainForm.cs
上传用户:linger1010
上传日期:2008-12-08
资源大小:561k
文件大小:2k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Text;
- using System.Windows.Forms;
- namespace DrawInBitmapAndSave
- {
- public partial class MainForm : Form
- {
- private Bitmap m_bmp = null;
- public MainForm()
- {
- InitializeComponent();
- m_drawInBitmap();
- m_saveBitmap();
- }
- private void m_drawInBitmap()
- {
- // 用于确定位图的位置和大小
- Rectangle rcBitmap = new Rectangle(0, 0, 200, 150);
- // 创建一个空的位图
- m_bmp = new Bitmap(rcBitmap.Width, rcBitmap.Height);
- // 从上面的位图创建一个绘图表面
- Graphics g = Graphics.FromImage(m_bmp);
- // 创建用于填充位图和绘制文字的画刷
- SolidBrush bb = new SolidBrush(Color.Black);
- SolidBrush bw = new SolidBrush(Color.White);
- // 创建文字的字体和格式
- Font f = new Font("Arial", 25.0f, FontStyle.Bold | FontStyle.Italic);
- StringFormat sf = new StringFormat();
- sf.Alignment = StringAlignment.Center;
- sf.LineAlignment = StringAlignment.Center;
- // 填充整个位图
- g.FillRectangle(bb, rcBitmap);
- // 在位图上绘制文字
- g.DrawString("Mobile", f, bw, rcBitmap, sf);
- // 释放资源
- g.Dispose();
- bb.Dispose();
- bw.Dispose();
- f.Dispose();
- sf.Dispose();
- }
- private void m_saveBitmap()
- {
- if(m_bmp == null)
- return;
- m_bmp.Save("mobile.gif", ImageFormat.Gif);
- }
- private void MainForm_Paint(object sender, PaintEventArgs e)
- {
- if(m_bmp == null)
- return;
- e.Graphics.DrawImage(m_bmp, 0, 0);
- }
- }
- }