简体   繁体   中英

How to reduce image file, resize without losing quality using MultipleFileUpload in ASP.NET C#

I am uploading multiple photos using MultipleFileUpload, if I will upload big size images, then in slider image is not fixed size not showing proper looks. Is there any code for while uploading time restricts the size of images of the gallery.

Below is my c# code:

protected void lnkbtn_Submit_Click(object sender, EventArgs e)
{
    try
    {
        if (MultipleFileUpload.HasFiles)
        {
            int MaxGalleryId, ReturnValue;
            ReturnValue = obj.fnCreateNewPhotoGallery(txtGalleryName.Text, txtGalleryDescrption.Text, DateTime.Now, out MaxGalleryId);
            if (ReturnValue != 0)
            {
                string GalleryPath = System.Configuration.ConfigurationManager.AppSettings["GalleryPath"] + MaxGalleryId;
                Directory.CreateDirectory(Server.MapPath(GalleryPath));

                string ThumbnailPath = System.Configuration.ConfigurationManager.AppSettings["ThumbnailPath"] + MaxGalleryId;
                Directory.CreateDirectory(Server.MapPath(ThumbnailPath));

                StringBuilder UploadedFileNames = new StringBuilder();

                foreach (HttpPostedFile uploadedFile in MultipleFileUpload.PostedFiles)
                {
                    //Upload file
                    string FileName = HttpUtility.HtmlEncode(Path.GetFileName(uploadedFile.FileName));
                    string SaveAsImage = System.IO.Path.Combine(Server.MapPath(GalleryPath + "/"), FileName);
                    uploadedFile.SaveAs(SaveAsImage);

                    //Create thumbnail for uploaded file and save thumbnail on disk
                    Bitmap Thumbnail = CreateThumbnail(SaveAsImage, 200, 200);
                    string SaveAsThumbnail = System.IO.Path.Combine(Server.MapPath(ThumbnailPath + "/"), FileName);
                    Thumbnail.Save(SaveAsThumbnail);
                }
                HTMLHelper.jsAlertAndRedirect(this, "Gallery created successfully. ", "Album.aspx?GalleryId=" + MaxGalleryId);
            }
        }
    }

    catch
    {
        HTMLHelper.jsAlertAndRedirect(this, "Gallery is not created. Some exception occured ", "CreateAlbum.aspx");
    }
}

Below is my Create Thumbnail method code :

public Bitmap CreateThumbnail(string ImagePath, int ThumbnailWidth, int ThumbnailHeight)
{
    System.Drawing.Bitmap Thumbnail = null;
    try
    {
        Bitmap ImageBMP = new Bitmap(ImagePath);
        ImageFormat loFormat = ImageBMP.RawFormat;

        decimal lengthRatio;
        int ThumbnailNewWidth = 0;
        int ThumbnailNewHeight = 0;
        decimal ThumbnailRatioWidth;
        decimal ThumbnailRatioHeight;

        // If the uploaded image is smaller than a thumbnail size the just return it
        if (ImageBMP.Width <= ThumbnailWidth && ImageBMP.Height <= ThumbnailHeight)
            return ImageBMP;

        // Compute best ratio to scale entire image based on larger dimension.
        if (ImageBMP.Width > ImageBMP.Height)
        {
            ThumbnailRatioWidth = (decimal)ThumbnailWidth / ImageBMP.Width;
            ThumbnailRatioHeight = (decimal)ThumbnailHeight / ImageBMP.Height;
            lengthRatio = Math.Min(ThumbnailRatioWidth, ThumbnailRatioHeight);
            ThumbnailNewWidth = ThumbnailWidth;
            decimal lengthTemp = ImageBMP.Height * lengthRatio;
            ThumbnailNewHeight = (int)lengthTemp;
        }
        else
        {
            ThumbnailRatioWidth = (decimal)ThumbnailWidth / ImageBMP.Width;
            ThumbnailRatioHeight = (decimal)ThumbnailHeight / ImageBMP.Height;
            lengthRatio = Math.Min(ThumbnailRatioWidth, ThumbnailRatioHeight);
            ThumbnailNewHeight = ThumbnailHeight;
            decimal lengthTemp = ImageBMP.Width * lengthRatio;
            ThumbnailNewWidth = (int)lengthTemp;
        }
        Thumbnail = new Bitmap(ThumbnailNewWidth, ThumbnailNewHeight);
        Graphics g = Graphics.FromImage(Thumbnail);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, ThumbnailNewWidth, ThumbnailNewHeight);
        g.DrawImage(ImageBMP, 0, 0, ThumbnailNewWidth, ThumbnailNewHeight);

        ImageBMP.Dispose();
    }
    catch
    {
        return null;
    }

    return Thumbnail;
}

The above code there is a command line //Upload file from there uploading images. I used this example for the gallery: http://www.bugdebugzone.com/2013/10/create-dynamic-image-gallery-slideshow.html

You can the ContentLength property of uploadedFile as such:

if (uploadedFile.ContentLength > 1000000)
{
    continue;
}

ContentLength is the size in bytes of the uploaded file. https://msdn.microsoft.com/en-us/library/system.web.httppostedfile.contentlength(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM