以二进制进行存储和读取图片

//存
string ImgFName;
HttpPostedFile UpFile=UP_FILE.PostedFile;
FileLength=UpFile.ContentLength;//记录文件长度
try
{
if(FileLength==0)
{
cfun.alert(Response,"请选择要上传的图片");
}
else
{
Byte[] FileByteArray = new Byte[FileLength];   //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream;      //建立数据流对像

int pos=UpFile.FileName.LastIndexOf("//");
ImgFName=UpFile.FileName.Substring(pos+1);

StreamObject.Read(FileByteArray,0,FileLength);  
cmd=new SqlCommand("InsImage",con);
cmd.CommandType=CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add(new SqlParameter("@ImageData",SqlDbType.Image)).Value=FileByteArray;
cmd.Parameters.Add(new SqlParameter("@ImageName",SqlDbType.NVarChar)).Value=ImgFName;
cmd.Parameters.Add(new SqlParameter("@ImageExplain",SqlDbType.NVarChar)).Value=txtBody.Text;
cmd.Parameters.Add(new SqlParameter("@ImageGroup",SqlDbType.NVarChar)).Value=txtImgGup.Text;
cmd.Parameters.Add(new SqlParameter("@ImageDate",SqlDbType.NVarChar)).Value=labDate.Text;
cmd.ExecuteNonQuery();
cfun.alert(Response,"上传成功!","upimage.aspx");
con.Close();
}
}
catch(Exception ex)
{
cfun.alert(Response,"'"+ex.Message+"'");
}

//

con=new SqlConnection(ConfigurationSettings.AppSettings["ConStr"]);
SimgId=Request.QueryString["ImgId"].ToString();
Sqlstr="select ImageData from ImageStore where ImgId='"+SimgId+"'";
if(!IsPostBack)
{
con.Open();
cmd=new SqlCommand(Sqlstr,con);
dr=cmd.ExecuteReader();
while(dr.Read())
{
Response.BinaryWrite((byte[])dr["ImageData"]);
}
con.Close();

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Eray/archive/2007/04/01/1548505.aspx

package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.NativeMenu; import flash.display.NativeMenuItem; import flash.display.NativeWindow; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.net.FileFilter; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.utils.ByteArray; public class Main extends Sprite { private var rootMenu:NativeMenu = new NativeMenu; private var fileMenu:NativeMenu = new NativeMenu; private var openItem:NativeMenuItem = new NativeMenuItem("打开文件"); private var quitItem:NativeMenuItem = new NativeMenuItem("退出"); private var helpMenu:NativeMenu = new NativeMenu; private var fsnhf:NativeMenuItem = new NativeMenuItem("00"); private var window:NativeWindow = stage.nativeWindow; private var loader:Loader = new Loader; private var imageFileFilter:FileFilter = new FileFilter("图片(*.jpg;*.png;*.gif;*.jpeg)", "*.jpg;*.png;*.gif;*.jpeg"); public function Main():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; buildMenu(); configListeners(); } private function buildMenu():void { rootMenu.addSubmenu(fileMenu, "文件"); fileMenu.addItem(openItem); fileMenu.addItem(quitItem); rootMenu.addSubmenu(helpMenu, "帮助"); helpMenu.addItem(fsnhf); window.menu = rootMenu; } private function configListeners():void { openItem.addEventListener(Event.SELECT, eventHandler); quitItem.addEventListener(Event.SELECT, eventHandler); fsnhf.addEventListener(Event.SELECT, eventHandler); } private function eventHandler(event:Event):void { switch(event.target) { case openItem: var file:File = new File; file.addEventListener(Event.SELECT, selectFile); file.browseForOpen("打开文件", [imageFileFilter]); break; case quitItem: window.close(); break; case fsnhf: navigateToURL(new URLRequest("00")); break; } } private function selectFile(event:Event):void { var file:File = event.target as File; var stream:FileStream = new FileStream; stream.open(file, FileMode.READ); var bytes:ByteArray = new ByteArray; stream.readBytes(bytes, 0, stream.bytesAvailable); stream.close(); loader.unload(); loader = new Loader; loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); loader.loadBytes(bytes); addChild(loader); } private function imageLoaded(event:Event):void { event.target.removeEventListener(Event.COMPLETE, imageLoaded); var bitmap:Bitmap = Bitmap(event.target.loader.content); window.width = bitmap.width; window.height = bitmap.height + 30; } } }
图片的常见存储与读取凡是有以下几种: 存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]. 1.参数是图片路径:返回Byte[]类型: public byte[] GetPictureData(string imagepath) { /**/////根据图片文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; }2.参数类型是Image对象,返回Byte[]类型: public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //将Image转换成流数据,并保存为byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }好了,这样通过上面的方法就可以把图片转换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把图片的二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的图片读取出来,实际上这是一个相反的过程。 读取图片:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX 1.参数是Byte[]类型,返回值是Image对象: 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; }2.参数是Byte[] 类型,没有返回值,这是针对asp.net中把图片从输出到网页上(Response.BinaryWrite) public void WritePhoto(byte[] streamByte) { // Response.ContentType 的默认值为默认值为“text/html” Response.ContentType = "image/GIF"; //图片输出的类型有: image/GIF image/JPEG Response.BinaryWrite(streamByte); }补充: 针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型: Response.ContentType = "application/msword"; Response.ContentType = "application/x-shockwave-flash"; Response.ContentType = "application/vnd.ms-excel";另外可以针对不同的格式,用不同的输出类型以适合不同的类型: switch (dataread("document_type")) { case "doc": Response.ContentType = "application/msword"; case "swf": Response.ContentType = "application/x-shockwave-flash"; case "xls": Response.ContentType = "application/vnd.ms-excel"; case "gif": Response.ContentType = "image/gif"; case "Jpg": Response.ContentType = "image/jpeg"; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值