Home > Software design >  asp.net general handler to get pictures
asp.net general handler to get pictures

Time:01-17

How to get pictures (binary data) in the database using general processing procedures in ASP.NET general procedure:

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; }
    }
}

I can't get pictures from the database. I do not know whether my code is correct.

I am new - who can please help me?

CodePudding user response:

You can read byte[] from the database and convert it to stream.

code show as below:

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;
}
  • Related