qq_27085783
qq_766202417
采纳率22.2%
2015-04-02 09:12

asp.net 一般处理程序获取图片

已采纳

asp.net 利用一般处理程序 怎么获取数据库中的图片(二进制数据)

一般处理程序
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using DingDan;
using WEIP.Common;

namespace DingDan.OA.Staff.Center
{
///
/// StffIcon 的摘要说明
///
public class StffIcon : IHttpHandler
{

    //员工信息
    ProductStaffBasic Psb = new ProductStaffBasic();
    //员工信息实体
    ProductStaffBasicEntity PsbE = new ProductStaffBasicEntity();

    public void ProcessRequest(HttpContext context)
    {
        MemoryStream ms = null;
        try
        {
            context.Response.ContentType = "text/plain";
            HttpRequest request = context.Request;
            int id = Convert.ToInt32(context.Request.QueryString["id"]);
            PsbE = Psb.GetInfo(id);
            Byte[] img = PsbE.Psblcon;
            ms = new MemoryStream(img);
            Image icon = Image.FromStream(ms);
            int thumbnailWidth = TypeParse.StrToInt(request["tw"],-1);
            int thumbnailHeight = TypeParse.StrToInt(request["th"],-1);
            string mode = request.Params["mode"].ToString();
            string module = request.QueryString["module"].ToString();

            string tablename = module + "_ATTACHMENT";
            WEIP.Sys.Attachment attach = new WEIP.Sys.Attachment("", tablename, true, "");
            ms.Close();

            //背景色
            WEIP.Site.Thumbnail.BgColor color = WEIP.Site.Thumbnail.BgColor.White;

            if (mode == "0")
            {
                color = WEIP.Site.Thumbnail.BgColor.White;
            }
            if (thumbnailWidth == -1 && thumbnailHeight == -1)
            {
                ImageFormat thisFormat = icon.RawFormat;
                if (thisFormat.Equals(ImageFormat.Gif))
                {
                    context.Response.ContentType = "image/gif";
                }
                else
                {
                    context.Response.ContentType = "image/jpeg";
                }
                Bitmap outBmp = new Bitmap(icon);
                outBmp.Save(context.Response.OutputStream, thisFormat);
            }
            else
                new WEIP.Site.Thumbnail().GetSmallImage(context.Response, icon, thumbnailWidth, thumbnailHeight, color);

            //写新图
        }
        catch
        {

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

}

asp页面




/asp:HyperLink
数据加载时的方法
this.PImage.ImageUrl = "~/Staff/Center/StffIcon.ashx?id=" + Id + "&tw=150&th=150&mode=0&module=OUTSIDE_SITE";
            this.LinkUrl.NavigateUrl = "~/Staff/Center/StffIcon.ashx?id=" + Id + "&mode=0&module=OUTSIDE_SITE";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • bulusli3 lhl_lqc 6年前

    public System.Drawing.Image ReturnPhoto(byte[] streamByte)
    {
    System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
    return img;
    }

    点赞 评论 复制链接分享
  • starfd 娃都会打酱油了 6年前

    将byte[]从数据库读取出来,然后转化为stream,最后将handler的返回类型声明为image/jpg这种类似的格式,在Response.OutputStream.Read(stream)
    好像是这样的写法,忘接了

    点赞 评论 复制链接分享
  • showbo GoCityPass新加坡曼谷通票 6年前

相关推荐