Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:10:32+00:00 2026-06-01T22:10:32+00:00

I am creating a image viewer sort of application. I am on Windows and

  • 0

I am creating a image viewer sort of application. I am on Windows and using .Net

In my app, I am trying to highlight a Particular area while dragging.
I have created a Rectangle.

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Point ptOld = new Point(0, 0);
Pen rectPen = new Pen(Brushes.White, 3);

protected override void OnPaint(PaintEventArgs e)
{
  Graphics dcPaint = e.Graphics;
  dcPaint.DrawRectangle(rectPen, areaRect);
}

Now I am dragging this rectangular area along with my mouse movements.

protected override void OnMouseMove(MouseEventArgs e)
{
 Point ptNew = new Point(e.X, e.Y);
 int dx = ptNew.X - ptOld.X;
 int dy = ptNew.Y - ptOld.Y;
 areaRect.Offset(dx, dy);
 MoveRect(ptNew);
 ptOld = ptNew;
}

Here I am trying to move this rect along with my mouse

void MoveRect(Point point)
{
 Graphics grfxClient = CreateGraphics();
 Rectangle tempRectangle = new Rectangle(areaRect.Left, areaRect.Top, areaRect.Width,   areaRect.Height);
 grfxClient.DrawRectangle(rectPen, tempRectangle);
 this.Invalidate();
 grfxClient.Dispose();
}

My Code till this point is working fine.
Now I would like to darken the INVERSE drag Area (The area which is outside the drag region), I mean the area which is within this Rectangle should gets highlighted while dragging.

Any idea how to proceed.

Thanks.

-Pankaj

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:10:33+00:00Added an answer on June 1, 2026 at 10:10 pm

    I suppose you can do it by creating a Region object that covers the outside of the rectangle and fill it with a semi-transparent SolidBrush to make it look darkened.

    You also don’t have to create a graphics and draw in OnMouseMove event, but just shift the rectangle and invalidate the surface of the control you are drawing on.

    The code I used looks more or less like this:

    Rectangle areaRect = new Rectangle(100,100, 300, 300);
    Point ptOld = new Point(0, 0);
    Pen rectPen = new Pen(Brushes.White, 3);
    
    //A new field with a semi-transparent brush to paint the outside of the rectangle
    Brush dimmingBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0));
    
    protected override void OnPaint(PaintEventArgs e)
    {
        Region outsideRegion = new System.Drawing.Region(e.ClipRectangle);
        outsideRegion.Exclude(areaRect);
        Graphics dcPaint = e.Graphics;
        dcPaint.FillRegion(dimmingBrush, outsideRegion);
        dcPaint.DrawRectangle(rectPen, areaRect);
    }
    
    protected override void OnMouseMove(MouseEventArgs e)
    {
        Point ptNew = new Point(e.X, e.Y);
        int dx = ptNew.X - ptOld.X;
        int dy = ptNew.Y - ptOld.Y;
        areaRect.Offset(dx, dy);
        ptOld = ptNew;
        this.Invalidate();
    }
    

    The method named MoveRect is not needed.

    It now seems to work as you wanted it to.

    Suggestions

    I also have some suggestions. You don’t have to use them, maybe they will be helpful for you.

    You haven’t written what kind of control you are using to draw on (or overriding Form methods and painting directly on it), but I suggest you to use a PictureBox control, create a custom control derived from it and override its events. This should make the painting process smooth and prevent flickering. To do it this way:

    • Create a new user control by selecting Add and User Control… and name a new control i.e. MyPictureBox
    • change the parent class of the control, so it should now contain the line:

      public partial class MyPictureBox : PictureBox
      
    • open file MyPictureBox.Designer.cs and comment out these lines:

      //this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      
    • copy the code I posted in this answer and add line base.OnPaint(e); and the beginning of OnPaint method

    • compile the project

    • now you should be able to open designer of your main form, drag MyPictureBox control from the toolbox and use it without additional code needed

    You also might consider changing the behaviour of the highlighted area, so mouse cursor was in the center of it. I suppose it would be more intuitive to the user.

    If you have any issues with the code, just write it in the comments and I’ll try to help :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a basic image browsing application using Silverlight. Depending on the user's
I am creating an image processing application in C# and trying to take advantage
I needed some suggestion from ya'll.. I'm creating a video image viewer app for
I'm trying to add a zoom feature for an image viewer control I'm creating.
I'm creating quite a cool image viewer but am stuck in one particular part:
I am creating an image dynamically using php. The image is being created if
I am creating an Image Processing app that does two image analysis functions. One
I'm creating an dynamic image, which creates headers on my page using PHPs GD-library.
I am creating a custom component that is an image viewer for a given
I am creating an image with a caption using the Imagick::newPseudoImage function as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.