擦除面板上加载的图像的一部分 [英] erasing parts of an image loaded on a panel

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

问题描述

我有一个面板,并使用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);
        }

推荐答案

我不能一直在这里,不幸的是,我还有其他事情要做...
我不太明白您为什么在这里遇到问题,因此我开始了一个新的解决方案来逐步解释它.

我做了什么:

从头开始创建一个新项目:Winforms,C#.

将一个类添加到项目:MyPanel,该类派生自Panel.
该课程的唯一目的是让我防止面板背景被擦除.
I can''t be here all the time, unfortunately, I do have other things to do...
I don''t quite see why you are having a problem here, so I have started a new solution to explain it step by step.

What I did:

Create a new project from scratch: Winforms, C#.

Add a class to the project: MyPanel, which is derived from Panel.
The sole purpose of the class is to allow me to prevent the panel background from being erased.
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.


使用mousedown事件开始区域的创建,(您可以使用鼠标移动来绘制选择矩形,然后在mouseup事件上给出结束的矩形,然后只需使用绘制矩形"选项填充到背景色即可(或者在您的情况下,使用该矩形将image1的部分复制到image2中,从而在显示图像1时产生错觉).

Derek
Use the mousedown event to get start of area, (you can use mouse move to draw a selection rectangle, then on mouseup event gives the end of rectangle, then simply use the draw rectangle option to fill to the back colour ( or in your case use that rectangle to copy the section of image1 into image2 giving the illusion on revealing image 1).

Derek


可能最简单的方法是定义一个类级别的区域,并在您的鼠标按下事件中添加到该区域.如果随后在绘制例程中使用ExcludeClip,则新图像将不会在这些区域中绘制.
Probably the easiest way is to define a class level region, and add to it in your mouse down event. If you then use ExcludeClip in your paint routine, the new image will not be drawn in those areas.
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆