码迷,mamicode.com
首页 > 数据库 > 详细

图片存入数据库并取出显示

时间:2014-11-19 18:34:32      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

path是图片路径。

/// <summary>
/// 将图片转成二进制
/// </summary>
/// <param name="FilePath"></param>
/// <returns></returns>
public static byte[] ImageDatabytes(string FilePath)
{
if (!File.Exists(FilePath))
{
return null;
}
Bitmap myBitmap = new Bitmap(System.Drawing.Image.FromFile(FilePath));
using (MemoryStream curImageStream = new MemoryStream())
{
myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
curImageStream.Flush();
byte[] bmpBytes = curImageStream.ToArray();
//如果转字符串的话
//string BmpStr = Convert.ToBase64String(bmpBytes);
return bmpBytes;
}
}

aspx用一般应用程序把图片上传到指定文件夹下
 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            HttpServerUtility server = context.Server;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            HttpPostedFile file = null;
            if (context.Request.Files != null && context.Request.Files.Count > 0)
            {
                file = context.Request.Files[0];
                if (file.ContentLength > 0)
                {
                    string extName = Path.GetExtension(file.FileName);
                    string fileName = Guid.NewGuid().ToString();
                    string fullName = fileName + extName;

                    string imageFilter = ".jpg|.png|.gif|.ico";// 随便模拟几个图片类型
                    if (imageFilter.Contains(extName.ToLower()))
                    {
                        string phyFilePath = server.MapPath("~/Upload/Image/") + fullName;
                        file.SaveAs(phyFilePath);
                        bool flage = IntoSQL(request, response);  //存数据库
                        if (flage)
                        {
                            response.Write("<script>alert(‘上传成功‘);history.back()</script>");
                            //response.Write("<script language=javascript>history.go(-2);</script>");
                            //response.Write("上传成功!文件名:" + fullName + "<br />");
                            //response.Write(string.Format("<img src=‘Upload/Image/{0}‘/>", fullName));
                        }
                    }
                }
            }
        }

借助一般应用程序,页面前端image标签把src指向“一般应用程序” 可以传相应的参数,来确定显示哪一张

 <img src="AppService/EditPicture.ashx?GOODSID=<%=goodsID %>" class="miniztpic" alt="" />

一般应用程序主要代码如下:

public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string goodsID = context.Request.QueryString["GOODSID"];
        WeChatBLL.ItemsListBLL itemsListBLL = new WeChatBLL.ItemsListBLL();

        System.Collections.Generic.List<MemberShip.ReplacementItem> replacementItemList = itemsListBLL.GetReplacementItemList();  //查询数据库所以图片

        Object o = "";
        if (goodsID == null || goodsID == "")
        {
            //o = voucherEditorContext.Voucher.VoucherData.Tables["GOODSINFO"].Rows[0][scenePropertyDetail.PropertyCode];
        }
        else
        {
            foreach (var item in replacementItemList)  //循环图片列表
            {
                if (item.ItemID == goodsID)  //当图片id相等时
                {
                    if (item.ItemPhotos != null)
                    {
                        o = item.ItemPhotos[0];  //直接把数据库二进制赋值给o
                    }
                }
            }
        }

        if (o is DBNull || o.ToString() == "")
        {
            return;
        }
        else
        {
            byte[] image = (Byte[])o;

            context.Response.BinaryWrite(image);  //返回二进制图片,页面会自动显示
        }
    }

 

 

图片存入数据库并取出显示

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/wwfjcy479/p/4108715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!