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

  • Home
  • SEARCH
  • 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 7043741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:19:36+00:00 2026-05-28T02:19:36+00:00

What I want to do is basically cropping a rectangle from an image. However,

  • 0

What I want to do is basically cropping a rectangle from an image. However, it should satisfy some special cases:

  1. I want to crop an angled rectangle on image.
  2. I don’t want to rotate the image and crop a rectangle 🙂
  3. If cropping exceeds the image size, I don’t want to crop an empty background color.

I want to crop from back of the starting point, that will end at starting point when rectangle size completed. I know I couldn’t explain well so if I show what I want visually:

enter image description here

The blue dot is the starting point there, and the arrow shows cropping direction. When cropping exceeds image borders, it will go back to the back of the starting point as much as, when the rectangle width and height finished the end of the rectangle will be at starting point.

Besides this is the previous question I asked:

  • How to crop a cross rectangle from an image using c#?

In this question, I couldn’t predict that a problem can occur about image dimensions so I didn’t ask for it. But now there is case 3. Except case three, this is exactly same question. How can I do this, any suggestions?

  • 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-05-28T02:19:36+00:00Added an answer on May 28, 2026 at 2:19 am

    What needs to be done is to add offsets to the matrix alignment. In this case I am taking one extra length of the rectangle from each side (total 9 rectangles) and offsetting the matrix each time.

    Notice that it is necessary to place offset 0 (the original crop) last, otherwise you will get the wrong result.

    Also note that if you specify a rectangle that is bigger than the rotated picture you will still get empty areas.

    public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality)
    {
        int[] offsets = { -1, 1, 0 }; //place 0 last!
        Bitmap result = new Bitmap(rect.Width, rect.Height);
        using (Graphics g = Graphics.FromImage(result))
        {
            g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default;
            foreach (int x in offsets)
            {
                foreach (int y in offsets)
                {
                    using (Matrix mat = new Matrix())
                    {
                        //create the appropriate filler offset according to x,y
                        //resulting in offsets (-1,-1), (-1, 0), (-1,1) ... (0,0)
                        mat.Translate(-rect.Location.X - rect.Width * x, -rect.Location.Y - rect.Height * y);
                        mat.RotateAt(angle, rect.Location);
                        g.Transform = mat;
                        g.DrawImage(source, new Point(0, 0));
                    }
                }
            }
        }
        return result;
    }
    

    To recreate your example:

    Bitmap source = new Bitmap("C:\\mjexample.jpg");
    Bitmap dest = CropRotatedRect(source, new Rectangle(86, 182, 87, 228), -45, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to automatically create a tiled image from a bunch of source
What i basically want to do is to get content from a website and
I want basically the same as jquery select image a row of images that
I basically want to use the XML parser from Qt in my existing project.
I basically want to show some php if it's inside the category work, I
I basically want to produce the following: from int 67 to 1 minute 7
I basically want to dynamically apply some style to all elements in a page,
I basically want to take two images taken from the camera on the iPhone
I basically want to learn how to extract a number from between 2 words...
I basically want the same functionality of preg_match_all() from PHP in a Python way.

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.