【问题标题】:How can I assign a bitmap to a Image control in asp.net using c#?如何使用 c# 将位图分配给 asp.net 中的图像控件?
【发布时间】:2017-11-09 04:30:48
【问题描述】:

我有一个 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>

我想使用以下代码在 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);
}

生成位图后如何在c#中直接设置image1而不将位图保存为图像文件?

【问题讨论】:

    标签: c# asp.net image bitmap


    【解决方案1】:

    您的上述解决方案不起作用。

    您必须创建一个单独的处理程序 (.ashx) 才能返回图像。您必须将适当的参数作为查询字符串传递并将其链接到您的图像控件

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

    【讨论】:

    • 如何创建单独的处理程序?
    • 查看修改后的答案
    【解决方案2】:

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

    【讨论】:

    • 生成位图后如何在c#中直接设置image1而不将位图保存为图像文件?
    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多