本文介绍了擦除面板上加载的图像的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个面板,并使用myImage1设置了面板的背景图像.
然后我在面板上加载了另一个图像(名为myImage2).代码如下:

Hi,I have a panel and I have set the background image of my panel with myImage1.
Then I have loaded another image(named myImage2) on my panel.Here is the code:

panel1.BackgroundImage = myImage1;
private void drawP_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(myImage2, new Point(0, 0));
 }


现在,我想用鼠标擦除myImage2的一部分(在面板上拖动),以便在擦除后可以看到myImage1.
我该怎么办?
感谢您的关注.
更新:我通过使用OriginalGriff的答案编写了以下代码.此代码不执行任何操作,仅在面板中显示图片.我知道我没有很好地使用他的答案,但是我能做的全部.请请尽可能进行更正.再次感谢


Now I want to erase parts of the myImage2 by mouse(dragging on the panel) so that myImage1 can be seen after erasing.
How can I do that?
Thanks for your attention.
updated:I wrote the following code by using OriginalGriff''s answer .This code does nothing and only shows the picture in a panel.I know I din''t use his answer well but it''s all I could do.Please correct it if possible.Thanks again

Image mySpareImage;
       Image myImage;


       private void Form1_Load(object sender, EventArgs e)
       {
           myPanel.BackgroundImage = Image.FromFile(@"E:\C#\for ore project\p\tt.jpg");
           mySpareImage = Image.FromFile(@"E:\C#\for ore project\p\1234.jpg");
           myClearBit.MakeEmpty();
           myImage = Image.FromFile(@"E:\C#\for ore project\p\tt.jpg");
           myPanel.Width = myImage.Width;
           myPanel.Height = myImage.Height;
       }
       private Region myClearBit = new Region();
       private void myPanel_Paint(object sender, PaintEventArgs e)
       {
           e.Graphics.ExcludeClip(myClearBit);
           e.Graphics.DrawImage(mySpareImage, new Point(0, 0));

           e.Graphics.SetClip(myClearBit, CombineMode.Replace);
           e.Graphics.DrawImage(myImage, new Point(0, 0));
       }
       private void myPanel_MouseDown(object sender, MouseEventArgs e)
       {
           myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
           myPanel.Invalidate();
       }
       private void myPanel_MouseMove(object sender, MouseEventArgs e)
       {
           myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
           myPanel.Invalidate();
       }
       private void button1_Click(object sender, EventArgs e)
       {
           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               Bitmap bm = new Bitmap(openFileDialog1.FileName);
               myImage = (Image)bm;
           }
       }

       public class MyPanel : Panel
       {
           protected override void OnPaintBackground(PaintEventArgs e)
           {
               // Prevent background clearing
           }
                       //    e.Graphics.ExcludeClip(myClearBit);
        }

推荐答案

public class MyPanel : Panel
    {
    protected override void OnPaintBackground(PaintEventArgs e)
        {
        // Do not erase the background
        }
    }

构建项目,MyPanel应该会在您的工具箱中显示我的魔力.
将MyPanel添加到设计器中的窗体"中,并添加两个按钮.将MyPanel命名为"myPanel",将按钮命名为"butLoadBackImage"和"butLoadFrontImage".
在文件顶部的using语句中添加一行:

Build the project, and MyPanel should appear my magic in your toolbox.
Add MyPanel to your Form in the designer, and also add your two buttons. Call the MyPanel "myPanel" and the buttons "butLoadBackImage" and "butLoadFrontImage".
Add a line to the using statements at the top of the file:

using System.Drawing.Drawing2D;


将三个类级别的变量添加到以下形式:


Add three class level variables to the form:

private Image myBackImage;
private Image myFrontImage;
private Region myClearBit = new Region();


您的表单应处理Load事件(在设计器中双击表单背景):


Your form should handle the Load event (double click the form background in the desginer):

private void Form1_Load(object sender, EventArgs e)
    {
    myBackImage = new Bitmap(10, 10);
    myFrontImage = new Bitmap(10,10);
    myClearBit.MakeEmpty();
    }

双击设计器中的每个按钮,添加一个处理程序:

Double click each button in the designer, to add a handler:

private void butLoadBackImage_Click(object sender, EventArgs e)
    {
    myBackImage = Image.FromFile(@"D:\Temp\MyPic.jpg");
    myClearBit.MakeEmpty();
    myPanel.Invalidate();
    }

private void butLoadFrontImage_Click(object sender, EventArgs e)
    {
    myFrontImage = Image.FromFile(@"D:\Temp\MyOtherPic.jpg");
    myClearBit.MakeEmpty();
    myPanel.Invalidate();
    }

返回设计师.单击面板,然后查看属性"窗格.单击事件按钮(看起来像是闪电).双击MouseDown事件以添加一个处理程序,然后对MouseMove和Paint事件执行相同的操作.

Go back to the designer. single click the panel, and look at the Properties Pane. Click the Events button (it looks like a lightning bolt). Double click on the MouseDown event to add a handler, then do the same for the MouseMove and Paint events.

private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
    myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
    myPanel.Invalidate();
    }

private void myPanel_MouseMove(object sender, MouseEventArgs e)
    {
    myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
    myPanel.Invalidate();
    }

private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.ExcludeClip(myClearBit);
    e.Graphics.DrawImage(myFrontImage, new Point(0, 0));
    e.Graphics.SetClip(myClearBit, CombineMode.Replace);
    e.Graphics.DrawImage(myBackImage, new Point(0, 0));
    }

运行应用程序.

Run the application.
You should find it works for you as well.



private void frmMain_Load(object sender, EventArgs e)
    {
    myPanel.BackgroundImage = Image.FromFile(@"D:\Temp\MyPic.jpg");
    mySpareImage = Image.FromFile(@"D:\Temp\MyOtherPic.jpg");
    myClearBit.MakeEmpty();
    }
private Region myClearBit = new Region();
private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.ExcludeClip(myClearBit);
    e.Graphics.DrawImage(mySpareImage, new Point(0, 0));
    }

private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
    myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
    myPanel.Invalidate();
    }

private void myPanel_MouseMove(object sender, MouseEventArgs e)
    {
    myClearBit.Union(new Rectangle(e.X - 5, e.Y - 5, 10, 10));
    myPanel.Invalidate();
    }

您可能需要使用它,因为它可能会可怕地闪烁!
我很想保留两个区域,一个打开"和一个关闭",并在添加到另一个区域时将其排除在外.然后,我不使用背景,而是在每个区域绘制一张图片.



很明显,我不希望我的图像闪烁得可怕.我应该如何划分两个区域?应该如何使用它们?请像我一样引导我."

考虑一下,您不需要第二个区域.
构造一个MyPanel类(您想要覆盖背景涂料,以阻止其发生)

You may have to play with this, as it will probably flash horribly!
I would be tempted to keep two regions, one "On" and one "Off" and exclude from one when you add to the other. Then instead of using the background, I would draw one picture in each region.



"It''s obvious that I don''t want my image to flash horrible.How can I have two regions? and how should I use them? please guide me as you alwase did."

Thinking about it, you don''t need a second region.
Construct a MyPanel Class (you want to override the back ground paint, to stop it ever occurring)

public class MyPanel : Panel
     {
     protected override void OnPaintBackground(PaintEventArgs e)
         {
         // Prevent background clearing
         }
     }

用MyPanel替换面板.
然后,像上面一样绘制,但是在Paint事件处理程序中添加了两行:

Replace your panel with a MyPanel.
Then, just draw as above, but with two lines added to the Paint event handler:

private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.ExcludeClip(myClearBit);
    e.Graphics.DrawImage(mySpareImage, new Point(0, 0));
    e.Graphics.SetClip(myClearBit, CombineMode.Replace);
    e.Graphics.DrawImage(myImage, new Point(0, 0));
    }

您所要做的就是将以前用作背景的图像加载到myImage中,您就关闭了!

ExcludeClip方法会删除您将鼠标移到的位以外的所有位,SetClip仅允许在这些位上绘制.

All you have to do is load myImage with the image you used to use as the background, and you are off!

The ExcludeClip method removes all but the bits you have moved the mouse over, the SetClip only allows those bits to be drawn on.


这篇关于擦除面板上加载的图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:58