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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:15:29+00:00 2026-05-25T16:15:29+00:00

This is my code: Bitmap bmp = ImageManipulator.GetMyImageModified(bmp); Bitmap tempBMP = ImageManipulator.cropImage(bmp, rect); tempBMP

  • 0

This is my code:

Bitmap bmp = ImageManipulator.GetMyImageModified(bmp);
Bitmap tempBMP = ImageManipulator.cropImage(bmp, rect);
tempBMP = ImageManipulator.CopyToBpp(tempBMP, 1);

string bmpFilename = String.Format("File{0}.png", indexNum);
tempBMP.Save(bmpFilename, ImageFormat.Png); 

Now I’ve seen that for IDisposable objects is a best practice to use the using statement to dispose those object as soon as they are not needed anymore.
I want to follow this practise and so I need some help rewriting the above code:

using (Bitmap bmp = ImageManipulator.GetMyImageModified(bmp){

   Bitmap tempBMP = ImageManipulator.cropImage(bmp, rect); // bmp should be disposed after this line
   tempBMP = ImageManipulator.CopyToBpp(tempBMP, 1);

   string bmpFilename = String.Format("File{0}.png", indexNum);
   tempBMP.Save(bmpFilename, ImageFormat.Png); 
} // bmp is disposed here

This is my first attempt but it’s not perfect cause the bmp Bitmap is non disposed as soon as not needed anymore though in this specific example shouldn’t be mandatory to get it disposed so fast.

tempBitmap is more problematic because i cannot reassign the reference to a new Object inside the using statement that should surrond:

Bitmap tempBMP = ImageManipulator.cropImage(bmp, rect);

Indeed tempBMP reference becomes readonly after surrounding the above line with the using keyword.

Also tempBMP gets saved to a file and the Save operation should be asyncrhonous and then I don’t know the effects of a dispose as soond as the:

tempBMP.Save(bmpFilename, ImageFormat.Png); 

Get called.

If you can help me write better code, I’m all ears.

  • 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-25T16:15:29+00:00Added an answer on May 25, 2026 at 4:15 pm
    using (var sourceBmp = ...)
    using (var modifiedBmp = ImageManipulator.GetMyImageModified(sourceBmp))
    using (var croppedBmp = ImageManipulator.cropImage(modifiedBmp, rect))
    using (var finalBmp = ImageManipulator.CopyToBpp(croppedBmp, 1))
    {
        string bmpFilename = String.Format("File{0}.png", indexNum);
        finalBmp.Save(bmpFilename, ImageFormat.Png); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, this code does not actually draw my bitmap file... or show
I'm using this code for downloading image from server: public Bitmap getBitmap(String path){ URL
Consider this code for loading, modifying and saving a Bitmap image: using (Bitmap bmp
I've seen this code on the web: Bitmap grayscaleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas
I have the following code. QString fileName = QFileDialog::getSaveFileName( this, tr(Output Image file), (),
I resize a bitmap using the following code: FileOutputStream out = new FileOutputStream("/sdcard/mods.png"); Bitmap
i am trying to show a bmp in the screen using this code: (this
I'm trying to use this code to draw a Bitmap directly onto a PictureBox
We have a piece of code which saves a .Net System.Drawing.Bitmap to file. The
That is my code... for(int i=0;i<sitesList.getPdf().size();i++) { Bitmap bmp; URL url=null; InputStream is; ImageView

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.