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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:56:16+00:00 2026-05-31T13:56:16+00:00

I have this code written in C#: int maxSideSize = 125; MemoryStream memory =

  • 0

I have this code written in C#:

            int maxSideSize = 125;
            MemoryStream memory = new MemoryStream( File.ReadAllBytes( Path.GetFullPath( "test1.png" ) ) );
            Image img = Image.FromStream( memory );

            //Determine image format 
            ImageFormat fmtImageFormat = img.RawFormat;

            //get image original width and height 
            int intOldWidth = img.Width;
            int intOldHeight = img.Height;

            //determine if landscape or portrait 
            int intMaxSide;
            if ( intOldWidth >= intOldHeight ) {
               intMaxSide = intOldWidth;
            } else {
               intMaxSide = intOldHeight;
            }

            if ( intMaxSide > maxSideSize ) {

               //set new width and height 
               double percent = maxSideSize / (double)intMaxSide;
               intNewWidth = Convert.ToInt32( percent * intOldWidth );
               intNewHeight = Convert.ToInt32( percent * intOldHeight );
            } else {
               intNewWidth = intOldWidth;
               intNewHeight = intOldHeight;
            }

            //create new bitmap 
            Bitmap bmpResized = new Bitmap( img, intNewWidth, intNewHeight );

            //save bitmap to disk 
            string path = Path.Combine( "C:\\Temp", test1.png" ) );

            bmpResized.Save( memory, fmtImageFormat );
            img.Save( path );

            //release used resources 
            img.Dispose();
            bmpResized.Dispose();
         } catch (Exception e) {
            Console.Write( e.Message );
         }

Can, the above code, be optimized for ASP.NET application ?

I think that if 1000 users are connected to my site, and maybe 20% of them upload an image with over 125px (as width and height) then the application may crash.

My friend recommended to use Canvas or Drawing2D library. What’s happens if there already exists a file ? It is possible to overwrite ?

Sorry for stupid question. I need advices in this case.

  • 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-31T13:56:17+00:00Added an answer on May 31, 2026 at 1:56 pm

    It is highly unlikely that 20% of users will upload an image in the same second. Of course you should make sure that you resize the image when it is uploaded and NOT when it is served. What is more if your server cannot do the work it will not crash but will put the users in the queue which will lead to slow response times.

    Keep in mind that if you let anonymous users upload pictures you could be a subject of DOS attack and no amount of optimization can prevent that. You should add code that actively checks if the upload is legitimate and throttle new uploads when there are problems.

    Finally if the code is really what you describe you do not need the memory stream at all you can read directly from the file when you create a bitmap and save directly to the file when you save a bitmap.

    You can load the file with this constructor overload and save it with this Save method overload

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

Sidebar

Related Questions

I have this code written for a Project Euler problem in c++: int sum
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have this code in my ASP.NET application written in C# that is trying
In our application, I have seen code written like this: User.java (User entity) public
This is similar to this question, except the code is already written. I have
I have always written my boolean expressions like this: if (!isValid) { // code
I have written this code but it will print these stack traces in the
I have written this piece of code public class Test{ public static void main(String[]

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.