对2个位图执行重叠检测 [英] Performing Overlap Detection of 2 Bitmaps

查看:78
本文介绍了对2个位图执行重叠检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的图片框控件,该控件允许在主图像上分别拖动2个位图,从而允许用户选择2个位图的位置.

I have a custom picturebox control that allows dragging 2 bitmaps separately on the Main image,thus allowing the user to select the location of the 2 bitmaps.

用于第一个位图

Point src = e.Location;
PointF ratio = new PointF((float)src.X / ClientSize.Width, (float)src.Y / ClientSize.Height);
LaunchOrigin.textratio = ratio;
Point origin = new Point((int)(backupbit1.Width * ratio.X), (int)(backupbit1.Height * ratio.Y));
LaunchOrigin.textorigin = origin;
point.X = src.X - origin.X;
point.Y = src.Y - origin.Y;

用于第二位图

Point src = e.Location;
PointF ratio = new PointF((float)src.X / Width, (float)src.Y / Height);
LaunchOrigin.logoratio = ratio;
Point origin = new Point((int)(backupbit2.Width * ratio.X), (int)(backupbit2.Height * ratio.Y));
LaunchOrigin.logoorigin = origin;
point2.X = src.X - origin.X;
point2.Y = src.Y - origin.Y;

这是返回到包含完整分辨率图像的主窗体的位置.为了正确转换(两个位图中的)两个点,我执行以下操作.

This is location is returned to the Main form which contains the full resolution image.In order to properly translate the 2 points (of the 2 bitmaps) i do the following.

Point origin = new Point((int)(bitmap.Width * textratio.X), (int)(bitmap.Height * textratio.Y));
Point pos2 = new Point((int)(textratio.X * img.Width), (int)(textratio.Y * img.Height));
cpoint.X = pos2.X - (int)(origin.X);
cpoint.Y = pos2.Y - (int)(origin.Y);

Point origin = new Point((int)(worktag.Width * logoratio.X), (int)(worktag.Height * logoratio.Y));
Point logopositionpoint = new Point((int)(logoratio.X * img.Width), (int)(logoratio.Y * img.Height));
imgpoint.X = logopositionpoint.X - origin.X;
imgpoint.Y = logopositionpoint.Y - origin.Y;

当将2个位图放置在较远的位置时,此效果很好,但是当将2个位图放置得更近且全分辨率图像的高度小于用于放置位图的参考图像时,这2个位图重叠

This works pretty fine when the 2 Bitmaps are placed at distant locations.But when the 2 bitmaps are placed closer to each other and the full resolution image becomes lesser in height than the reference image used for placing the bitmaps these 2 bitmaps overlap.

我做错了什么?还是我需要做一些重叠检测?

I'm i doing something wrong? Or do i need to do some overlap detection?

请提出建议.

推荐答案

我创建了一个适用于表单的示例代码.窗体具有图片框,可从按钮获取图像.如果解决方案可行,则可以更改它.表单具有组件(pictureBox,btnImage)

I crated a sample code which works on form. The form has picturebox that gets the image from the button. You can change it if the solution works. The Form has the componenets ( pictureBox, btnImage)

这是完整的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Image img1;
        Image img2;
        public Form1()
        {
            InitializeComponent();

            Image img = Image.FromFile(@"C:\pics\1.jpg");
            this.btnImage.Image = img;
            this.pcitureBox.AllowDrop = true;                
        }

        private void btnImage_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnPic = (Button)sender;
            btnPic.DoDragDrop(btnPic.Image, DragDropEffects.Copy);
        }

        private void picBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        int img1X = 0;
        int img1Y = 0;
        private void picBox_DragDrop(object sender, DragEventArgs e)
        {
            var point = this.PointToClient(new Point(e.X, e.Y));
            int X = point.X - pcitureBox.Left;
            int Y = point.Y - pcitureBox.Top;



                PictureBox picbox = (PictureBox)sender;
                Graphics g = picbox.CreateGraphics();
                if (img1 == null)
                {
                    img1 = (Image)e.Data.GetData(DataFormats.Bitmap);
                    img1X=X;
                    img1Y = Y;
                    g.DrawImage(img1, new Point(img1X, img1Y));

                    return;
                }
                else
                {
                    #region add img2
                    img2 = (Image)e.Data.GetData(DataFormats.Bitmap);

                    //img1 has standart 0,0 point u can change
                    Rectangle r1 = new Rectangle(img1X, img1Y,     img1.Width, img1.Height);
                    Rectangle r2 = new Rectangle(X, Y, img2.Width,   img2.Height);
                    Rectangle overlapRect = Rectangle.Intersect(r1, r2);

                    int img2X = X;
                    int img2Y = Y;
                    if (overlapRect.Width > 0 || overlapRect.Height > 0)
                    {
                        bool betweenX = overlapRect.X >= r1.X &&         overlapRect.X <= (r1.X + r1.Height);
                        bool betweenY = overlapRect.Y >= r1.Y &&     overlapRect.Y <= (r1.Y + r1.Width);

                        if (betweenX)
                        {
                            img2X = GetNewX(r1, r2);
                        }
                        else if (betweenY)
                        {
                            img2Y = GetNewY(r1, r2);
                        }
                        else
                        {
                            if (overlapRect.Width <= overlapRect.Height)
                            {
                                img2X = GetNewX(r1, r2);
                            }
                            else
                            {
                                img2Y = GetNewY(r1, r2);
                            }
                        }
                    }
                    g.DrawImage(img1, new Point(img2X, img2Y));
                    #endregion
                }

        }

        private int GetNewX(Rectangle r1, Rectangle r2)
        {
            if (r2.X < r1.X)
            {
                return r1.X - r2.Width;
            }
            else
            {
                return r1.X + r1.Width;
            }
        }
        private int GetNewY(Rectangle r1, Rectangle r2)
        {
            if (r2.Y < r1.Y)
            {
                return r1.Y - r2.Height;
            }
            else
            {
                return r1.Y + r1.Height;
            }
        }

    }
}

这篇关于对2个位图执行重叠检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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