ImageDescription Column为储蓄图象文件说明字段,ImageSize Column为储存图象文件长度字段,结构如下:
CREATE TABLE [dbo].[ImageStore] (
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 向数据库中存入图片:using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
using System.IO
using System.Data.SqlClientnamespace UpLoadFile
{
/// <summary>
/// Summary description for UpLoadImage.
/// </summary>
public class UpLoadImage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnUpload
protected System.Web.UI.WebControls.Label txtMessage
protected System.Web.UI.WebControls.TextBox txtDescription
protected System.Web.UI.HtmlControls.HtmlTable Table1
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE//HtmlControl、WebControls控件对象
protected Int32 FileLength = 0
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent()
base.OnInit(e)
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click)
this.Load += new System.EventHandler(this.Page_Load) }
#endregion private void btnUpload_Click(object sender, System.EventArgs e)
{
HttpPostedFile UpFile = this.UP_FILE.PostedFile//HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength//记录文件长度
try
{
if (FileLength == 0)
{ //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>"
}
else
{
Byte[] FileByteArray = new Byte[FileLength]//图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream//建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength)
//建立SQL Server链接
SqlConnection Con = new SqlConnection("uid=sapwd= initial catalog=EEdata source=127.0.0.1Connect Timeout=90")
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)"
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con)
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = UpFile.ContentType//记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength
Con.Open()
CmdObj.ExecuteNonQuery()
Con.Close()
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>"//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString()
}
} }
}
将数据库中的图片数据读出来显示:using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
using System.IO
using System.Data.SqlClientnamespace UpLoadFile
{
/// <summary>
/// Summary description for ReadImage.
/// </summary>
public class ReadImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
string id = Request.QueryString["ImgID"] //得到图片的IDif (id != ""&&id != null &&id != string.Empty)
{
ShowImage( id)
}
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent()
base.OnInit(e)
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load) }
#endregion public void ShowImage(string id)
{
int ImgID = Convert.ToInt32(id)//ImgID为图片ID
//建立数据库链接
SqlConnection Con = new SqlConnection("uid=sapwd= initial catalog=EEdata source=127.0.0.1Connect Timeout=90")
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID"
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con)
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID
try
{
Con.Open()
SqlDataReader SqlReader = CmdObj.ExecuteReader()
SqlReader.Read()
Response.ContentType = (string)SqlReader["ImageContentType"]//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"])
Response.End()
Con.Close()
}
catch
{
Response.Write("<script>alert('该图片不存在')</script>")
return
} }
}
}
1、二进制图片是指图片是二进制文件,图片保存在磁盘是二进制文件。实际就是称作文本文件。它在磁盘保存时也是一种二进制文件。计算机的存储在物理上是都二进制的,所以文本文件与二进制文件的区别并不是物理上的,而是逻辑上的。这两者只是在编码层次上有差异。
2、通过分割从彩色图像生成二进制图像。分割是将源图像中的每个像素分配给两个或更多个类的过程。如果有两个以上的类,则通常的结果是几个二进制图像。最简单的分割方式可能是基于灰度强度将像素分配给前景或背景。
3、基本思路是在图片文件以二进制流的方式读入到计算机中后,将该二进制流转换为字符串,即“图片字符串”,最后保存到XML文档中。显示时,则将XML文档中的“图片字符串”转换为二进制流,并用可视组件(如web网页中的 组件)进行显示。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)