Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile( Path.GetFullPath(file.FileName));
that gave me error i want to give path of any image which is loacted in my computer but i cant.any solution ??

What I have tried:

C#
var file = Request.Files[0];
                        //HttpPostedFileBase hpf = Request.Files[0];

                        if (file != null && file.ContentLength > 0)
                        {
                            //var fileName = Path.GetFileName(file.FileName);                        
                            var fileExtension = Path.GetExtension(file.FileName);
                            var allowedExtensions = new[] { ".bmp", ".png", ".jpg", "jpeg", ".gif" };
                            if (allowedExtensions.Contains(fileExtension))
                            {
                                //Delete files
                                var pathD = Server.MapPath("~/Avatar/1");
                                var images = Directory.GetFiles(pathD, CustomerID + ".*");
                                for (int i = 0; i < images.Length; i++)
                                    System.IO.File.Delete(Server.MapPath(("~/Avatar/1/") + Path.GetFileName(images[i])));

                                //Up files
                                var fileName = CustomerID + fileExtension;
                                var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
                                //var store = Path.GetFullPath(file.FileName);
                            
                                //System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
                                var test = Server.MapPath(file.FileName);
                                var test2 = Path.GetFullPath(file.FileName);
                                System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile( Path.GetFullPath(file.FileName));
                                int imageHeight = imageToBeResized.Height;
                                int imageWidth = imageToBeResized.Width;
                                int maxHeight = 400;
                                int maxWidth = 400;
                                imageHeight = (imageHeight * maxWidth) / imageWidth;
                                imageWidth = maxWidth;

                                if (imageHeight > maxHeight)
                                {
                                    imageWidth = (imageWidth * maxHeight) / imageHeight;
                                    imageHeight = maxHeight;
                                }

                                Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
                                System.IO.MemoryStream stream = new MemoryStream();
                                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                stream.Position = 0;
                                byte[] imasave = new byte[stream.Length + 1];
                                stream.Read(imasave, 0, imasave.Length);

                                var newImage = System.Drawing.Image.FromStream(stream);
                                newImage.Save(path);
Posted
Updated 2-May-16 19:05pm
v2

 
Share this answer
 
Comments
Rakib Ahmed 3-May-16 0:24am    
not working any other example
Raje_ 3-May-16 0:41am    
Did you try like this?
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(file.PostedFile.InputSteam);
Rakib Ahmed 3-May-16 1:06am    
Postedfile is not working that gave me error 'System.Web.HttpPostedFileBase' does not contain a definition for 'PostedFile' and no extension method 'PostedFile' accepting a first argument of type 'System.Web.HttpPostedFileBase' could be found (are you missing a using directive or an assembly reference?
You are not supposed to open a Image from Client machine. Instead save a copy in your hosted Directory and try to access it.

C#
string fileName = FileUpload1.FileName;
          var path = Path.Combine(Server.MapPath("~/Images/"), fileName); // I have a folder called 'Images' in my root directory, configure it for your need
          FileUpload1.SaveAs(path);
          System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(path);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900