如何在C#中的图像上绘制多个矩形? [英] How can I draw multiple rectangles over an image in C#?

查看:480
本文介绍了如何在C#中的图像上绘制多个矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个PictureBox,我想在图像的尖端绘制动态矩形,可以调整大小(如有必要)。我不会成为图像的一部分而是它将作为图像顶部的一层。



在线代码似乎没有任何帮助。请帮忙。



问候

Aman Chaurasia



什么我试过了:



我尝试在MouseDown,MouseMove,MouseUp和Paint事件下绘图,但似乎都没有。

Hello,

I have a PictureBox and I want to draw dynamic rectangles on tip of the image that can be resized (if necessary). I will not be a part of the image rather it will act as a layer on top of the image.

None of the codes online seem to help. Please help.

Regards
Aman Chaurasia

What I have tried:

I tried drawing under MouseDown, MouseMove, MouseUp and Paint events but none seem to have worked.

推荐答案

好的,我假设你在这里的WinForms世界。



你真正在做的是开发一个自定义控件 - 图片框的专业化,也支持形状的叠加。首先要注意的是,任何绘图都应该只在Paint方法中完成,当然不能在鼠标移动事件中完成。



它的工作方式是所有绘图都在绘制事件处理程序,当需要绘图时,你需要Invalidate()相关区域,这将调用一个新的绘画,如果你做得很好,你的绘画方法只需绘制在需要刷新的区域。



因此,从PictureBox派生一个新类开始,这将是PictureBox的专业化,因为我们正在扩展它的功能。首先要做的是覆盖OnPaint方法,确保调用base。()方法,因为这将绘制图片,但是我们可以添加自己的绘图代码来覆盖顶部。这是一个在图片上放置黑色正方形的示例:



OK, I'm assuming you're in the world of WinForms here.

What you really are doing is developing a custom control - a specialization of a picture box which also supports the overlay of shapes. First thing to note is that any drawing should only be done in the Paint method, certainly not in mouse movement events.

The way it works is all drawing goes in the paint event handler, and when drawing is needed you Invalidate() the area concerned, this will invoke a new paint, and if you do it well your paint method only need to draw in the area which needs refreshing.

So, start by deriving a new class from PictureBox, this is going to be the 'specialization' of PictureBox because we're extending it's functionality. The first thing to do is override the OnPaint method, make sure you call the base.() method, as this will draw the picture, but then we can add our own drawing code to go over the top. Here's an example that puts a black square over the picture:

public class Box : PictureBox
{
    protected override void OnPaint(PaintEventArgs pe)
    {
        // let the default draw the image
        base.OnPaint(pe);

        pe.Graphics.FillRectangle(System.Drawing.Brushes.Black, new System.Drawing.Rectangle(0, 0, Width - 100, Height - 100));
    }
}







这是一个起点。改变你的设计师代码来创建一个新的Box而不是一个新的PictureBox。



从这里,它只是添加逻辑来跟踪你的矩形,以及进行拖动 - 因此您需要拦截鼠标事件,确定何时发生跟踪(按住鼠标按钮),更改选择矩形的尺寸并发出Invalidate()s。一个好的控制也应该响应键盘。



控制开发是繁琐而难以完美的,但它只是一个继续前进并轻推它的情况当你熟悉所需的电话和回调时,正确的方向。




That's a starting point. Alter your designer code to create a new Box rather than a new PictureBox.

From here, it's just a case of adding the logic to keep track of your rectangles, and do the dragging - so you'll need to intercept the mouse events, determine when tracking occurs (mouse button held down), change the dimension of the choice rectangle and issue Invalidate()s. A good control should respond to the keyboard as well.

Control development is fiddly and hard to get perfect, but its just a case of keeping going and nudging it in the right direction when you get familiar with the calls and callbacks you need.


这篇关于如何在C#中的图像上绘制多个矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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