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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:30:29+00:00 2026-05-16T12:30:29+00:00

I have created these functions which I described in the question. However I think

  • 0

I have created these functions which I described in the question. However I think the way I did it is not the optimal way of doing it.

        [HttpPost]
        public ActionResult Create(FormCollection collection, string schooljaarparam, FlatONASAanbieder foa) {

        if (ModelState.IsValid) {

            // var r = new List<ViewDataUploadFilesResult>();

            foreach (string file in Request.Files) {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;

                //extensie nakijken. jpg, png, jpeg, of GIF. 
                if (MvcApplication.isImage(hpf.FileName)) {
                    //Image img = new Image();


                    string savedFileName = Path.Combine(
                       AppDomain.CurrentDomain.BaseDirectory + "uploads\\ONAS\\",
                       Path.GetFileName(hpf.FileName));
                    FileInfo fi = new FileInfo(savedFileName);

                    int i = 1;
                    while (fi.Exists) {
                        fi = new FileInfo(savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + " (" + i++ + ") " + Path.GetExtension(savedFileName));
                    }
                    savedFileName = fi.DirectoryName + "\\" + fi.Name;
                    hpf.SaveAs(savedFileName);

                    using (Image Img = Image.FromFile(savedFileName)) {
                        //Size ThumbNailSize = NewImageSize(Img.Height, Img.Width, 79);
                        Size NewSize = VerkleinMaxHoogte(Img.Size, 79);

                        using (Image ImgThnail = new Bitmap(Img, NewSize.Width, NewSize.Height)) {
                            //string ss = savedFileName.Substring(0, savedFileName.Length - Path.GetFileName(savedFileName).Length) + Path.GetFileNameWithoutExtension(savedFileName) + "-thumb" + Path.GetExtension(savedFileName);
                            ImgThnail.Save(savedFileName + ".tmp", Img.RawFormat);
                            ImgThnail.Dispose();
                        }
                        Img.Dispose();
                    }
                    System.IO.File.Delete(savedFileName);
                    FileInfo f = new FileInfo(savedFileName + ".tmp");
                    f.MoveTo(savedFileName);


                } else {
                    ModelState.AddModelError("ONAS_Logo", "Het geuploadde bestand is geen afbeelding. ");

                }

                //r.Add(new ViewDataUploadFilesResult() {
                //    Name = savedFileName,
                //    Length = hpf.ContentLength
                //});
            }
        }

        // return View("UploadedFiles", r);

        return View();
    }


    [NonAction]
    public Size VerkleinMaxHoogte(Size orig, double height) {
        double tempval = height / orig.Height;

        return new Size(Convert.ToInt32(tempval * orig.Width), Convert.ToInt32(height));
    }

in global.asax

    public static bool isImage(string s) {
        if (s.EndsWith(".jpg", true, null) || s.EndsWith(".jpeg", true, null) || s.EndsWith(".gif", true, null) || s.EndsWith(".png", true, null)) {
            return true;
        }
        return false;
    }

so the way I do it:

  1. I get the file from the browser
  2. I check if it is an Image
  3. I check if the file exists, and if so, change the filename accordingly
  4. I save the file on disk (IO, slow)
  5. I open the file as an image
  6. I calculate the width and height with the VerkleinMaxHoogte method
  7. I create the thumbnail and save it with a tmp extension
  8. I delete the original file
  9. I rename the thumbnail to the original file name (this is what I want)

How do I do it faster?

  • 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-16T12:30:30+00:00Added an answer on May 16, 2026 at 12:30 pm

    You can always use HttpPostedFile.InputStream and Image.FromStream method to combine #4 & #5. This will also eliminate #8 & #9.

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

Sidebar

Related Questions

Basically I have created two MATLAB functions which involve some basic signal processing and
I have created HTTP handlers. How do I create global variables for these handlers
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
I have a folder with these files: alongfilename1.txt <--- created first alongfilename3.txt <--- created
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
I have created a debugger visualizer in VS2008. There are two classes i've made,
I have an ABC with several derived classes. To create these derived classes I
I have a class that creates several IDisposable objects, all of these objects are
Is there anyway I can create a not in clause like I would have
Have created a c++ implementation of the Hough transform for detecting lines in images.

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.