net显示VARBINARY图像

net显示VARBINARY图像

本文介绍了在C#中asp.net显示VARBINARY图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

所以我有这样的方法:

    string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);


    SqlCommand cm = baseballConnection.CreateCommand();

    baseballConnection.Open();
    cm.CommandText = sql;
    baseballConnection.Close();
    return cm.ExecuteScalar() as byte[];

和它的作品,至少我没有得到任何错误。但现在,我已经是一个字节数组,我怎么能在屏幕上显示呢?我在屏幕上的图像的工具,但我知道如何显示的图像是如果保存在服务器上,并有一个文件路径的唯一途径。我不会知道如何保存字节数组服务器,但是这将是cheesing它,就没有点我已经保存在数据库作为varbinary的形象,现在它检索。

and it works, at least i get no errors. but now all i have is a byte array, how can i display this on the screen? i have an image tool on the screen but the only way i know how to display an image is if its saved on the server and has a file path. i wouldnt know how to save the byte array to the server, but that would be cheesing it and there would be no point to my already saving the image in the db as a varbinary and now retrieving it.

我怎样才能得到这个字节数组显示?

how can i get this byte array to display?

推荐答案

要呈现在asp.net中的形象,你必须创建一个asp.net页面或通用处理程序保存在输出响应流中的字节。

To render the image in asp.net, you have to create an asp.net page or generic handler to save the bytes in the output response stream.

您会再设置一个HTML图像或控制链接到网页或通用处理器。

You would then set the an HTML image or control to link to the page or generic handler.

例如,

<img src="getimage.aspx?id=1" style="width:100px; height:100px" alt="" />

所以你的情况会是这样的:

So in your case it would be something like this:

using System.Web;

namespace ImageUtil
{
    public class ImageProvider : IHttpHandler
    {
        publicvoid ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";

            string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);

            SqlCommand cm = baseballConnection.CreateCommand();
            baseballConnection.Open();
            cm.CommandText = sql;
            byte[] img = (byte[])cm.ExecuteScalar();
            baseballConnection.Close();

            context.Response.BinaryWrite(img);
        }

        publicbool IsReusable
        {
            get
            {
                returnfalse;
            }
        }
    }
}

下面是一个链接:

这篇关于在C#中asp.net显示VARBINARY图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:12