Nurlan
Nurlan

Reputation: 2960

Create image from file programmatically asp.net C#

I am writing project in asp.net C#. I want to create Image programmatically by the following code:

protected void Page_Load(object sender, EventArgs e)
        {
            Image image = new Image();
            image.ID = "image11";
            image.ImageUrl = "a.jpg";
            image.ImageAlign = ImageAlign.AbsMiddle;
            image.Visible = true;

        }

But nothing is displayed when I run the project. How to create image from file and display it in the page by writing code in .cs file?

Upvotes: 0

Views: 3294

Answers (3)

Pramod Gupta
Pramod Gupta

Reputation: 41

You will need to create panel and then you will have to add that image to panel.

Panel Panel1= new Panel();
form1.Controls.Add(image);

Upvotes: 0

kakridge
kakridge

Reputation: 2293

At this point, you have just created an image, but you haven't added it to a control or page context to be displayed. You essentially said

int x = 10;

but then never did anything with x.

ASP.NET uses composition, so it maintains a collection of controls, with each control also containing a collection of children nodes. You need to add the image to a container. For instance, if you want to add the image to a panel named myPanel, it would be

myPanel.Controls.Add(image);

Check out this article.

Upvotes: 1

Waqar Janjua
Waqar Janjua

Reputation: 6123

You have created an image control but you have not added it to your form. Write the below code to add the image control to your form.

form1.Controls.Add(image);

Upvotes: 1

Related Questions