locked
rezise image on upload RRS feed

  • Question

  • User-93558027 posted

    Hi have the following method to upload images but I want to resize every image uploaded to 90x40 can anyone help me to edit this code so this will happen cheers

     

    private string UploadFile(string pathToSave, FileUpload uploadfile)
            {
                string strFileName;
                string strFilePath;
                string strFolder;
                string file_version = "";
                strFolder = Server.MapPath("~/images/" + pathToSave);

                // Retrieve the name of the file that is posted.
                strFileName = uploadfile.PostedFile.FileName;
                strFileName = Path.GetFileName(strFileName);

                if (uploadfile.HasFile)
                {
                    // Create the folder if it does not exist.
                    if (!Directory.Exists(strFolder))
                    {
                        Directory.CreateDirectory(strFolder);
                    }
                    // Save the uploaded file to the server.
                    strFilePath = strFolder + strFileName;
                    int number = 1;
                    while (File.Exists(strFolder + file_version + strFileName))
                    {
                        file_version = number.ToString() + "_";
                        number++;
                    }
                    strFilePath = strFolder + file_version + strFileName;
                    //{
                    //    uploadfile.PostedFile.SaveAs(strFilePath);
                    //}
                    //Save normalized
                    System.Drawing.Image image = System.Drawing.Image.FromStream(uploadfile.PostedFile.InputStream);
                    double h = image.Height / 40D;
                    double w = image.Width / 90D;
                    double normalHeight = image.Height, normalWidth = image.Width;

                    if (h >= 1 && h >= w)
                    {
                        normalHeight = 40; normalWidth = normalWidth / h;
                    }
                    if (w >= 1 && h < w)
                    {
                        normalWidth = 100; normalHeight = normalHeight / w;
                    }

                    //using (Bitmap bitmap = new Bitmap(image, (int)Math.Ceiling(normalWidth), (int)Math.Ceiling(normalHeight)))
                    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(WebUtility.GenerateThumbnail(image, (int)Math.Ceiling(normalWidth), (int)Math.Ceiling(normalHeight)), (int)Math.Ceiling(normalWidth), (int)Math.Ceiling(normalHeight)))
                    {
                        //bitmap.Save(strFilePath, image.RawFormat);
                        bitmap.Save(strFilePath, WebUtility.GenerateThumbnail(image, (int)Math.Ceiling(normalWidth), (int)Math.Ceiling(normalHeight)).RawFormat);
                    }
                }
                return pathToSave + file_version + strFileName;
            }

    Friday, September 10, 2010 8:23 AM

All replies

  • User-93558027 posted

    Ive got the following code but geting the following errors can anyone help

    on fvClient.FindControl("Image1") as System.Drawing.Image;

    Cannot convert type 'System.Web.UI.Control' to 'System.Drawing.Image' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

     on UploadFile("../Images/client/", fileUpload);

     The best overloaded method match for 'JustClick.Site.Admin.Pages.ClientsManagement.UploadFile(object, System.EventArgs)' has some invalid arguments

     Argument '2': cannot convert from 'System.Web.UI.WebControls.FileUpload' to 'System.EventArgs'     

     on .ImageUrl = "~/images/" + hfLogo.Value;

     System.Drawing.Image' does not contain a definition for 'ImageUrl' and no extension method 'ImageUrl' accepting a first argument of type 'System.Drawing.Image' could be found (are you missing a using directive or an assembly reference?) LogoImage   

     

     <tr>
       <td>
           <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Logo")==null ? "" :"~/images/" + Eval("Logo").ToString()  %>' CssClass="border" />
       </td>
       <td>
           <asp:CheckBox ID="chkWorkingAbroad" runat="server" Text="WorkingAbroad" Checked='<%# Bind("WorkingAbroad") %>' /> 
           <br />
           <asp:FileUpload ID="fileUpload" runat="server" CssClass="input_nosize" />
           <asp:Button ID="btnUpload" runat="server" OnClick="UploadFile" Text="Upload" CssClass="input_button_face_nosize"
                Height="22px" CausesValidation="false" /><br/>
           Please try to use image with size 90 x 40.<br />
           <asp:Label ID="errorLabel" runat="server" />
          <br />
           Uploaded file<br/>
         <asp:TextBox ID="txtImagePath" runat="server" CssClass="input_nosize" ReadOnly="true"
                     Text='<%# Eval("Logo")==null ? "" : System.IO.Path.GetFileName(Eval("Logo").ToString()) %>' Width="310px"></asp:TextBox>
         </td>
     </tr>

     

    protected void UploadFile(Object s, EventArgs e)
            {
               
                FileUpload uploadfile = (FileUpload)fvClient.FindControl("fileUpload");
                //string strFileName = Server.MapPath("~/images/" + uploadfile.ToString());

                if (uploadfile.HasFile)
                {
                   // Find the fileUpload control
                   string filename = uploadfile.FileName;
                   
                    // Create the folder if it does not exist.
                    if (!Directory.Exists(Server.MapPath("~/images/Client/")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/images/Client/"));
                    }

                    // Specify the upload directory
                    string directory = Server.MapPath("~/images/Client/");
                                    
                    // Create a bitmap of the content of the fileUpload control in memory
                    Bitmap originalBMP = new Bitmap(uploadfile.FileContent);

                    // Calculate the new image dimensions
                    int origWidth = originalBMP.Width;
                    int origHeight = originalBMP.Height;
                    int newWidth = 90;
                    int newHeight = 40;               

                    // Create a new bitmap which will hold the previous resized bitmap
                    Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                   
                    // Create a graphic based on the new bitmap
                    Graphics oGraphics = Graphics.FromImage(newBMP);               

                    // Set the properties for the new graphic file
                    oGraphics.SmoothingMode = SmoothingMode.AntiAlias;oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;               
                  
                    // Draw the new graphic based on the resized bitmap
                    oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);               

                    // Save the new graphic file to the server
                    newBMP.Save(directory + filename);

                   // Once finished with the bitmap objects, we deallocate them.
                   originalBMP.Dispose();
                   newBMP.Dispose();
                   oGraphics.Dispose();
                }
             }

           
             protected void btnUpload_Click(object sender, EventArgs e)
             {
              
                 JustClick.BLL.Client client = JustClick.DAL.DataRepository.ClientProvider.GetByClientID(Convert.ToInt32(grvClients.SelectedValue));
                 TextBox txtImagePath = fvClient.FindControl("txtImagePath") as TextBox;
                 HiddenField hfLogo = fvClient.FindControl("hfLogo") as HiddenField;
                 FileUpload fileUpload = fvClient.FindControl("fileUpload") as FileUpload;

                 System.Drawing.Image LogoImage = fvClient.FindControl("Image1") as System.Drawing.Image;
                 hfLogo.Value = UploadFile("../Images/client/", fileUpload);
                 LogoImage.ImageUrl = "~/images/" + hfLogo.Value;
                 txtImagePath.Text = Path.GetFileName(fileUpload.PostedFile.FileName);            
              }

     

    Monday, September 13, 2010 9:35 AM