Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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));
 }

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);
        }
Posted
Updated 17-Dec-11 6:23am
v2

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.
C#
public class MyPanel : Panel
    {
    protected override void OnPaintBackground(PaintEventArgs e)
        {
        // Do not erase the background
        }
    }
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:
C#
using System.Drawing.Drawing2D;

Add three class level variables to the form:
C#
private Image myBackImage;
private Image myFrontImage;
private Region myClearBit = new Region();

Your form should handle the Load event (double click the form background in the desginer):
C#
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:
C#
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();
    }
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.
C#
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.
 
Share this answer
 
Comments
ready to learn 18-Dec-11 9:50am    
Thanks a lot for your patience and step by step answer.this code runs well but when custom opens the backimage nothing apears ,unless he moves the mouse on the panel.when moving the mouse without clicking ,it draws the backimage.when custom opens the front image the front image is shown and when moving the mouse(without clicking) it draw the back image.Second problem is that the front image is shown streched (the backimage and the front image are the same size).I should again say that I'm so thankfull for your attempt and attention.
OriginalGriff 18-Dec-11 11:23am    
My images were the same size - if yours are also, and one is shown stretched, then check you are using the same code to draw each image. Mine draws the images in the right order too - the MyOtherPic.jpg file is drawn over the MyPic.jpg image, and disappears as I move the mouse.
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
 
Share this answer
 
v2
Comments
ready to learn 13-Dec-11 8:20am    
can you explain by giving codes please?
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.
C#
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();
    }
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)
C#
public class MyPanel : Panel
     {
     protected override void OnPaintBackground(PaintEventArgs e)
         {
         // Prevent background clearing
         }
     }
Replace your panel with a MyPanel.
Then, just draw as above, but with two lines added to the Paint event handler:
C#
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));
    }
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.
 
Share this answer
 
v2
Comments
ready to learn 13-Dec-11 8:06am    
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.
OriginalGriff 13-Dec-11 8:41am    
Answer updated
ready to learn 13-Dec-11 11:39am    
sorry to ask again.But I can't understand what should I write in the OnPaintBackground method.
OriginalGriff 13-Dec-11 12:05pm    
Absolutely nothing! :laugh:
Just as I showed above - the idea is that when you override OnPaintBackground you become responsible to painting it - if you do nothing, it never gets erased, and so neither does either image. Since the two images fill the panel client area completely, the lack of erase isn't seen, but neither is the flicker it causes.
ready to learn 13-Dec-11 12:18pm    
Thanks a lot OriginalGriff for your attention and answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900