本文介绍了动态影像使用通用处理器(从DB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用通用处理器来检索和显示存储在数据库中的图像。

但它只是不工作。香港专业教育学院试图verious低于code,但我似乎无法得到它的工作。

能否anyonne发现我在做什么错了,还是有一些建议?

 <%@ WebHandler LANGUAGE =C#类=IconsDb%GT;使用系统;
使用的System.Web;
使用System.Linq的;
使用System.Data.Entity的;公共类IconsDb:IHttpHandler的{    公共无效的ProcessRequest(HttpContext的背景下){
        context.Response.ContentType =text / plain的;
        context.Response.Write(的Hello World);
        INT32 iconId;        如果(context.Request.QueryString [ID]!= NULL)
            iconId = Convert.ToInt32(context.Request.QueryString [ID]);
        其他
            抛出新的ArgumentException(没有指定的参数);        context.Response.ContentType =图像/ GIF;
        //System.IO.Stream STRM = ShowEmpImage(iconId);        变种DB =新UdINaturen.UdINaturenContext();        在db.subcategoryicons VAR =调用getIcon(从我
                       其中,i.id == iconId
                       选择i.p​​icture).FirstOrDefault();
        反对IMG =调用getIcon;        System.IO.MemoryStream memStream =新System.IO.MemoryStream((字节[])Convert.FromBase64String(调用getIcon));
        System.Drawing.Bitmap bitImage =新System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(memStream));
        字节[]缓冲= memStream.ToArray();
        context.Response.ContentType =图像/ GIF;
        //context.Response.OutputStream.Write(buffer,0,buffer.Length);
        //context.Response.WriteFile();
        context.Response.BinaryWrite(缓冲液);
        //context.Response.Flush();
    }    公共BOOL IsReusable {
        获得{
            返回true;
        }
    }}


解决方案

哇,好不好。不知道有多少是旧的code,什么应该被注释掉或W / E,但尝试这样的:

 公共无效的ProcessRequest(HttpContext的背景下){
    INT iconId;    如果(string.IsNullOrEmpty(context.Request.QueryString [ID])||!int.TryParse(context.Request.QueryString [ID],出iconId))
        抛出新的ArgumentException(没有指定的参数);    context.Response.ContentType =图像/ GIF;    变种DB =新UdINaturen.UdINaturenContext();    在db.subcategoryicons VAR =调用getIcon(从我
                   其中,i.id == iconId
                   选择i.p​​icture).FirstOrDefault();
    字节[]缓冲=(字节[])Convert.FromBase64String(调用getIcon);    context.Response.ContentType =图像/ GIF;
    context.Response.BinaryWrite(缓冲液);
    context.Response.Flush();
}

I'm trying to using a Generic Handler to retrieve and display images that are stored in a database.

But its just not working. Ive tried verious of the code below, but I cant seem to get it to work.

Can anyonne spot what I am doing wrong, or have some suggestions?

<%@ WebHandler Language="C#" Class="IconsDb" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Entity;

public class IconsDb : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        Int32 iconId;

        if (context.Request.QueryString["id"] != null)
            iconId = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/gif";
        //System.IO.Stream strm = ShowEmpImage(iconId);

        var db = new UdINaturen.UdINaturenContext();

        var GetIcon = (from i in db.subcategoryicons
                       where i.id == iconId
                       select i.picture).FirstOrDefault();
        object img = GetIcon;

        System.IO.MemoryStream memStream= new System.IO.MemoryStream((byte[])Convert.FromBase64String(GetIcon));
        System.Drawing.Bitmap bitImage=new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(memStream));


        byte[] buffer = memStream.ToArray();
        context.Response.ContentType = "image/gif";
        //context.Response.OutputStream.Write(buffer, 0, buffer.Length);
        //context.Response.WriteFile();
        context.Response.BinaryWrite(buffer);
        //context.Response.Flush();


    }





    public bool IsReusable {
        get {
            return true;
        }
    }

}
解决方案

wow, ok. Not sure how much of that is old code, what should be commented out or w/e but try something like this:

public void ProcessRequest (HttpContext context) {
    int iconId;

    if (string.IsNullOrEmpty(context.Request.QueryString["id"]) || !int.TryParse(context.Request.QueryString["id"], out iconId) )
        throw new ArgumentException("No parameter specified");

    context.Response.ContentType = "image/gif";

    var db = new UdINaturen.UdINaturenContext();

    var GetIcon = (from i in db.subcategoryicons
                   where i.id == iconId
                   select i.picture).FirstOrDefault();
    byte[] buffer = (byte[])Convert.FromBase64String(GetIcon);

    context.Response.ContentType = "image/gif";
    context.Response.BinaryWrite(buffer);
    context.Response.Flush();
}

这篇关于动态影像使用通用处理器(从DB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:03