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

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);  //返回二进制图片,页面会自动显示
        }
    }
时间: 2024-08-01 02:01:15

图片存入数据库并取出显示的相关文章

C#图片存入数据库及其读出显示

<1>将图片转换成二进制插入数据库 FileStream fs = new FileStream("D:\\Add.ico",FileMode.Open); byte[] imagebytes = new byte[fs.Length]; BinaryReader br = new BinaryReader(fs); imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));   //将图片转换成二进制字符串 string

方法:C#在WinForm中如何将Image存入SQL2000和取出显示

//保存     sql="insert   into   p_ry_photo(id,photo)   values   ('"+id+"',@i)";     SqlCommand   cmd=new   SqlCommand(sql,CommonVar.SqlConDataBase);     MemoryStream   s   =   new   MemoryStream();     picPhoto.Image.Save(s,System.Drawin

Android 图片存入系统相册更新显示实例详解

在开发android的过程中,我们避免不了可能会涉及到做一个自定义相册或则会去本地创建一个文件夹来存储我们需要的图片.拿相册来说,比如我们创建一个test的文件夹,拍完一张照片后存储到这个指定的test文件夹里,然后在相册里面显示出来,就像微信的效果一样.拍完即可立即显示.但是,在实际开发过程中我们保存完一张图片后并不能立即更新显示出来这个图片,需要我们重启手机才能在系统相册中显示出来. 这里先提供一个插入系统图库的方法: MediaStore.Images.Media.insertImage(

将图片存入数据库【java实现】

import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class

C#_图片存取数据库Winform

#region 用于在PictureBox控件中显示选择的图片        /// <summary>        /// 用于在PictureBox控件中显示选择的图片        /// </summary>        /// <param name="openF">图像名</param>        /// <param name="MyImage">pictureBox控件ID</

将图片以Blob格式存入数据库,再通过Servlet显示到界面

1:为了方便测试,直接将1.png图片存入到数据库中. public static void storePicBlog() throws FileNotFoundException, SQLException, IOException{ Connection conn = JdbcUtil.getConnection(); File f = new File("1.jpg"); FileInputStream fis = new FileInputStream(f); String s

运用PDO存储将图片、音频文件存入数据库

在数据库中创建表格的时候,有一个字段为image,用来保存图片,那么其类型就是blob,关于blob,百度百科是这样描述的 BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器. 在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型. BLOB是一个大文件,典型的BLOB是一张图片或一个声音文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传.下载或者存放到一个数据库). 根据Eric Raymond的说法,处理BLOB的主要思想就是

在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好

花了一下午时间,终于学会了在数据库中存储图片,以及在界面中显示图片了. 存储图片有两种方法: 一种是:直接把图片转换成二进制文件存储在数据库中. 一种是:存储图片的路径到数据库,用的时候直接调用路径给image等图像控件即可. 两种方法,有利有弊,第一种方法适合存储量少且重要的图片信息.第二种方法适合存储量大但不是太重要的图片. 我使用的是第二种方法,简单,实用. 下面就是我具体的实现过程: 首先我新建了两个网页文件,insert_photo.aspx / select_photo.apsx 第

C# 图片存入SQL Server数据库

OpenFileDialog openfiledialog1 = new OpenFileDialog(); if (openfiledialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openfiledialog1.FileName;//文件路径 } string imageName = DateTime.Now.ToString("yyyymmddhhMMss");//生成文件名到数据库 FileStream fs