Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Resizing images on upload

Resizing images on upload

Scheduled Pinned Locked Moved ASP.NET
csharpsysadminhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AdamskiR
    wrote on last edited by
    #1

    Hi I currently have a form for people to fill in, with this they need to uplaod a profile picture of themselves. At the moment I have the following code which uploads the image, but i want to be able to resize the image to a certin size. Ive seen resizing help on the web, but most of these seem to be large/thumbnail codes and im getting a bit boggled!

    Boolean fileOK = false;
    String path = Server.MapPath("~/uploadedconsultantimages/");
    if (UploadImage.HasFile)
    {
    String fileExtension =
    System.IO.Path.GetExtension(UploadImage.FileName).ToLower();
    String[] allowedExtensions =
    { ".gif", ".png", ".jpeg", ".jpg" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
    if (fileExtension == allowedExtensions[i])
    {
    fileOK = true;
    }
    }
    }

            if (fileOK)
            {
                try
                {
                    UploadImage.PostedFile.SaveAs(path
                        + UploadImage.FileName);
                    FileUploaded.Text = "File uploaded!";
                }
                catch (Exception ex)
                {
                    FileUploaded.Text = "File could not be uploaded.";
                }
            }
            else
            {
                FileUploaded.Text = "Cannot accept files of this type.";
            }
    

    Any help would be appreciated. I working in c# .net2:) Thanks Adam

    G 1 Reply Last reply
    0
    • A AdamskiR

      Hi I currently have a form for people to fill in, with this they need to uplaod a profile picture of themselves. At the moment I have the following code which uploads the image, but i want to be able to resize the image to a certin size. Ive seen resizing help on the web, but most of these seem to be large/thumbnail codes and im getting a bit boggled!

      Boolean fileOK = false;
      String path = Server.MapPath("~/uploadedconsultantimages/");
      if (UploadImage.HasFile)
      {
      String fileExtension =
      System.IO.Path.GetExtension(UploadImage.FileName).ToLower();
      String[] allowedExtensions =
      { ".gif", ".png", ".jpeg", ".jpg" };
      for (int i = 0; i < allowedExtensions.Length; i++)
      {
      if (fileExtension == allowedExtensions[i])
      {
      fileOK = true;
      }
      }
      }

              if (fileOK)
              {
                  try
                  {
                      UploadImage.PostedFile.SaveAs(path
                          + UploadImage.FileName);
                      FileUploaded.Text = "File uploaded!";
                  }
                  catch (Exception ex)
                  {
                      FileUploaded.Text = "File could not be uploaded.";
                  }
              }
              else
              {
                  FileUploaded.Text = "Cannot accept files of this type.";
              }
      

      Any help would be appreciated. I working in c# .net2:) Thanks Adam

      G Offline
      G Offline
      Gavin Roberts
      wrote on last edited by
      #2

      http://www.peterprovost.org/archive/2003/05/29/516.aspx[^] I haven't played around with the Upload stuff yet so not sure whether you can access the uploaded file in memory and resize it before saving it. If not, then you simply need to use something like Boolean fileOK = false; String path = Server.MapPath("~/uploadedconsultantimages/"); if (UploadImage.HasFile) { String fileExtension = System.IO.Path.GetExtension(UploadImage.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); Bitmap source = Bitmap.FromFile(path + UploadImage.FileName); Bitmap result = new Bitmap(250,250); using(Graphics g = Graphics.FromImage((Image)result); g.DrawImage(source, 0, 0, 250, 250); result.Dispose(); source.Dispose(); result.Save(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; } This is untested, but basically "should" resize the uploaded image to 250x250.

      A 1 Reply Last reply
      0
      • G Gavin Roberts

        http://www.peterprovost.org/archive/2003/05/29/516.aspx[^] I haven't played around with the Upload stuff yet so not sure whether you can access the uploaded file in memory and resize it before saving it. If not, then you simply need to use something like Boolean fileOK = false; String path = Server.MapPath("~/uploadedconsultantimages/"); if (UploadImage.HasFile) { String fileExtension = System.IO.Path.GetExtension(UploadImage.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); Bitmap source = Bitmap.FromFile(path + UploadImage.FileName); Bitmap result = new Bitmap(250,250); using(Graphics g = Graphics.FromImage((Image)result); g.DrawImage(source, 0, 0, 250, 250); result.Dispose(); source.Dispose(); result.Save(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; } This is untested, but basically "should" resize the uploaded image to 250x250.

        A Offline
        A Offline
        AdamskiR
        wrote on last edited by
        #3

        Thanks for this, but im getting errors of the type or namespace could not be found for bitmap and graphics?

        S 1 Reply Last reply
        0
        • A AdamskiR

          Thanks for this, but im getting errors of the type or namespace could not be found for bitmap and graphics?

          S Offline
          S Offline
          Sandeep Akhare
          wrote on last edited by
          #4

          Add System.Drawing in your application

          Thanks and Regards Sandeep If you want something you never had, do something you have never done!

          A 1 Reply Last reply
          0
          • S Sandeep Akhare

            Add System.Drawing in your application

            Thanks and Regards Sandeep If you want something you never had, do something you have never done!

            A Offline
            A Offline
            AdamskiR
            wrote on last edited by
            #5

            Thanks, im still getting build errors though: Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)

            Bitmap source = Bitmap.FromFile(path + UploadImage.FileName);

            Thanks

            S 1 Reply Last reply
            0
            • A AdamskiR

              Thanks, im still getting build errors though: Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)

              Bitmap source = Bitmap.FromFile(path + UploadImage.FileName);

              Thanks

              S Offline
              S Offline
              Sandeep Akhare
              wrote on last edited by
              #6

              Bitmap source = (Bitmap)Bitmap.FromFile(path + UploadImage.FileName); try this Does it will help you ? Bitmap.FromFile(path + UploadImage.FileName); return type is Image Why you want to convert it into Bitmap What you want to do ?

              Thanks and Regards Sandeep If you want something you never had, do something you have never done!

              A 1 Reply Last reply
              0
              • S Sandeep Akhare

                Bitmap source = (Bitmap)Bitmap.FromFile(path + UploadImage.FileName); try this Does it will help you ? Bitmap.FromFile(path + UploadImage.FileName); return type is Image Why you want to convert it into Bitmap What you want to do ?

                Thanks and Regards Sandeep If you want something you never had, do something you have never done!

                A Offline
                A Offline
                AdamskiR
                wrote on last edited by
                #7

                Ill give it a go, what i want to do is allow the user to upload a picture of themselves which is then resized automatically before it is saved. The image is just getting saved to file rather than in a database. Thanks

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups