本文介绍了如何在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.

推荐答案

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#中的图像上绘制多个矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:26