Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对2个位图执行重叠检测_C#_.net_Bitmap_Gdi+_System.drawing - Fatal编程技术网

C# 对2个位图执行重叠检测

C# 对2个位图执行重叠检测,c#,.net,bitmap,gdi+,system.drawing,C#,.net,Bitmap,Gdi+,System.drawing,我有一个自定义picturebox控件,允许在主图像上分别拖动2个位图,从而允许用户选择2个位图的位置 用于第一个位图 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)(backupbit

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

用于第一个位图

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;
用于第二个位图

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;
这是返回到包含全分辨率图像的主窗体的位置

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;
当这两个位图放置在较远的位置时,这种方法非常有效。但是,当这两个位图彼此靠近并且全分辨率图像的高度小于用于放置位图的参考图像时,这两个位图会重叠

我做错什么了?或者我需要做一些重叠检测吗


请给出建议。

我用板条箱装了一个在表单上工作的示例代码。表单具有picturebox,可从按钮获取图像。如果解决方案有效,您可以更改它。表单包含组件(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;
            }
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
图像img1;
图像img2;
公共表格1()
{
初始化组件();
Image img=Image.FromFile(@“C:\pics\1.jpg”);
this.btnImage.Image=img;
this.pcitureBox.AllowDrop=true;
}
私有void btnImage\u MouseDown(对象发送方,MouseEventArgs e)
{
按钮btnPic=(按钮)发送器;
btnPic.DoDragDrop(btnPic.Image,DragDropEffects.Copy);
}
私有无效picBox_DragEnter(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(DataFormats.Bitmap))
{
e、 效果=DragDropEffects.Copy;
}
其他的
{
e、 效果=DragDropEffects。无;
}
}
int img1X=0;
int img1Y=0;
私有void picBox_DragDrop(对象发送方,DragEventArgs e)
{
var point=此.PointToClient(新点(e.X,e.Y));
int X=点X-pcitureBox.左;
int Y=点Y-pcitureBox.Top;
PictureBox picbox=(PictureBox)发送方;
Graphics g=picbox.CreateGraphics();
如果(img1==null)
{
img1=(Image)e.Data.GetData(DataFormats.Bitmap);
img1X=X;
img1Y=Y;
g、 DrawImage(img1,新点(img1X,img1Y));
返回;
}
其他的
{
#区域添加img2
img2=(Image)e.Data.GetData(DataFormats.Bitmap);
//img1的标准为0,0点u可以更改
矩形r1=新矩形(img1X,img1Y,img1.宽度,img1.高度);
矩形r2=新矩形(X,Y,img2.宽度,img2.高度);
矩形重叠矩形=矩形相交(r1,r2);
int img2X=X;
int img2Y=Y;
如果(重叠矩形宽度>0 | |重叠矩形高度>0)
{

bool betweenX=overlapRect.X>=r1.X&&overlapRect.X=r1.Y&&overlapRect.Y将检查并返回。