ASP.NET Image uploading with Resizing

前端 未结 14 1899
广开言路
广开言路 2020-11-30 01:27

I have an aspx page which will upload images to server harddisk from client pc

But now i need to change my program in such a way that it would allow me to resize the

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 01:47

    How to resize & Upload Image only for .jpg Extensions :
    In upload.aspx page

        
        
     
                            
    

    And upload.aspx.cs
    For resize

    /// 
    /// Created By Rajib Chowdhury Mob. 01766-306306; Web: http://onlineshoping.somee.com/
    /// Complete This Page Coding On January 05, 2014
    /// Programing C# By Visual Studio 2013 For Web
    /// Dot Net Version 4.5
    /// Database Virsion MSSQL Server 2005
    /// 
            public bool ResizeImageAndUpload(System.IO.FileStream newFile, string folderPathAndFilenameNoExtension, double maxHeight, double maxWidth)
            {
                try
                {
                    // Declare variable for the conversion
                    float ratio;
                    // Create variable to hold the image
                    System.Drawing.Image thisImage = System.Drawing.Image.FromStream(newFile);
                    // Get height and width of current image
                    int width = (int)thisImage.Width;
                    int height = (int)thisImage.Height;
                    // Ratio and conversion for new size
                    if (width > maxWidth)
                    {
                        ratio = (float)width / (float)maxWidth;
                        width = (int)(width / ratio);
                        height = (int)(height / ratio);
                    }
                    // Ratio and conversion for new size
                    if (height > maxHeight)
                    {
                        ratio = (float)height / (float)maxHeight;
                        height = (int)(height / ratio);
                        width = (int)(width / ratio);
                    }
                    // Create "blank" image for drawing new image
                    Bitmap outImage = new Bitmap(width, height);
                    Graphics outGraphics = Graphics.FromImage(outImage);
                    SolidBrush sb = new SolidBrush(System.Drawing.Color.White);
                    // Fill "blank" with new sized image
                    outGraphics.FillRectangle(sb, 0, 0, outImage.Width, outImage.Height);
                    outGraphics.DrawImage(thisImage, 0, 0, outImage.Width, outImage.Height);
                    sb.Dispose();
                    outGraphics.Dispose();
                    thisImage.Dispose();
                    // Save new image as jpg
                    outImage.Save(Server.MapPath(folderPathAndFilenameNoExtension + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                    outImage.Dispose();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
    

    And Button1_Click Event

            string filePath = "~\\Image\\";//your normal image path
            if (Page.IsValid)
            {
                HttpPostedFile myFile = ProductImage.PostedFile;//Get Slected Image
                int nFileLen = myFile.ContentLength;//Get slected Image Size
                string myimag = txtProductName.Text;//Get user input image name
                Guid ImageName = Guid.NewGuid();//get unique id
                if ((myFile != null) && (nFileLen > 1048576))
                {
                    LabelAddStatus.Text = "minimum size exceed"; //If file image size 1 MB above
                }
                else
                {
                    try
                    {
                        if (ProductImage.HasFile)
                        {
                            String fileExtension = System.IO.Path.GetExtension(ProductImage.FileName).ToLower();
                            String[] allowedExtensions = { ".jpg" };//Declare For Allowed Extension
                            for (int i = 0; i < allowedExtensions.Length; i++)
                            {
                                if (fileExtension == allowedExtensions[i])
                                {
                                    // Read file into a data stream
                                    byte[] myData = new Byte[nFileLen];
                                    myFile.InputStream.Read(myData, 0, nFileLen);
                                    myFile.InputStream.Dispose();
                                    // Save the stream to disk as temporary file. make sure the path is unique!
                                    System.IO.FileStream newFile
                                            = new System.IO.FileStream(Server.MapPath(filePath + "_temp.jpg"),
                                                                       System.IO.FileMode.Create);
                                    newFile.Write(myData, 0, myData.Length);
                                    bool success = ResizeImageAndUpload(newFile, filePath + ("thumbs"+myimag + ImageName), 100, 100);//Save image your thumb image path
                                    success = ResizeImageAndUpload(newFile, filePath + (myimag + ImageName), 768, 1024);//Save image your normal image path
                                    //delete the temp file.
                                    newFile.Close();
                                    System.IO.File.Delete(Server.MapPath(filePath + "_temp.jpg"));
                                    LabelAddStatus.Text = "File uploaded.";
                                }
                                else
                                {
                                    LabelAddStatus.Text = "Unable to accept file type..";
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //No Exception Message
                    }
                }
            }
    

    Thanks...

提交回复
热议问题