using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System;namespace test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <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.SuspendLayout();
// 
// pictureBox1
// 
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(96, 48);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(248, 224);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
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);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 325);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
//this.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
bool StartedCrop = false;
bool CropOn =false;
bool CroppingPaint = false;
int DeltaX = 0;
int DeltaY = 0;
private System.Windows.Forms.PictureBox pictureBox1;
Point StartPoint = new Point(0,0); private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (StartedCrop == false)
{
DeltaX = 0;
DeltaY  = 0;
} if (CropOn == true)
{
StartedCrop = true;
CroppingPaint = true;
StartPoint = new Point(e.X, e.Y);
DeltaX = 0;
DeltaY = 0;
Invalidate();
}

} private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
CropOn = false;
StartedCrop = false;
SetupCropping();
this.pictureBox1.Invalidate();
} private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ((CroppingPaint && CropOn) && StartedCrop)
{
DeltaX = e.X - StartPoint.X; 
if (DeltaX < 0)
DeltaX = 0;
DeltaY = e.Y - StartPoint.Y; 
if (DeltaY < 0)
DeltaY = 0; // InvalidateClipBorders(new Rectangle(StartPoint.X, StartPoint.Y, DeltaX, DeltaY));
//Invalidate(new Rectangle(StartPoint.X, StartPoint.Y, DeltaX + 20, DeltaY + 20));
this.pictureBox1.Invalidate();
}
} private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (CroppingPaint == true)
{
Graphics g = e.Graphics ;   // this.CreateGraphics();
int x = this.pictureBox1.Left - this.Left ;
int y = this.Top - this.pictureBox1.Top; g.DrawRectangle(Pens.Red, StartPoint.X -x , StartPoint.Y  - y, DeltaX, DeltaY);
//this.CreateGraphics().DrawRectangle(Pens.Red, StartPoint.X, StartPoint.Y, DeltaX, DeltaY);
//g.Dispose();
}
}
private void SetupCropping()
{
CropOn = true;
this.Cursor = Cursors.Cross;
}
}
}我简单的修改了一下你的代码,部份的达到了你要实现的效果。你是想让红矩形框只画在picturebox内,是这样吗?如果是这样:
你的代码中有这样几点错误:1.不应把Form和Picture的Paint事件处理函数写在一起,我感觉你的Form并不需要Paint 事件处理。2.你在PictureBox的Paint处理函数中,本可以直接使用参数中提供的Graphics对象,但你却调用CreateGraphics函数来创建,结果你创建的Graphics并不是PictureBox的设备上下文,而是Form!的,导致了图只能画在Form上。3.你没有使用座标变换。4..我认为实现这个功能,最好是从Picturebox或UserControl派生一个类,把绘图方法封闭在这个类里,这样实现会更合理,也不用座标变换。

解决方案 »

  1.   

    1。写在一起是为了调试方便,恩,已经修改了。
    2。调用CreateGraphics,我看了一篇叫做双缓冲解决闪烁的问题,
    就是这么调用的,相信g.DrawRectangle(Pens.Red, StartPoint.X -x , StartPoint.Y  - y, DeltaX, DeltaY);
    pictureBox1.CreateGraphics().DrawRectangle(Pens.Red, StartPoint.X -x , StartPoint.Y  - y, DeltaX, DeltaY);
    就完美了.
    this.pictureBox1.Invalidate();是我要的
    3。坐标的变换,我还没想道什么好办法。用4。的方法给点提示。