C# 用鼠标擦除加载图像的部分内容

C# 用鼠标擦除加载图像的部分内容,c#,winforms,C#,Winforms,我已在面板中加载图像。我想使用鼠标(在面板上拖动)擦除该图像的部分。以下是我加载图像的代码: private void drawP_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(myImage, new Point(0, 0)); } 我怎么做? 提前谢谢。 更新: 很抱歉,前面没有说,我已经设置了另一个图像(image2)作为面板的背景,我希望在擦除我的图像(上面加载了代码

我已在面板中加载图像。我想使用鼠标(在面板上拖动)擦除该图像的部分。以下是我加载图像的代码:

 private void drawP_Paint(object sender, PaintEventArgs e)
    {
            e.Graphics.DrawImage(myImage, new Point(0, 0));
    }
我怎么做? 提前谢谢。 更新:
很抱歉,前面没有说,我已经设置了另一个图像(image2)作为面板的背景,我希望在擦除我的图像(上面加载了代码的图像)后可以看到它。

您好,我假设您希望此功能像油漆上的橡皮擦一样工作

您将需要3项活动 1.mousedown-调用第一个擦除并打开mousemove事件方法。 2.mouseup-停止mousemove事件方法 3.mousemove-仅调用擦除方法

代码://part pseudo因为即时消息现在不在visual studio中:(

此外,您还需要为面板创建图形对象:(
我希望这有帮助,因为这个问题有点模糊。

这里我创建了一个简单的例子。当然可以做得更好,但我只是想知道如何做……所以分享我的结果

public partial class mainForm : Form
{
    private Bitmap image;
    private Rectangle imgRect;

    public mainForm()
    {
        InitializeComponent();
        BackColor = Color.Chartreuse;
        image = new Bitmap(@"C:\image.jpg");
        imgRect = new Rectangle(0,0,image.Width, image.Height);
    }

    private void main_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(image, 0, 0);
    }

    private void main_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left && e.X < image.Width && e.Y < image.Height)
        {
           image.SetPixel(e.X, e.Y, Color.Magenta);//change pixel color;
           image.MakeTransparent(Color.Magenta);//Make image transparent
           Invalidate(imgRect);
        }
    }
}
public分部类mainForm:Form
{
私有位图图像;
私有矩形imgRect;
公共表格(
{
初始化组件();
背景色=颜色。黄绿色;
image=新位图(@“C:\image.jpg”);
imgRect=新矩形(0,0,image.Width,image.Height);
}
私有void main_Paint(对象发送器,PaintEventArgs e)
{
e、 Graphics.DrawImage(图像,0,0);
}
private void main_MouseMove(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left&&e.X
…让我们来测试一下


哈!害怕我删除了他的眼睛:)

笔上的纹理刷可以用来擦除

工作示例(图像1和图像2是相同大小的图像):


问题很简单,但答案真的很难谢谢你的回答,我更新了问答,很抱歉没有早点做。我认为这个答案在面板上画了白点,所以擦除后我看不到背景图像。通过立即使像素透明来避免昂贵的MakeTransparent()调用。Color.FromArgb()alpha为0。顺便说一句,有点令人毛骨悚然。我看到您添加了“this.DoubleBuffered=true.由于我的项目中有其他代码,我不希望孔项目被双重缓冲。有其他方法吗?或者我可以在其他任何地方添加它吗?@asmagod这只是我的例子,它使用的是表单,而不是面板。如果你做了大量的绘图,你应该使用双重缓冲面板,这听起来像你。我想我在一个例子中涵盖了这一点谢谢你的关注和回答。我不知道如何在面板中使用你的方式。虽然你以前解释过,我也不知道如何使面板双缓冲。请为面板编写孔代码。我在这个领域是新手。
public partial class mainForm : Form
{
    private Bitmap image;
    private Rectangle imgRect;

    public mainForm()
    {
        InitializeComponent();
        BackColor = Color.Chartreuse;
        image = new Bitmap(@"C:\image.jpg");
        imgRect = new Rectangle(0,0,image.Width, image.Height);
    }

    private void main_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(image, 0, 0);
    }

    private void main_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left && e.X < image.Width && e.Y < image.Height)
        {
           image.SetPixel(e.X, e.Y, Color.Magenta);//change pixel color;
           image.MakeTransparent(Color.Magenta);//Make image transparent
           Invalidate(imgRect);
        }
    }
}
Bitmap bmp1;
TextureBrush tb;
Point _LastPoint;

public Form1()
{
  InitializeComponent();
  this.DoubleBuffered = true;

  bmp1 = new Bitmap(@"c:\image1.png");
  tb = new TextureBrush(new Bitmap(@"c:\image2.png"));
}

private void Form1_MouseMove(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left) {
    if (!_LastPoint.IsEmpty) {
      using (Graphics g = Graphics.FromImage(bmp1))
      using (Pen p = new Pen(tb, 15)) {
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.Round;
        g.DrawLine(p, _LastPoint, e.Location);
      }
    }
    _LastPoint = e.Location;
    this.Invalidate();
  }
}

private void Form1_MouseUp(object sender, MouseEventArgs e) {
  _LastPoint = Point.Empty;
}

private void Form1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.DrawImage(bmp1, new Point(0, 0));
}