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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:09:43+00:00 2026-05-27T08:09:43+00:00

I want to crop a bitmap with this function but the bitmap could be

  • 0

I want to crop a bitmap with this function but the bitmap could be smaller then the crop area so I want to get the bitmap bigger in that case.

As example I have a bitmap which is 200×250 and if I use the CropBitmap method with 250×250 I get an out of memory error. It should return a bitmap with 250×250 where the missing left 50px are filled with white.

How can I achieve that?

public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
{
    var rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);

    if(bitmap.Width < cropWidth || bitmap.Height < cropHeight)
    {
        // what now?
    }

    return bitmap.Clone(rect, bitmap.PixelFormat);
}
  • 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-27T08:09:44+00:00Added an answer on May 27, 2026 at 8:09 am

    Create a new Bitmap with the appropriate size. Then get a System.Drawing.Graphics and use it to create the white area and to insert the source image. Something like this:

        if (bitmap.Width < cropWidth && bitmap.Height < cropHeight)
        {
            Bitmap newImage = new Bitmap(cropWidth, cropHeight, bitmap.PixelFormat);
            using (Graphics g = Graphics.FromImage(newImage))
            {
                // fill target image with white color
                g.FillRectangle(Brushes.White, 0, 0, cropWidth, cropHeight);
    
                // place source image inside the target image
                var dstX = cropWidth - bitmap.Width;
                var dstY = cropHeight - bitmap.Height;
                g.DrawImage(bitmap, dstX, dstY);
            }
            return newImage;
        }
    

    Note, that I replaced the || in the outer if expression with &&. To make it work with ||, you have to calculate the source region and use another overload of Graphics.DrawImage

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

Sidebar

Related Questions

I want to mimic the trim behavior of Photoshop (crop the area that is
Want the function to sort the table by HP but if duplicate HPs then
I want to resize photos, crop photos, etc. That's all. But I need to
I want to create a crop function in an existing engine. This is what
I want to read a bitmap that represents a sprite, and crop a part
I want to crop images using the GD, but I want it to crop
I want to crop an image. Is it possible to do like this. And
I would like to crop an image but I want to retain the part
I want to crop a UIImage that the size of the UIImage is 640*960
I want to create a function that after an image upload, will allow a

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.