简体   繁体   中英

how to display image created dynamically in asp.net 3.5 c# on localhost

I have to display image dynamically. I have written code for that, i got the name of the image & path also from database ie Filename & filepath which i stored earlier, but I didnt get image(not displaying image even i stored path of that image). Please give me idea about how to set imageurl of image here on local host. My code is as follow:

//PlaceHolder for adding images i.e inside formview control 
PlaceHolder PHFilename = (PlaceHolder)FVViewCustData.FindControl("PHFilename");
for (int i = 0; i < dsfile.Tables[0].Rows.Count; i++)
{
  HyperLink hypname = new HyperLink();
  hypname.Text = Convert.ToString(dsfile.Tables[0].Rows[i]["FileName"]);
  PHFilename.Controls.Add(hypname);

  Image img = new Image();
  //IPAddress is my ipaddress
  img.ImageUrl = "IPAddress" + Convert.ToString(
                     dsfile.Tables[0].Rows[i]["FilePath"]);

  PHFilename.Controls.Add(img);
}

If you keep the Image data in DB you can use image handler and load dynamically.

But if you keep the image URL in DB depends on what do you save filename or relative path or absolute path.

C# Save and Load Image from Database

There are couple of ways to solve your issues:

First Way : Store all your images under some base directory - you can have sub-directories under it. The image path that you store in the database will be relative to this base directory. Now map this base directory as some virtual directory in your web site. Now you can use code such as

string imageVirtualDir = "/StoredImages/";
img.ImageUrl = imageVirtualDir  + Convert.ToString(
                     dsfile.Tables[0].Rows[i]["FilePath"]);

Needless to say, base directory path and image virtual directory path will be configurable values.

Second Way : You can store images wherever you wish. Let's say you get the complete physical path from the database. So you will set the image url such as

img.ImageUrl = "/ImageServer.ashx?path=" + Convert.ToString(
                     dsfile.Tables[0].Rows[i]["FilePath"]);

Here, you will need a simple http handler (ImageServer.ashx) to serve your images - the code will be something like

public class ImageServer : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var path = context.Request["path"];
        context.Response.ContentType = "image/jpeg";
        context.Response.TransmitFile(path);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Note that this is just a template code to get you started. For production code, you need to have error handling, setting correct content type as per image type. Also, its not advisable to have physical image path in query string - so you need to pass either relative path or some token instead - for example, you can pass the row id so that your handler can query database and get the image path etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM