简体   繁体   English

如何使用C#将位图分配给ASP.NET中的Image控件?

[英]How can I assign a bitmap to a Image control in asp.net using c#?

I have an asp.net file as follows: 我有一个asp.net文件,如下所示:

<body>
<form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    <div>
        <asp:Image ID="Image1" runat="server" />
    </div>
</form>

I want to set the Image1 in the c# using the following code: 我想使用以下代码在c#中设置Image1:

    protected void Button1_Click(object sender, EventArgs e)
{
    Bitmap oBmp1 = new Bitmap(460, 60);
    Graphics oGrp1 = Graphics.FromImage(oBmp1);
    Color ocolor = new Color();
    ocolor = Color.Yellow;
    SolidBrush oBrush = new SolidBrush(ocolor);
    SolidBrush oBrushWrite = new SolidBrush(Color.White);
    oGrp1.FillRectangle(oBrush, 100, 25, 500, 75);
    Response.ContentType = "image/jpeg";
    oBmp1.Save(Response.OutputStream, ImageFormat.Jpeg);
}

How can I directly set the image1 in c# after I generate the bitmap without saving bitmap as an image file? 生成位图后如何在c#中直接设置image1而又不将位图另存为图像文件?

Your above solution won't work. 您上面的解决方案无法正常工作。

You have to create a separate handler (.ashx) to return image. 您必须创建一个单独的处理程序(.ashx)以返回图像。 You will have to pass appropriate parameters as query string and link it to your Image control 您将必须传递适当的参数作为查询字符串,并将其链接到Image控件。

See this http://www.4guysfromrolla.com/articles/042209-1.aspx 看到这个http://www.4guysfromrolla.com/articles/042209-1.aspx

您可以将位图另存为任何文件夹中的图像文件,并使用图像控件找到图像文件路径以显示图像。

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

相关问题 C#:如何将Memory stream设置为asp.net图像控件的source属性? - C# :How can i set Memory stream as the asp.net image control's source property? ASP.NET C#如何将画布图像作为Bitmap传递给控制器 - ASP.NET C# How do I pass canvas image to controller as Bitmap 如何在 asp.net 核心 1.0 c# 中将字符串(实际上是位图)转换为 IFormFile - How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c# 如何使用 ASP.NET C# 动态地将图像添加到 Gridview 数据绑定中 - How can I add image into Gridview Databound dynamically using ASP.NET C# 如何在ASP.NET中将Image Web Control设置为Bitmap? - How to set Image Web Control into Bitmap in ASP.NET? 使用linq将多个查询分配给asp.net c#中的gridview控件 - multiple query assign to gridview control in asp.net c# using linq 如何在C#控件中创建ActiveX并在ASP.NET站点中引用它? - How can I create an activeX in C# control and reference it in a ASP.NET site? 无法使用 asp.net 和 CSS (C#) 居中图像 - Can not center image using asp.net and CSS (C#) 如何在 static 方法 c# 中为 asp.net 控件 ID 赋值 - how to assign a value to asp.net control id in static method c# 如何在ASP.Net C#中将连接字符串分配给ChangePassword控件 - How to Assign Connection String to ChangePassword control in ASP.Net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM