1

I am trying to display some image which are fetching from database using c# asp.net but i am getting some error.

Error:

Server Error in '/' Application.

C:\ASP project\Odiya_Doctor_Client\Odiya_Doctor_Client\ODIYA_Doctor_Admin\Upload\Banner\2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: C:\ASP project\Odiya_Doctor_Client\Odiya_Doctor_Client\ODIYA_Doctor_Admin\Upload\Banner\2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg

I am explaining my code below.

index.aspx:

<img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64("/ODIYA_Doctor_Admin/Upload/Banner/" + Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />

index.aspx.cs:

protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
        {
            Bitmap newImage = new Bitmap(newWidth, newHeight);
            System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath(imageDirectory));
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.HighQuality;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));

            }
            MemoryStream ms = new MemoryStream();
            newImage.Save(ms, ImageFormat.Gif);
            var base64Data = Convert.ToBase64String(ms.ToArray());
            return "data:image/gif;base64," + base64Data;
        }

Actually i want to access one image which is present inside c:\ASP\ODIYA_Doctor_Admin\Upload\Banner folder but here my inde.aspx page is inside c:\ASP\Odiya_Doctor_Client\Odiya_Doctor_Client folder. In the error message the path is coming C:\ASP project\Odiya_Doctor_Client\Odiya_Doctor_Client\ODIYA_Doctor_Admin\Upload\Banner\2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg and from this path i want to remove \Odiya_Doctor_Client\Odiya_Doctor_Client. So please help me to resolve this error.

20
  • why don't you include the image in your solution. Commented Jul 15, 2015 at 6:13
  • the path: /ODIYA_Doctor_Admin/Upload/Banner/ is the root folder of the solution, type the whole filepath: C:\....etc... Commented Jul 15, 2015 at 6:15
  • @Amit: it fetching the wrong path.Actually i want to move out from root folder and access image from another folder which is present inside c:/ASP project. Commented Jul 15, 2015 at 6:16
  • @Cageman: I know that but i dont want to use the whole path.Let me to edit the back end code of index page in my post. Commented Jul 15, 2015 at 6:17
  • you might be interestd in Path.Combine. Commented Jul 15, 2015 at 6:19

2 Answers 2

1

use fileupload control for file location instead of server.mappath because server.mappath always looking at your solution folder

private void getPicture()
 {
      string location = FileUpload1.PostedFile.FileName;
      resizeAndConvertToBase64(location, 100,100);
 }

 protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
    {
        Bitmap newImage = new Bitmap(newWidth, newHeight);
        System.Drawing.Image srcImage = imageDirectory;
        using (Graphics gr = Graphics.FromImage(newImage))
        {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));

        }
        MemoryStream ms = new MemoryStream();
        newImage.Save(ms, ImageFormat.Gif);
        var base64Data = Convert.ToBase64String(ms.ToArray());
        return "data:image/gif;base64," + base64Data;
    }
Sign up to request clarification or add additional context in comments.

Comments

0
I would suggest you to do this way:

1. Create a application setting:

      <appSettings >
        <add key="ImgFolderPath" value="c:\ASP\ODIYA_Doctor_Admin\Upload\Banner"/>
      </appSettings>
2. Change your aspx code:

    <img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64( Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />

3. Change your .cs page code:


protected string resizeAndConvertToBase64(string imageName, int newWidth, int newHeight)
            {
                Bitmap newImage = new Bitmap(newWidth, newHeight);
                string path = ConfigurationManager.AppSettings["ImgFolderPath"].ToString();
                System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path+"/"+imageName));
                using (Graphics gr = Graphics.FromImage(newImage))
                {
                    gr.SmoothingMode = SmoothingMode.HighQuality;
                    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));

                }
                    MemoryStream ms = new MemoryStream();
                    newImage.Save(ms, ImageFormat.Gif);
                    var base64Data = Convert.ToBase64String(ms.ToArray());
                    return "data:image/gif;base64," + base64Data;
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.