如何在两个Bitmap之间复制图像?

limengchen 2002-11-26 04:20:39
将一个Bitmap对象中的一部分复制到另一个Bitmap对象的指定位置,并可以设置透明色?不会一个一个点拷贝吧!
...全文
752 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dy_2000_abc 2002-12-01
  • 打赏
  • 举报
回复
关键在这一句:Graphics g=Graphics.FromHwnd(pictureBox2.Handle);这样,在g上画的东西自然就显示在pictureBox2上了,pictureBox2.Image包含了后来画上去的东西。
limengchen 2002-12-01
  • 打赏
  • 举报
回复
to dy_2000_abc(芝麻开门):
那么如何实现我所说的功能呢?
dy_2000_abc 2002-12-01
  • 打赏
  • 举报
回复
我觉得你可能对Graphics的理解有误,Graphics不是图像对象,你不能在Graphics和Image之间进行转换。
limengchen 2002-12-01
  • 打赏
  • 举报
回复
我现在有几张图片(从文件加载的,因此可以为Image、Graphics、Bitmap或其它形式),想将上面的白色设置为透明色(目前是用Bitmap.MakeTransparent实现),合并(目前是用Graphics.DrawImage实现),然后在PictureBox中显示(PictureBox.Picture属性为Image对象)
不知谁有实现的方法
dy_2000_abc 2002-11-30
  • 打赏
  • 举报
回复
Graphics 类提供将对象绘制到显示设备的方法,Bitmap是用于处理由像素数据定义的图像的对象,你不能将Graphics转成Bitmap的,你试一试下面的语句,pictureBox2的图像将发生变化。
Graphics g=Graphics.FromHwnd(pictureBox2.Handle);
g.DrawImage(bm,0,0,70,70);
dy_2000_abc 2002-11-29
  • 打赏
  • 举报
回复
hope it will be helpful for you.

PictureBox pictureBox1=new 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();
limengchen 2002-11-29
  • 打赏
  • 举报
回复
to dy_2000_abc(芝麻开门) :
你的方法只能将image转成bitmap,不能将graphics转成bitmap。
limengchen 2002-11-27
  • 打赏
  • 举报
回复
Sorry,是Graphics转换成Image怎么办?
limengchen 2002-11-27
  • 打赏
  • 举报
回复
但是Graphics转换成Bitmap怎么办?Graphics对象没有Clone方法。
yarshray 2002-11-26
  • 打赏
  • 举报
回复
image.clone();
limengchen 2002-11-26
  • 打赏
  • 举报
回复
to aspcn(飞刀) :
我已经找到那个方法了,叫MakeTransparent。但是,PictureBox.Image只接受Image对象,我怎样才能把一个Graphics对象在PictureBox中显示出来?
aspcn 2002-11-26
  • 打赏
  • 举报
回复
直接画上去就成了.

透明色.Bitmap中有一个方法,我忘记名字了:)
他有一个例子就是教你取点,然后设置透明..

你在帮助中,用"透明"搜索一下就知道了.
limengchen 2002-11-26
  • 打赏
  • 举报
回复
to all:
我现在用Graphics.DrawImage方法复制图像,但无法设置透明色,谁知道如何设置透明色?
qieyj 2002-11-26
  • 打赏
  • 举报
回复
楼上的代码很不错啊.同意,您仔细看看把
snewxf 2002-11-26
  • 打赏
  • 举报
回复
晕!怎么COPY了那么多代码:(55555555555
snewxf 2002-11-26
  • 打赏
  • 举报
回复
之是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);
}
}
}
你参考一下!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧