简体   繁体   English

像工具MS Paint C#这样的矩形选择

[英]Rectangle Selection Like Tool MS Paint C#

I have been working on creating a program similar to MS Paint. 我一直在创建类似于MS Paint的程序。 I have several of the features it has down but the one which is currently giving me trouble is the rectangular selection tool. 我有几个功能有所下降,但目前给我带来麻烦的一个是矩形选择工具。 My program currently draws everything on the panel and saves it all in an ArrayList so each shape can be redrawn in Paint(). 我的程序当前在面板上绘制所有内容并将其保存在ArrayList中,以便可以在Paint()中重绘每个形状。

Like MS paint I would like the user to be able to select a section of the drawing on the panel and either copy it, move it, re-size it, or even delete it. 像MS paint一样,我希望用户能够在面板上选择图形的一部分,然后对其进行复制,移动,调整大小,甚至删除。 I was thinking about having the user draw a rectangle & saving the information for it. 我正在考虑让用户绘制一个矩形并为其保存信息。 Then taking that information for the rectangle, passing them to create a new Bitmap. 然后,将该信息用于矩形,将其传递以创建新的位图。 I would then paint a new rectangle in the background color to give the appearance that the selected area was "removed" when the selected portion is moved. 然后,我将在背景色中绘制一个新的矩形,以使在移动选定部分时,选定区域被“删除”。 It sounded okay until I realized that I couldn't use the Graphics.FromImage() on the PaintEventArgs variable passed to Paint() which made my idea useless. 在我意识到无法使用传递给Paint()的PaintEventArgs变量上的Graphics.FromImage()之前,这听起来还不错,这使我的想法变得毫无用处。 Not sure if that makes sense so my apologies if it's a confusing mess. 不知道这是否有意义,因此如果造成混乱,我深表歉意。

I've been searching the internet for some assistance and I haven't found much to help so either this is very easy to do, very difficult, or "rectangle selection tool" is not the proper term. 我一直在互联网上寻求帮助,但没有找到太多帮助,因此这很容易做到,非常困难,或者“矩形选择工具”不合适。 Any help or pointers would be greatly appreciated!!! 任何帮助或指针将不胜感激! Thank you for your time! 感谢您的时间! :) :)

I understand that you actually have the Rectangle and now would like to copy an area from your painted Panel . 我了解您实际上拥有Rectangle ,现在想从已绘制的Panel复制一个区域。

This is possible, assuming you have, as you should, placed all the painting in the Paint event of the Panel . 这是有可能的,前提是您已将所有绘画都放置在PanelPaint事件中。

Then you can, use DrawToBitmap to ask the Panel to draw itself onto a new Bitmap ; 然后,您可以使用DrawToBitmap要求Panel将其自身绘制到新的Bitmap from there you can DrawImage the Rectangle onto your Panel . 从那里,你可以DrawImageRectangle到您的Panel

Note: For this to integrate with your list of 'Paint-Actions' you will have to either now store that Bitmap or store the Rectangle and redo the whole operation. 注意:要将此与您的“绘画动作”列表集成在一起,您现在必须存储该Bitmap或存储Rectangle然后重做整个操作。

using (Graphics G = panelCanvas.CreateGraphics() )
{
    Rectangle R0 = new Rectangle(22,22,55,55); // your Rectangle!
    using (Bitmap bmp = new 
           Bitmap(panelCanvas.ClientSize.Width, panelCanvas.ClientSize.Height))
    {    panelCanvas.DrawToBitmap(bmp, panelCanvas.ClientRectangle);
         G.DrawImage(bmp, 111f, 111f, R0, GraphicsUnit.Pixel);
    }
}

Aside: Please do replace the ArrayList , which is depracated by the new List<T> , eg a List<PaintAction> or whatever name your class has! 另外:请不要替换ArrayList ,它被新的List<T>取代,例如List<PaintAction>或您的班级名称!

If you simply want to extract a rectanglular area from the Panel Control you can use thsi function: 如果只想从面板控件中提取矩形区域,则可以使用以下函数:

public Bitmap getAreaFrom(Control ctl, Rectangle area)
{
    Bitmap bmp2 = new Bitmap(area.Width, area.Height);
    using (Graphics G = ctl.CreateGraphics())
    using (Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height))
    {
        ctl.DrawToBitmap(bmp, ctl.ClientRectangle);
        using (Graphics G2 = Graphics.FromImage(bmp2))
            G2.DrawImage(bmp, 0f, 0f, area, GraphicsUnit.Pixel);
    }
    return bmp2;
}

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

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