分类: C#/.net
2015-11-17 08:10:40
public partial class Form1 : Form
{
Graphics g;
Rectangle rect
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
rect = new Rectangle(5, 10, 30, 30);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// 作图方法必须在Paint事件中处理!
g.DrawRectangle(Pens.Red, rect);
}
}
|
public partial class Form1 : Form
{
//Graphics g;
Rectangle rect
public Form1()
{
InitializeComponent();
//g = this.CreateGraphics();
rect = new Rectangle(5, 10, 30, 30);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//直接使用事件参数e中的Graphics作图
e.Graphics.DrawRectangle(Pens.Red, rect);
}
}
|
作图方法必须在Paint事件中处理。原因与Form的刷新有关:每当窗体被Windows系统刷新都会引发Paint事件。在Paint事件中,你必须重新绘制你要的图形,否则画出的图形在窗体被刷新后就消失了;如果你把绘制图形的方法放到别的地方(比如你放在Form的Load事件处理中),窗体被Windows刷新后,你画出的矩形就被刷没了。
private void button1_Click(object sender, EventArgs e)
{
Bitmap bp = new Bitmap("S1.BMP");
//在pictureBox1里绘画
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle);
RectangleF destRect = new RectangleF(20, 20, 100, 100);
destRect.Width += 50;
GraphicsUnit units = GraphicsUnit.Pixel;
//从g对象(即pictureBox1)的(10,10)处开始绘制图bp的destRect区域
g.DrawImage(bp, 10, 10, destRect, units);
//Graphics.DrawImage (Image, Int32, Int32, Rectangle, GraphicsUnit)
//(要绘制的图bp(Image), 要绘制的开始位置Int32, Int32,(要绘制什么样的图(可选项)矩形),图形单元.像素(GraphicsUnit.Pixel)或单位(units))// /*菜鸟个人理解注解翻译不能信为真
//bp.Save("..jpg");
//修改之后的图片保存
Bitmap saveBitmap = new Bitmap(1280, 1024);
g = Graphics.FromImage(saveBitmap);
g.DrawImage(bp, 50, 50, destRect, units);
saveBitmap.Save("E:\\bmp.bmp");
}
//以上程序来源为网络,本只复制,为验证,,