Drawing a resizeable opaque rectangle over a picture

  • 13 years ago
        I need to draw opaque rectangles over an existing image as part of a larger project.  I've tried a few approaches, but the only one I've been able to come up with that actually works is outlined below in code.  The problem is that it's slow.  I was hoping that someone might have a better idea as to how to go about this.

    Remember, what I need to draw over the window must be opaque, and the user must be able to resize it.  For simplicity's sake, I didn't expand the example code below to allow resizing, but I'm sure I could write that using the same concept.

    Basically what my code does is it makes a copy of the original image at startup.  Then every time the user moves the mouse over the image with the left mouse button down, the code creates a copy of the image and draws an opaque rectangle in it.  It then reassigns the new image to the picture box.

    ANY help or ideas are greatly appreciated.  I'm just getting started in C# and I'm SURE there's a better way to do this.  (By the way, I experimented with all sorts of stuff before posting here... I'm pretty much at my wits end to make this any better! -- THANKS!)

    To replicate my program just follow these instructions:

    Use all default settings unless otherwise noted.

    Create a new form with all default options.

    Drop a pictureBox into the form with:
      Image = Any image from your system.
      Events = MouseDown, MouseMove, MouseUp

    Paste this code into the form at the following points:

    Immediately after:
        public class Form1 : System.Windows.Forms.Form
        {
    add:
            private System.Drawing.Image img;
            private System.Drawing.Rectangle re;
            private System.Drawing.Brush hbr;
            private bool mbdown = false;

    Immediately after:
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
    add:
                img = (System.Drawing.Image)pictureBox1.Image.Clone();
                hbr = new SolidBrush(Color.FromArgb(128,Color.Yellow));

    replace the three event handlers with this code:
            private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
                mbdown = true;
                re.X = e.X;
                re.Y = e.Y;
            }

            private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
                if(mbdown == true) {
                    System.Drawing.Image timg = (System.Drawing.Image)img.Clone();
                    Graphics g = Graphics.FromImage(timg);
                    re.Width = e.X - re.X;
                    re.Height = e.Y - re.Y;
                    g.FillRectangle(hbr,re);
                    pictureBox1.Image = timg;
                }
            }

            private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
                mbdown = false;
            }




























































  • 13 years ago
    To make this a little easier for people to take a look at what I'm talking about, here's a link that will download a little .NET test project that just includes the code and a 36k picture that illustrates how I feel about the progress I've made so far on my own on this feature of the program:

    http://www.mediafire.com/?2wrojgdktoi

    If you use firefox 2.0.0.3 the download works fine, it just doesn't alert me when it's done downloading (which is just about instant as the file is 200k).

    If you really want to see this drawing method drag a**, then just pop a 500k or larger picture in the picture box.  BTW, don't set the "Stretch Image" property as I stripped out the code that converts the points for that for brevity.

    Thanks again!

    Help!

    -- Pete













Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay