简体   繁体   English

以编程方式从asp.net C#创建映像#

[英]Create image from file programmatically asp.net C#

I am writing project in asp.net C#. 我在asp.net C#中编写项目。 I want to create Image programmatically by the following code: 我想通过以下代码以编程方式创建Image:

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? 如何从文件创建图像并通过在.cs文件中编写代码将其显示在页面中?

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. 但后来从未对x做过任何事情。

ASP.NET uses composition, so it maintains a collection of controls, with each control also containing a collection of children nodes. ASP.NET使用组合,因此它维护一组控件,每个控件还包含一组子节点。 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的面板中,它就是

myPanel.Controls.Add(image);

Check out this article . 看看这篇文章

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);

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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