之是COP一个图像到剪贴版的例子:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
int bitmapWidth = this.ClientRectangle.Width;
int bitmapHeight = button1.Top- 20 ;
bitmap = new Bitmap(bitmapWidth, bitmapHeight, this.CreateGraphics());
Graphics g = Graphics.FromImage(bitmap);
g.Clear(this.BackColor);
g.FillEllipse(new SolidBrush(Color.YellowGreen),5,5 ,bitmapWidth - 10, bitmapHeight - 10);
g.DrawRectangle(new Pen(Color.Black, 4.0f), 1, 1, bitmapWidth, bitmapHeight);
//
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 216);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "copy to cipboard";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 216);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 1;
this.button2.Text = "paste to cipboard data";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bitmap , new Rectangle(0,0, this.ClientRectangle.Width, button1.Top -20),0, 0,bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
Clipboard.SetDataObject(bitmap);
}
catch(Exception exce)
{
MessageBox.Show(exce.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void button2_Click(object sender, System.EventArgs e)
{
try
{
IDataObject dataObject = Clipboard.GetDataObject();
object data = dataObject.GetData(typeof(object));
if(data != null)
{
bitmap = (Bitmap)data;
this.Invalidate(this.ClientRectangle);
}
}
catch(Exception exce)
{
MessageBox.Show(exce.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void Form1_Resize(object sender, System.EventArgs e)
{
button1.Location = new Point(this.ClientRectangle.Width -150, ClientRectangle.Height-50);
button2.Location = new Point(this.ClientRectangle.Width -700, ClientRectangle.Height-50);
this.Invalidate(this.ClientRectangle);
}
}
}
你参考一下!
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
int bitmapWidth = this.ClientRectangle.Width;
int bitmapHeight = button1.Top- 20 ;
bitmap = new Bitmap(bitmapWidth, bitmapHeight, this.CreateGraphics());
Graphics g = Graphics.FromImage(bitmap);
g.Clear(this.BackColor);
g.FillEllipse(new SolidBrush(Color.YellowGreen),5,5 ,bitmapWidth - 10, bitmapHeight - 10);
g.DrawRectangle(new Pen(Color.Black, 4.0f), 1, 1, bitmapWidth, bitmapHeight);
//
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 216);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "copy to cipboard";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 216);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 1;
this.button2.Text = "paste to cipboard data";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bitmap , new Rectangle(0,0, this.ClientRectangle.Width, button1.Top -20),0, 0,bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
Clipboard.SetDataObject(bitmap);
}
catch(Exception exce)
{
MessageBox.Show(exce.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void button2_Click(object sender, System.EventArgs e)
{
try
{
IDataObject dataObject = Clipboard.GetDataObject();
object data = dataObject.GetData(typeof(object));
if(data != null)
{
bitmap = (Bitmap)data;
this.Invalidate(this.ClientRectangle);
}
}
catch(Exception exce)
{
MessageBox.Show(exce.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void Form1_Resize(object sender, System.EventArgs e)
{
button1.Location = new Point(this.ClientRectangle.Width -150, ClientRectangle.Height-50);
button2.Location = new Point(this.ClientRectangle.Width -700, ClientRectangle.Height-50);
this.Invalidate(this.ClientRectangle);
}
}
}
你参考一下!
解决方案 »
- c# Oracle.DataAccess.dll引用问题
- 求助sqlconnection问题
- Control.Invoke 必须用于与在独立线程上创建的控件交互
- 根据 object 的类型信息Type,如何将object强制转换Type对于的类型
- 急求 C# Netron二次开发滴问题```大侠们帮帮我吖` 狠急
- 谁有 Janus.WinForms.Controls.Suite.v2.0.1000注册机 啊
- c# 窗体键盘事件
- sqlserver 2000中图片的存储与读取操作?
- 请问水晶报表中如何设定打印纸张为自定义的呢?
- 求大神们给个完整的程序
- Crystal report问题!急!今天结!
- 关于抽象类的菜鸟问题,在线等候,一定要帮帮俺GGJJDDMM........
我现在用Graphics.DrawImage方法复制图像,但无法设置透明色,谁知道如何设置透明色?
他有一个例子就是教你取点,然后设置透明..你在帮助中,用"透明"搜索一下就知道了.
我已经找到那个方法了,叫MakeTransparent。但是,PictureBox.Image只接受Image对象,我怎样才能把一个Graphics对象在PictureBox中显示出来?
PictureBox pictureBox2=new PictureBox();
...
Bitmap bm=(Bitmap)pictureBox1.Image;
bm.MakeTransparent(bm.GetPixel(0,0));
Graphics g=Graphics.FromHwnd(pictureBox2.Handle);
g.DrawImage(bm,0,0,70,70);\\在特定位置,以特定大小画图
g.Dispose();
你的方法只能将image转成bitmap,不能将graphics转成bitmap。
Graphics g=Graphics.FromHwnd(pictureBox2.Handle);
g.DrawImage(bm,0,0,70,70);
不知谁有实现的方法
那么如何实现我所说的功能呢?