Hi,
I am using VS2005, C#.
Am trying to populate an image in Image(control) at run-time.
The image to be populated is not fixed. It is decided by the a criterion in the previous page.
Can anyone help me with code snippets.

Something like this?

private void SetLogo(string logoPath) {
                try {
                    pctLogo.Image = System.Drawing.Bitmap.FromFile(logoPath);
                }
                catch {
                }
        }

Something like this?

private void SetLogo(string logoPath) {
                try {
                    pctLogo.Image = System.Drawing.Bitmap.FromFile(logoPath);
                }
                catch {
                }
        }

Hi,
Please confirm if the variables used logoPath will contain the complete path of the image and pctLogo is an Image type variable.
Please confirm

Hi,
Please confirm if the variables used logoPath will contain the complete path of the image and pctLogo is an Image type variable.
Please confirm

logoPath is the complete path to the image.
pctLogo is a control of the type System.Windows.Forms.PictureBox, but the image-property is of the same type for every control. I think the code would also work if pctLogo would be a CommandButton ;)

logoPath is the complete path to the image.
pctLogo is a control of the type System.Windows.Forms.PictureBox, but the image-property is of the same type for every control. I think the code would also work if pctLogo would be a CommandButton ;)

Hi,

I am trying to do this on a web page wherein I don't have picturebox control.
Can i do it using the Image control in web page or do i need to do it using some other control

Ah, this question belongs in the ASP.NET forum instead of the C# forum. Please send those questions to the other forum in the future.

For html you need an <img ...> tag to display the image in the client browser.

Here is an example of the html from my project. The important thing in the runat="server":

<img id="imgNurses" runat="server" style="border-width: 0px;" alt="Nurses Say It's Great" src="/images/ontheroad-nurse-2.gif"/>

Now at runtime you can switch the image out in code if you have runat="server" set:

protected void Page_Load(object sender, EventArgs e)
    {
      imgNurses.Src = "/images/whatever.jpg";
    }

Ah, this question belongs in the ASP.NET forum instead of the C# forum. Please send those questions to the other forum in the future.

For html you need an <img ...> tag to display the image in the client browser.

Here is an example of the html from my project. The important thing in the runat="server":

<img id="imgNurses" runat="server" style="border-width: 0px;" alt="Nurses Say It's Great" src="/images/ontheroad-nurse-2.gif"/>

Now at runtime you can switch the image out in code if you have runat="server" set:

protected void Page_Load(object sender, EventArgs e)
    {
      imgNurses.Src = "/images/whatever.jpg";
    }

Hi,

Thnx for the help.
Actually I also need to read the image file name from a folder.
For example the folder name is images and the file name is whatever.jpg.
I will know only the folder name but i won't be knowing the image file name (whatever.jpg).
How will i read the image file name from this folder.

That depends on how you know what the image name is .. is it part of your query string or a field in the postback etc? Just set the img.Src = Request.QueryString["whatever"] .. or img.Src = textbox1.Text

That depends on how you know what the image name is .. is it part of your query string or a field in the postback etc? Just set the img.Src = Request.QueryString["whatever"] .. or img.Src = textbox1.Text

No it is not a part of a query string
The images are placed inside a folder.
These image files are subjected to changes(they can be removed from their also).
So i have to read the file names inside the folder.
How do i read the file names from a folder.

private void button1_Click(object sender, EventArgs e)
    {
      //string dir = Server.MapPath()
      string dir = @"C:\";
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
      System.IO.FileInfo[] files = di.GetFiles("*.jpg");
      foreach (System.IO.FileInfo file in files)
      {
        Console.WriteLine(file.FullName);
      }
    }
private void button1_Click(object sender, EventArgs e)
    {
      //string dir = Server.MapPath()
      string dir = @"C:\";
      System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
      System.IO.FileInfo[] files = di.GetFiles("*.jpg");
      foreach (System.IO.FileInfo file in files)
      {
        Console.WriteLine(file.FullName);
      }
    }

Hi,

I am using the following code:

try
        {
            
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\Documents and Settings\\Test1\\");
            System.IO.FileInfo[] files = di.GetFiles("*.jpg");
            foreach (System.IO.FileInfo file in files)
            {
                arr.Add(file);
            }
            
        }
        catch
        {}

I am trying to add the .jpg files in the mentioned directory (di).
But while debugging i found that the variable files is not picking up anything.
Plz suggest.

Are there any .jpg (not .jpeg) files in that directory? Try running that code on a windows form application in the same environment. If it works on a form application then we know the problem is something with IIS, if not then we need to look at other solutions. I try to avoid the filesystem in ASP.NET as much as possible. Are the extensions .jpg or .JPG? I don't know if .GetFiles() is case sensitive off hand.

Are there any .jpg (not .jpeg) files in that directory? Try running that code on a windows form application in the same environment. If it works on a form application then we know the problem is something with IIS, if not then we need to look at other solutions. I try to avoid the filesystem in ASP.NET as much as possible. Are the extensions .jpg or .JPG? I don't know if .GetFiles() is case sensitive off hand.

I have put the files in the collection.
I have used:
System.IO.FileInfo[] files = di.GetFiles();
It is reading means the files count is increasing.
I have checked it is not taking the file name instead it is taking wrong values.
Also how do i retrieve them back from the collection??

I have put the files in the collection.
I have used:
System.IO.FileInfo[] files = di.GetFiles();
It is reading means the files count is increasing.
I have checked it is not taking the file name instead it is taking wrong values.
Also how do i retrieve them back from the collection??

Hey,

I have done it man.
Thnx a lot for ur help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.