简体   繁体   English

如何在 C# 中的 2 个图片框上画一条线?

[英]How to draw a line across 2 pictureboxes in C#?

I'm currently tasked to make a program to draw line between 2 pictures.我目前的任务是制作一个程序来在 2 张图片之间画线。 in the future the line will be rotated but still connected as 1 line.将来该线将旋转但仍连接为 1 条线。 for now trying to make the line connect between 2 pictures first.现在首先尝试使两条图片之间的线连接。 currently using 2 panels as the pictures and big picturebox set as transparent covering both of the panel as canvas for draw the lines but when doing test run the form are blank like in the picture.目前使用 2 个面板作为图片,大图片框设置为透明,覆盖两个面板作为画布用于绘制线条,但在进行测试运行时,表单是空白的,如图所示。 My question is.我的问题是。

  1. How to fix this blank error?如何修复这个空白错误? if this can be fixed then drawing the lines across pictures can be done on the big picturebox.如果可以解决此问题,则可以在大图片框上绘制跨图片的线条。
  2. if cant.如果不能。 is there any other method to draw lines / connected pictures across 2 pictureboxes?有没有其他方法可以在 2 个图片框上绘制线条/连接图片? thank you very much.非常感谢您。

I dont know the keyword to search that error.我不知道搜索该错误的关键字。 I tried using picturebox & panels but the result are same.我尝试使用图片框和面板,但结果是一样的。

Form with 2 panels as pictureboxes带有 2 个面板作为图片框的表单

带有 2 个面板作为图片框的表单

big picturebox covering the panel覆盖面板的大图框

覆盖面板的大图框

blank error result空白错误结果

空白错误结果

edit.编辑。 tried splitting the 1 big picturebox for 2 images.尝试将 1 个大图片框拆分为 2 个图像。 it works for the line drawing but the blank error for the buttons below is back.它适用于画线,但下面按钮的空白错误又回来了。

工作图片框但有空白错误

In your main code, load the images into memory.在您的主代码中,将图像加载到内存中。 This method keeps the files locked which can be avoided by loading them into a memorystream if desired.这种方法保持文件锁定,如果需要,可以通过将它们加载到内存流中来避免。

    Image image1 = Image.FromFile(@"C:/Users/RPC1940/Pictures/500px.jpg");
    Image image2 = Image.FromFile(@"C:/Users/RPC1940/Pictures/500px2.jpg");

Then on the picturebox paint event draw the images onto the main canvas and paint your line over top as well.然后在图片框绘制事件中,将图像绘制到主画布上,并在顶部绘制线条。 This doesn't take into account stretching, etc but should give you a start.这不考虑拉伸等,但应该给你一个开始。

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        int halfWidth = pictureBox1.Width / 2;
        e.Graphics.DrawImage(image1, new Rectangle(0, 0, halfWidth, pictureBox1.Height));
        e.Graphics.DrawImage(image2, new Rectangle(halfWidth + 1, 0, halfWidth, pictureBox1.Height));
        e.Graphics.DrawLine(Pens.Black, 140, 140, 300, 300);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM