using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace SaveToBmp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox PictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// PictureBox1
//
this.PictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox1.Location = new System.Drawing.Point(88, 0);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(264, 284);
this.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.PictureBox1.TabIndex = 2;
this.PictureBox1.TabStop = false;
this.PictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
this.PictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 286);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.PictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
Point oldPoint =new Point(0,0); private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
(sender as PictureBox).CreateGraphics().DrawLine(System.Drawing.SystemPens.ControlText,oldPoint.X,oldPoint.Y, e.X,e.Y);
oldPoint=new Point(e.X,e.Y);
}
} private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
oldPoint=new Point(e.X,e.Y);
} private void button1_Click(object sender, System.EventArgs e)
{
SaveFileDialog f=new SaveFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
try
{
Graphics g1 = PictureBox1.CreateGraphics();
Image MyImage = new Bitmap(PictureBox1.Width, PictureBox1.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, PictureBox1.Width, PictureBox1.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(f.FileName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
} private void button2_Click(object sender, System.EventArgs e)
{
OpenFileDialog f=new OpenFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
PictureBox1.Image=new Bitmap(f.FileName);
}
}
protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x0011;
if (m.Msg ==WM_QUERYENDSESSION)
{
Application.Exit();
return;
}
else
{
base.WndProc (ref m);
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace SaveToBmp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox PictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// PictureBox1
//
this.PictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox1.Location = new System.Drawing.Point(88, 0);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(264, 284);
this.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.PictureBox1.TabIndex = 2;
this.PictureBox1.TabStop = false;
this.PictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
this.PictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 286);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.PictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
Point oldPoint =new Point(0,0); private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
(sender as PictureBox).CreateGraphics().DrawLine(System.Drawing.SystemPens.ControlText,oldPoint.X,oldPoint.Y, e.X,e.Y);
oldPoint=new Point(e.X,e.Y);
}
} private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
oldPoint=new Point(e.X,e.Y);
} private void button1_Click(object sender, System.EventArgs e)
{
SaveFileDialog f=new SaveFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
try
{
Graphics g1 = PictureBox1.CreateGraphics();
Image MyImage = new Bitmap(PictureBox1.Width, PictureBox1.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, PictureBox1.Width, PictureBox1.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(f.FileName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
} private void button2_Click(object sender, System.EventArgs e)
{
OpenFileDialog f=new OpenFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
PictureBox1.Image=new Bitmap(f.FileName);
}
}
protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x0011;
if (m.Msg ==WM_QUERYENDSESSION)
{
Application.Exit();
return;
}
else
{
base.WndProc (ref m);
}
}
}
}
谢谢大家真心帮助我!~~~
{
if(e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
} private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(x != 0 || y != 0)
{
xx = e.X;
yy = e.Y; this.panel1.CreateGraphics().DrawLine(Pens.Black,x,y,xx,yy);
}
}
当然,还要最好另外做一个方法。放在窗体的Paint事件里面去。
这就需要你要用一系列的变量来保存你的图,比如说,类型,到底是直线还是圆?还有坐标。等等
报错啊~~谢谢你了~~
Bitmap bit = new Bitmap(10,10,this.panel1.CreateGraphics());
bit.Save(................;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace SaveToBmp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox PictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// PictureBox1
//
this.PictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.PictureBox1.Location = new System.Drawing.Point(88, 0);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(264, 284);
this.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.PictureBox1.TabIndex = 2;
this.PictureBox1.TabStop = false;
this.PictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp);
this.PictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
this.PictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 24);
this.button3.TabIndex = 4;
this.button3.Text = "直线";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(8, 80);
this.button4.Name = "button4";
this.button4.TabIndex = 5;
this.button4.Text = "画";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 144);
this.button5.Name = "button5";
this.button5.TabIndex = 6;
this.button5.Text = "圆";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(8, 184);
this.button6.Name = "button6";
this.button6.TabIndex = 7;
this.button6.Text = "矩形";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 286);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.PictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
Point oldPoint =new Point(0,0); private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (DrawStyle==0)
{
if (e.Button==MouseButtons.Left)
{
(sender as PictureBox).CreateGraphics().DrawLine(System.Drawing.SystemPens.ControlText,oldPoint.X,oldPoint.Y, e.X,e.Y);
oldPoint=new Point(e.X,e.Y);
}
}
} private void PictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
oldPoint=new Point(e.X,e.Y);
} private void button1_Click(object sender, System.EventArgs e)
{
SaveFileDialog f=new SaveFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
try
{
Graphics g1 = PictureBox1.CreateGraphics();
Image MyImage = new Bitmap(PictureBox1.Width, PictureBox1.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, PictureBox1.Width, PictureBox1.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(f.FileName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
} private void button2_Click(object sender, System.EventArgs e)
{
OpenFileDialog f=new OpenFileDialog();
if (f.ShowDialog()==DialogResult.OK)
{
PictureBox1.Image=new Bitmap(f.FileName);
}
}
protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x0011;
if (m.Msg ==WM_QUERYENDSESSION)
{
Application.Exit();
return;
}
else
{
base.WndProc (ref m);
}
} int DrawStyle=0;
private void button3_Click(object sender, System.EventArgs e)
{
DrawStyle=1;
}
{
if (DrawStyle==1)
{
this.PictureBox1.CreateGraphics().DrawLine(System.Drawing.SystemPens.ControlText,oldPoint,new Point(e.X,e.Y));
}
else if (DrawStyle==2)
{
this.PictureBox1.CreateGraphics().DrawEllipse(System.Drawing.SystemPens.ControlText,oldPoint.X,oldPoint.Y,e.X-oldPoint.X,e.Y-oldPoint.Y);
}
else if (DrawStyle==3)
{
this.PictureBox1.CreateGraphics().DrawRectangle(System.Drawing.SystemPens.ControlText,oldPoint.X,oldPoint.Y,e.X-oldPoint.X,e.Y-oldPoint.Y);
}
} private void button4_Click(object sender, System.EventArgs e)
{
DrawStyle=0;
} private void button5_Click(object sender, System.EventArgs e)
{
DrawStyle=2;
} private void button6_Click(object sender, System.EventArgs e)
{
DrawStyle=3;
}
}
}
http://www.c-sharpcorner.com//Code/2002/Mar/RubberBandLinesNShapes.asp