问题描述
我有一个PictureBox,我在其中显示图像(我们称它为Image1).当用户用鼠标悬停Image1时,需要显示第二个图像(Image2).我只需要显示Image2的一部分(一个10X10像素大小的框),而不是鼠标移动时显示的整个图像.
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.
两个图像都是BMP.
如何完成此任务?我会考虑使用叠加层吗?
How can I accomplish this task? I would think using overlays?
我在图片框中显示Image1,然后将Image2加载到内存中,现在只需在鼠标移动时在Image1上显示Image2的部分即可.
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.
谢谢
推荐答案
以下是示例:
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..
这篇关于在另一个图像后面显示图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!