0

I have a PictureBox where I display an image (Let's call it Image1). There's a second image (Image2) which needs to be revealed as the user hovers the Image1 with the mouse. I only need to reveal part of Image2 (a 10X10 pixel size box), not the whole image as the mouse moves.

Both images are BMP.

How can I accomplish this task? I would think using overlays?

I display the Image1 in the picturebox and then I load the Image2 in memory, now I just need to display the portions of the Image2 over Image1 as the mouse moves.

Thank you,

Matias
  • 539
  • 5
  • 28
  • 1
    Read the appropriate pixels from Image2 and put them in the displayed object – Val Feb 13 '15 at 14:06
  • Ok so your suggestion is to use the Draw method to display the portions of Image2 as the mouse moves? – Matias Feb 13 '15 at 14:10
  • I'm not sure what methods and data objects you're using now to keep Image1 and display it. I'd assume, the data object is some 2D array containing pixel values. Overwrite them with pixels from Image2 in appropriate coords and then display the new image – Val Feb 13 '15 at 14:41
  • To spell out Val's advice: You can use the DrawImage format which can copy a rectanlge form one image to a location in another image. – TaW Feb 13 '15 at 14:43
  • Have you resolved your problem? – TaW Feb 17 '15 at 22:06
  • Hi, yes! all answers have been great, thank you! – Matias Feb 20 '15 at 12:44

1 Answers1

1

Here is an example:

public Form1()
{
    InitializeComponent();
    pictureBox1.Image = Bitmap.FromFile(your1stImage);
    bmp = (Bitmap)Bitmap.FromFile(your2ndImage);
    pb2.Parent = pictureBox1;
    pb2.Size = new Size(10,10);
    /* this is for fun only: It restricts the overlay to a circle: 
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(pb2.ClientRectangle);
    pb2.Region = new Region(gp);
    */

}

Bitmap bmp;
PictureBox pb2 = new PictureBox();

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    Rectangle rDest= pb2.ClientRectangle;
    Point tLocation =   new Point(e.Location.X - rDest.Width - 5, 
                                  e.Location.Y - rDest.Height - 5);
    Rectangle rSrc= new Rectangle(tLocation, pb2.ClientSize);

    using (Graphics G = pb2.CreateGraphics() )
    {
        G.DrawImage(bmp, rDest, rSrc, GraphicsUnit.Pixel);
    }
    pb2.Location = tLocation;
}

It uses offsets the overly to the top left of the Cursor adding a little for smooth movement..

TaW
  • 53,122
  • 8
  • 69
  • 111