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 8081189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:39:18+00:00 2026-06-05T16:39:18+00:00

So I have a source bitmap that is fairly large, so I shrink it

  • 0

So I have a source bitmap that is fairly large, so I shrink it down by a scale of %25 to speed things along in image processing and what not. At the end, I have a group of Rectangles (around 2000) that correspond to sections of the scaled image. I am trying to rescale the Rectangles to match the same areas on the source, then saving that area as a cropped image.

This was my initial code that saved cropped images of the scaled image:

for (int i = 0; i < cells.Count; i++)
{
    for (int j = 0; j < cells[i].Count; j++)
    {
        Cell cell = cells[i][j];

        if (cell.width < 0 || cell.height < 0)
        {
            return;
        }

        Bitmap bitmap = new Bitmap(cell.width, cell.height);

        using (Graphics c = Graphics.FromImage(bitmap))
        {
            c.DrawImage(inputBitmap, new Rectangle(0, 0, cell.width, cell.height), new Rectangle(cell.x1, cell.y1, cell.width, cell.height), GraphicsUnit.Pixel);
        }

        bitmap.Save(cellDirectory + "\\cell" + i.ToString("D2") + j.ToString("D2") + ".png", ImageFormat.Png);
    }
}

This is my code changed to save the cropped image of the original bitmap:

for (int i = 0; i < cells.Count; i++)
{
    for (int j = 0; j < cells[i].Count; j++)
    {
        Cell cell = cells[i][j];

        if (cell.width < 0 || cell.height < 0)
        {
            return;
        }

        int x = cell.x1 * 4;
        int y = cell.y1 * 4;
        int width = cell.width * 4;
        int height = cell.height * 4;

        Bitmap bitmap = new Bitmap(width, height);

        using (Graphics c = Graphics.FromImage(bitmap))
        {
            c.DrawImage(input, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
        }

        bitmap.Save(cellDirectory + "\\cell" + i.ToString("D2") + j.ToString("D2") + ".png", ImageFormat.Png);
    }
}

The program with the first code finishes in about 20 seconds on average, but for some reason the second version takes over 6 minutes. My brain math might be lying to me, but that seems to be a disproportionate time increase.

The debugging that I have done so far has revealed to me that this line:

c.DrawImage(input, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);

is taking longer to complete over time. I suspect that some sort of memory leak could be causing this, but I have tried manually calling Dispose on every object I can and nothing helped. Is there some kind of under-the-hood thing that I should be aware of that is causing this?

  • 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-05T16:39:20+00:00Added an answer on June 5, 2026 at 4:39 pm

    Your original method saves a file at the original resolution, while the new method increases both width and height by a factor of 4, which is a 16x increase in the image size. The time difference (6 minutes vs 20 seconds) is roughly proportional:

    (6 * 60) / 20 = 18 times slower
    4 * 4         = 16 times the image size
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a source image in PNG with alpha information, that I scale with
I have some images that I loaded from a remote source stored in Bitmap
I have a source bitmap that is 1x1 and I am trying to take
I have an Image that I have bound the 'Source' property of to the
I have this method for shrinking down an image for a website that I'm
If I have a large bitmap file that would normally generate an Out of
I have source XMLfiles that come in with multiple root elements and there is
Have a source xml document that uses namespace containing prefixes and a default namespace.
I have a source XML file that is used to create C# files that
I have a source directory that includes a mix of different languages and files.

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.