locked
How to use : System.Drawing.Image.FromFile() method RRS feed

  • Question

  • User1940824251 posted

    Hi ,

          i have a requirement , where i need to create thumbnails on the fly and save to sever. For this  iam using the

    system.drawing.image.fromfile() method, to get the width and height of image . Here is my code


    string filename = fuleuplaod1.postedFile.FileName;

    System.Drawing.Image img=System.Drawing.Image.FromFile(filename);



    but the problem is i have been getting the exception like File not found.

    The System.Drawing.Image.FromFile() method is accepting the absoulte path of image

    if i code like below, i didn't got any error and got the expected result

    string src="D:\\testimg.jpg"

    System.Drawing.Image img=System.Drawing.Image.FromFile(src);



    Pls suggest me how can i get the absoulte path from File Upload control  or  is there any other way can we get around without getting exception  by suppling the only filename without path to

    System.Drawing.Image.FromFile(filename) method.


    Regards

    Praveen.K

    Tuesday, July 6, 2010 1:07 AM

Answers

All replies

  • User-1618234021 posted

    System.Drawing.Image.FromFile(...) expects the physical file source on the server. Which means you have to upload the file on the server to some temp location. Then you can use the path in the FromFile function.You can save the file using:

    FileUpload1.PostedFile.SaveAs(.......)


    Following code works because there is a physical file on d: with the name testimg.jpg.

    string src="D:\\testimg.jpg"

    System.Drawing.Image img=System.Drawing.Image.FromFile(src);

    Tuesday, July 6, 2010 5:20 AM
  • User1940824251 posted

    Hi adeelehsan,

                             can't we get the absoulte path without saving images to server.


    Regards

    Praveen.k


    Tuesday, July 6, 2010 6:28 AM
  • User-1618234021 posted

    The server does not know about the path, unless the file is uploaded. Also the image object will be nothing or null unless the file is physically present on the server.

    You can temporarily upload the file, use it and then delete it.

    Tuesday, July 6, 2010 6:36 AM
  • User1211441112 posted

    Use System.Drawing.Image img=System.Drawing.Image.FromStream(fuleuplaod1.FileContent);

     

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, Server.MapPath("~/Pics/"));

    Wednesday, July 7, 2010 1:08 AM
  • User1940824251 posted

    Hi Vipin,

                 i am generating file upload control dynamically . so that  i could not get  fileupload.filecontent property.

    intially i'll have one file upload control on the page and one button(Add Pic) beside of that file upload control. If user wish

    to upload more than one file he'll click on  Add pic button, so that he get another fileupload control .He can add 5 images

    at a time and upload.

    To get the files i wrote the code as follow

    for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFile postedFile = Request.Files[i];
                if (postedFile.ContentLength > 0)

                 {

                          string filename = postedFile.FileName;
                       

                     
                        string dest = Server.MapPath("~/gallery/"+filename);                    


                        System.Drawing.Image img = System.Drawing.Image.FromStream(filename);
                        int srcWidth=img.Width;
                        int srcHeight=img.Height;
                        int _thumbnailSize = 150;
                        int width, height;
                        if (img.Width > img.Height)
                        {
                            width = _thumbnailSize;
                            height = img.Height * _thumbnailSize / img.Width;   
                        }
                        else
                        {
                            width = img.Width * _thumbnailSize / img.Height;
                            height = _thumbnailSize;
                        }
                        Bitmap target = new Bitmap(width, height);
                        Graphics graphics = Graphics.FromImage(target);
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        graphics.CompositingQuality = CompositingQuality.HighQuality;
                        graphics.InterpolationMode = InterpolationMode.High;
                        Rectangle rectDestination = new Rectangle(0, 0, width, height);
                        graphics.DrawImage(img, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
                        target.Save(dest);
                        target.Dispose();
                        img.Dispose();

                  }

     

    but the problem is iam getting the error at this line

    System.Drawing.Image img = System.Drawing.Image.FromFile(filename);

    Error: File doesn't exists

    so could u tell me how to set and get the ID's of fileupload controls which will be created dynamically


    Thanks & Regards

    Praveen.K


     

     

     

     




    Wednesday, July 7, 2010 2:03 AM
  • User1211441112 posted

    try this. This will help u 

    http://forums.asp.net/t/1565831.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 9, 2010 1:21 AM