如何使用 C# 从字节数组显示 Web 表单中的图像 [英] How to display image inside web form from Byte Array with C#

查看:19
本文介绍了如何使用 C# 从字节数组显示 Web 表单中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它所做的只是清除网页并绘制图像,因此表单的其余部分消失了!我需要在表单内显示图像.

This is my code, all it does is clear the web page and draw the image, so the rest of the form disappears! I need to show the image inside the form.

这是我用来显示图像的用户控件:

This is my User Control to display the images:

protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["ObjHoteles"] == null)
        {
            Label1.Text = "Por favor, primero seleccione un hotel para desplegar las fotos.";
        }
        else
        {
            if (!IsPostBack)
            {
                List<Byte[]> ArrayFotos = new List<Byte[]>();
                string NombreDelHotel = "";

                Hoteles Hotel1 = (Hoteles)Session["ObjHoteles"];
                NombreDelHotel = Hotel1.NombreHotel;

                ArrayFotos = Persistencia.PersistenciaFotos.FotosDeHotel(NombreDelHotel);
                Session["CantFotos"] = ArrayFotos.Count();

                Byte[] Foto = ArrayFotos[0];

                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "image/jpeg";
                Response.Expires = 0;
                Response.BinaryWrite(Foto);
                Session["NumFoto"] = 0;
            }
            else
            {
                List<Byte[]> ArrayFotos = new List<Byte[]>();
                string NombreDelHotel = "";

                Hoteles Hotel1 = (Hoteles)Session["ObjHoteles"];
                NombreDelHotel = Hotel1.NombreHotel;

                ArrayFotos = Persistencia.PersistenciaFotos.FotosDeHotel(NombreDelHotel);
                Session["CantFotos"] = ArrayFotos.Count();

                Byte[] Foto = ArrayFotos[(int)Session["NumFoto"]];

                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "image/jpeg";
                Response.Expires = 0;
                Response.BinaryWrite(Foto);

            }
        }

我需要找到一种不清除所有页面的方法,只需在用户控件内绘制图像即可.

I need to find a way that doesnt clear all the page, just draw the image inside the user control.

推荐答案

您可以编写一个页面 .aspx 或一个处理程序,可能是 .ashx 将图片的内容发送回浏览器.您可以在 url 中传递信息.然后用这个页面的url加上html标签或者html控件来显示.

You could write a page .aspx or a handler maybe .ashx that send the content of the picture back to the browser. You could pass information in the url. Then use the url to this page with an html tag or html control to display it.

您需要使用控件.您可以将二进制数据转换为 base64 并将内容输出到 html 页面.

You need to use a Control. You could convert the binary data to base64 and output the content to the html page.

我给你一个带有 img html 标签的例子:

I give you an example with an img html tag:

<代码>< IMG ALT = " SRC =数据:图像/JPEG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAsMAAAGhCAIAAAALOi7ZAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QgLEhM6PUSGrwAAIABJREFUeNq8vcuSLEmWHKZ6jnlEZt5761Z3T/eAHAICAYRcEALsuOCWPzbzDfwP/gKXWJACoRDCBSkEBgPhADKY7qnu + 4wIdztHuThmHh55q2t6ho + SlpaqyMwID3ez89CjqsY////DM BM/+ ZMC/PGE3//PT/Z09/1I0t/1Rz/x+o9+0I++vv/n8fU/8MW/9U9+9JVvL/v/u1cy86cv5ttfePXKq//8fTfhp+/qT3/oq8v+6V/+Ay/v25/+4X/46nqO"/>

您还可以使用 CSS 对图像使用 base64 编码:

You can also use base64 encoding for image with CSS:

background-image: url(data:image/jpeg;base64,IVB)

要从 C# 转换为 base64,您可以使用:

To convert into base64 from C# you can use:

using System;

和:

Convert.ToBase64String(Foto);

这篇关于如何使用 C# 从字节数组显示 Web 表单中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆