读书人

BitMap背景色有关问题

发布时间: 2012-02-21 16:26:23 作者: rapoo

BitMap背景色问题
为什么bitmap类型的变量没有设置背景色时,画图是会产生重影?

下面为代码 发表于:2009-02-02 21:25:01 楼主
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2009-2-2
* Time: 20:26
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DrawG
{
/// <summary>
/// Description of Box2.
/// </summary>
public partial class Box2 : Form
{

protected Image imgPic;
protected Point pS,pE;
protected Graphics temG;
protected Image imgTem;
protected Pen p;
protected bool flag=false ;


public Box2()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
p=new Pen (Color.Blue ,2);
imgPic =new Bitmap (pbImg .Width ,pbImg .Height );
imgTem =(Image )imgPic.Clone () ;

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void PbImgMouseDown(object sender, MouseEventArgs e)
{
pS =new Point (e.X ,e.Y );
flag =true ;
}

void PbImgMouseMove(object sender, MouseEventArgs e)
{
if (flag )
{
pE =new Point (e.X,e.Y );
Image img=(Image )imgPic .Clone ();
temG =Graphics .FromImage (img );
temG .DrawLine (p,pS ,pE );
temG .Dispose ();
temG =Graphics .FromImage (imgTem );
temG .DrawImage (img,0,0);
temG .Dispose ();
pbImg .CreateGraphics ().DrawImage (img ,0,0);
img .Dispose ();

}
}

void PbImgMouseUp(object sender, MouseEventArgs e)
{
flag =false ;
temG =Graphics .FromImage (imgPic );
temG .DrawImage (imgTem,0,0);
temG .Dispose ();
}

void PbImgPaint(object sender, PaintEventArgs e)
{
Graphics g=e.Graphics ;
g.DrawImage (imgPic ,0,0);
}
}
}

用鼠标画直线产生重影

但是如果为imgPic和imgTem两个变量加入了背景色后就不会产生重影?
比如加入下面的代码:
Graphics g=Graphics .FromImage (imgPic );
g.FillRectangle (new SolidBrush (Color .Gray ),new Rectangle (0,0,pbImg .Width ,pbImg .Height ));
g.Dispose ();
g=Graphics .FromImage (imgTem );
g.FillRectangle (new SolidBrush (Color .Gray ),new Rectangle (0,0,pbImg .Width ,pbImg .Height ));
g .Dispose ();

[解决办法]
简单的说


Graphics g=Graphics .FromImage (imgPic );
g.FillRectangle (new SolidBrush (Color .Gray ),new Rectangle (0,0,pbImg .Width ,pbImg .Height ));
g.Dispose ();
g=Graphics .FromImage (imgTem );
g.FillRectangle (new SolidBrush (Color .Gray ),new Rectangle (0,0,pbImg .Width ,pbImg .Height ));
g .Dispose ();


在执行这个后其实还是重影~~ 只是不设置前面的是透明 你可以看到重影~~设置就看不到重影

读书人网 >C#

热点推荐