locked
Resize photos on upload with multiple photos. RRS feed

  • Question

  • User567490574 posted

    I am using Slide.Show and have everything working great except I have not found a good example to help me optimize/resize my images. I would like to set thier size to 400 by 400 and possibly put a water mark on them. The person the site is for is not going to take the time to optimize/resize all of the images they upload so I dont want to find out in a year he filled up his file system on the server and have to spend countless hours going through hundres of photos.  Can someone please help me understand how to use GZip or the System.Drawing class or whatever may be the best option. From what I have found the GetThumbnailImage() method is not very good. I would like to be able to optimize/resize the images before they are saved but Ill take anything right now! Thanks in advance for your help!

     

    //Get files from uploaders and go through them uploading then and storing the file names in the database.

    HttpFileCollection uploads = HttpContext.Current.Request.Files;
    for (int i = 0; i < uploads.Count; i++)

    {

    HttpPostedFile upload = uploads[i];

    if (upload.ContentLength == 0)

    continue;

    imageName = System.IO.Path.GetFileName(upload.FileName); // I don't need the path, just the name.

    //Try uploading the Images into the folders first.

    try

    {

    String ext;

    int change = 0;

    ext = System.IO.Path.GetExtension(upload.FileName);

    imageName = System.IO.Path.GetFileName(upload.FileName);

    //Check to see if the file name exists, if it does append the counter.

    if (!File.Exists(Server.MapPath("~/Photos/" + imageName)) & !File.Exists(Server.MapPath("~/JobImagesBackup/" + imageName)))

    {

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    errorLabel.Text = "Upload(s) Successful.";

    }

    else

    {

    //Append a number until the file name does not exist.

    while (File.Exists(Server.MapPath("~/Photos/" + imageName)) & File.Exists(Server.MapPath("~/PhotosBackup/" + imageName)))

    {

    change++;

    imageName = System.IO.
    Path.GetFileNameWithoutExtension(upload.FileName) + change.ToString() + ext;

    }

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    errorLabel.Text = "Upload(s) Successful.";

    //errorLabel.Text = "Upload(s) Successful With Appended Number.";

    }

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Upload(s) FAILED putting images into folders. " + Exp.Message.ToString();

    }

    //Try inserting the file names into the database

    try

    {

    PhotoID =
    Convert.ToInt16(DropDownList5.SelectedValue.ToString());PhotoBLL PhotoLogic = new PhotoBLL();

    PhotoLogic.AddImages(PhotoID, PhotoName);

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Error Inserting file names into database. " + Exp.Message.ToString();

    }

    }

    Response.Redirect(Request.Url.ToString());

    }

     

    }

    Saturday, December 20, 2008 6:26 PM

Answers

  • User-158764254 posted

    take a look at the sample at the end of this thread: http://forums.asp.net/t/1357517.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2008 6:32 PM
  • User567490574 posted

    Hi Mike,

     Thank you so much for that link. The funny thing is I had already had in at one time almost identical code, in C# of course but I am still getting the same error as I did before. Its giving me a " A generic error occurred in GDI+." error. And I am thinking it is because I want to overwrite my origonal image with the thumbnail image. I dont need both images. Here is my code now. NOTE: I have changed the folder names so dont worry if I mispelled one of those. I also didnt worry about putting the thumbnail image into a backup folder yet either. Thanks again for your help!

    protected void photoUpload_Click(object sender, EventArgs e)

    {

    if (DropDownList100.SelectedIndex == 0)

    {

    errorLabel.Visible =
    true;errorLabel.Text = "You must select a albulm from the drop down.";

    }

    else

    {

    //Try creating the folders if they do not exist.

    try

    {

    String UpPath;

    String UpPathBackup;

    UpPath = Server.MapPath("~/Photos/");

    UpPathBackup = Server.MapPath("~/PhotosBackup/");

    if (!Directory.Exists(UpPath))

    {

    Directory.CreateDirectory(Server.MapPath("~/Photos/"));

    }

    if (!Directory.Exists(UpPathBackup))

    {

    Directory.CreateDirectory(Server.MapPath("~/PhotosBackup/"));

    }

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Error Creating Folders." + Exp.Message.ToString();

    }

    //Get files from uploaders and go through them uploading then and storing the file names in the database.

    HttpFileCollection uploads = HttpContext.Current.Request.Files;for (int i = 0; i < uploads.Count; i++)

    {

    HttpPostedFile upload = uploads[i];

    if (upload.ContentLength == 0)

    continue;

    imageName = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.

    //Try uploading the Images into the folders first.

    try

    {

    String ext;

    int change = 0;

    ext = System.IO.Path.GetExtension(upload.FileName);

    imageName = System.IO.Path.GetFileName(upload.FileName);

    //Check to see if the file name exists, if it does append the counter.

    if (!File.Exists(Server.MapPath("~/Photos/" + imageName)) & !File.Exists(Server.MapPath("~/PhotosBackup/" + imageName)))

    {

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    errorLabel.Text = "Upload(s) Successful.";

    }

    else

    {

    //Append a number until the file name does not exist.

    while (File.Exists(Server.MapPath("~/Photos/" + imageName)) & File.Exists(Server.MapPath("~/PhotosBackup/" + imageName)))

    {

    change++;

    imageName = System.IO.
    Path.GetFileNameWithoutExtension(upload.FileName) + change.ToString() + ext;

    }

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    //Create Thumbnails of the origonal images

    //Open the original image

    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("~/Photos/" + imageName));

    //Get the original image bounds (size)

    System.Drawing.GraphicsUnit pixel = System.Drawing.GraphicsUnit.Pixel;

    //RectangleF originalBounds = originalImage.GetBounds(System.Drawing.GraphicsUnit.Pixel);

    RectangleF originalBounds = originalImage.GetBounds(ref pixel);

    //Calculate the thumbnail size (apply the formulas that you want, for this

    //example I simply divide by 4)

    RectangleF thumbnailBounds = new RectangleF(0, 0, originalBounds.Width / 4, originalBounds.Height / 4);

    //Create a new empty bitmap for holding the thumbnail

    //with the new calculated size

    Bitmap thumbnailImage = new Bitmap((int)thumbnailBounds.Width, (int)thumbnailBounds.Height);

    //Get a graphics object for the bitmap

    Graphics g = Graphics.FromImage(thumbnailImage);

    //Apply high quality properties for the graphics object

    // ERROR: Not supported in C#: WithStatement

    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

    //Draw the original image scaled into the graphics object

    g.DrawImage(originalImage, thumbnailBounds, originalBounds, GraphicsUnit.Pixel);

    //Dispose the graphics object (free memory)

    g.Dispose();

    //Save the thumbnail image (into response object or disk filename)

    thumbnailImage.Save(Server.MapPath("~/Photos/" + imageName));

    //Dispose the both thumbnail and original images

    thumbnailImage.Dispose();

    originalImage.Dispose();

    errorLabel.Text =
    "Upload(s) Successful.";

    //errorLabel.Text = "Upload(s) Successful With Appended Number.";

    }

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Upload(s) FAILED putting images into folders. " + Exp.Message.ToString();

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 2:14 PM

All replies

  • User-158764254 posted

    take a look at the sample at the end of this thread: http://forums.asp.net/t/1357517.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 20, 2008 6:32 PM
  • User567490574 posted

    Hi Mike,

     Thank you so much for that link. The funny thing is I had already had in at one time almost identical code, in C# of course but I am still getting the same error as I did before. Its giving me a " A generic error occurred in GDI+." error. And I am thinking it is because I want to overwrite my origonal image with the thumbnail image. I dont need both images. Here is my code now. NOTE: I have changed the folder names so dont worry if I mispelled one of those. I also didnt worry about putting the thumbnail image into a backup folder yet either. Thanks again for your help!

    protected void photoUpload_Click(object sender, EventArgs e)

    {

    if (DropDownList100.SelectedIndex == 0)

    {

    errorLabel.Visible =
    true;errorLabel.Text = "You must select a albulm from the drop down.";

    }

    else

    {

    //Try creating the folders if they do not exist.

    try

    {

    String UpPath;

    String UpPathBackup;

    UpPath = Server.MapPath("~/Photos/");

    UpPathBackup = Server.MapPath("~/PhotosBackup/");

    if (!Directory.Exists(UpPath))

    {

    Directory.CreateDirectory(Server.MapPath("~/Photos/"));

    }

    if (!Directory.Exists(UpPathBackup))

    {

    Directory.CreateDirectory(Server.MapPath("~/PhotosBackup/"));

    }

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Error Creating Folders." + Exp.Message.ToString();

    }

    //Get files from uploaders and go through them uploading then and storing the file names in the database.

    HttpFileCollection uploads = HttpContext.Current.Request.Files;for (int i = 0; i < uploads.Count; i++)

    {

    HttpPostedFile upload = uploads[i];

    if (upload.ContentLength == 0)

    continue;

    imageName = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.

    //Try uploading the Images into the folders first.

    try

    {

    String ext;

    int change = 0;

    ext = System.IO.Path.GetExtension(upload.FileName);

    imageName = System.IO.Path.GetFileName(upload.FileName);

    //Check to see if the file name exists, if it does append the counter.

    if (!File.Exists(Server.MapPath("~/Photos/" + imageName)) & !File.Exists(Server.MapPath("~/PhotosBackup/" + imageName)))

    {

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    errorLabel.Text = "Upload(s) Successful.";

    }

    else

    {

    //Append a number until the file name does not exist.

    while (File.Exists(Server.MapPath("~/Photos/" + imageName)) & File.Exists(Server.MapPath("~/PhotosBackup/" + imageName)))

    {

    change++;

    imageName = System.IO.
    Path.GetFileNameWithoutExtension(upload.FileName) + change.ToString() + ext;

    }

     

    upload.SaveAs(Server.MapPath(
    "~/Photos/" + imageName));

    upload.SaveAs(Server.MapPath("~/PhotosBackup/" + imageName));

    //Create Thumbnails of the origonal images

    //Open the original image

    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("~/Photos/" + imageName));

    //Get the original image bounds (size)

    System.Drawing.GraphicsUnit pixel = System.Drawing.GraphicsUnit.Pixel;

    //RectangleF originalBounds = originalImage.GetBounds(System.Drawing.GraphicsUnit.Pixel);

    RectangleF originalBounds = originalImage.GetBounds(ref pixel);

    //Calculate the thumbnail size (apply the formulas that you want, for this

    //example I simply divide by 4)

    RectangleF thumbnailBounds = new RectangleF(0, 0, originalBounds.Width / 4, originalBounds.Height / 4);

    //Create a new empty bitmap for holding the thumbnail

    //with the new calculated size

    Bitmap thumbnailImage = new Bitmap((int)thumbnailBounds.Width, (int)thumbnailBounds.Height);

    //Get a graphics object for the bitmap

    Graphics g = Graphics.FromImage(thumbnailImage);

    //Apply high quality properties for the graphics object

    // ERROR: Not supported in C#: WithStatement

    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

    //Draw the original image scaled into the graphics object

    g.DrawImage(originalImage, thumbnailBounds, originalBounds, GraphicsUnit.Pixel);

    //Dispose the graphics object (free memory)

    g.Dispose();

    //Save the thumbnail image (into response object or disk filename)

    thumbnailImage.Save(Server.MapPath("~/Photos/" + imageName));

    //Dispose the both thumbnail and original images

    thumbnailImage.Dispose();

    originalImage.Dispose();

    errorLabel.Text =
    "Upload(s) Successful.";

    //errorLabel.Text = "Upload(s) Successful With Appended Number.";

    }

    }

    catch (Exception Exp)

    {

    errorLabel.Text =
    "Upload(s) FAILED putting images into folders. " + Exp.Message.ToString();

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 21, 2008 2:14 PM