《 巧用CLIPBOARD建立图像数据库 》   VisualBasic中的数据控件(datacontrol)能连接众多的数据库源并且操纵简便,用来开发数据库管理应用程序,可以轻而易举地完成以前需要大量编写程序才能完成的任务。
  使用其缺省数据库(Access格式)的二进制类型(Binary)字段来存放图形图像数据可建立包含图像的数据库,只是图像的格式受限制(缺省为.bmp类型)。
  那么,能否在VB中建立一个不受图像格式限制的图像数据库呢?下面通过实例介绍实现方法:1.定义数据库结构用数据管理器(datamanager)建立一个包含表ImgTable的数据库Imge1.mdb。
  其中表ImgTable的结构定义为:
  字段名 类型
  No   Integer
  ImgData Binary
  这里,同时预先建立几个ImgData内容为空的记录,供测试用。
  2.添加控件及代码
  在Form1上画出标签(Label1)、按钮(Command1)、图片框(Picture1)、数据控件(Data1)各一个,属性及代码如下:
  Data1DatabaseName=″C:\VB40\IMGE1MDB″
  Data1RecordSource=ImgTable
  Label1DataSource=ImgTable
  Label1DataField=No
  Picture1DataSource=ImgTable
  Picture1DataField=ImgData
  SubCommand1_Click()
  ′从Clipboard截取图像数据到图片框控件中
  Picture1Picture=ClipboardGetData()
  End Sub
  3.往Clipboard送图像可采用各种支持Clipboard的图像编辑器,如在Photoshop上处理好图像,然后将其“拷贝”或“剪切”至Clipboard。
  4.从Clipboard截取图像到数据库
  切换至VB,运行Form1。用鼠标点击按钮Command1,将图像从Clipboard“粘贴”到图片框中。然后,利用数据控件将当前记录往后(或往前)滑动,图片框中的图像便自动保存至数据库中。
  5.重复3、4步,利用Clipboard可建立一个不受图像格式限制的图像数据库,而且,比起字段中放置图像文件名再依此调用图像文件的方法更易管理。
  另外,对于一些数据库的增加、查询、修改等操纵功能,可配合使用数据库对象(如Database、TableDef、Field、Dynaset、Snapshot等)去完成。
  以上程序在VisualBasic3.0/4.0上通过。
(浙江 胡文俊) 

解决方案 »

  1.   

    There has been a endless discussion on the internet about the problems of storing images in databases and retrieving them with ASP. Some people say it is possible, but don't have the code, while others say it is only possible using MS SQL and not using MS Access.I have been looking for the solutions and found it: it is very well possible, even when using Access, and I have the code for you: 
    I assume that you have created in Access a database db1.mdb with a table Table1 and a field Field1 of the OLE type. This is the Visual Basic code for storing an image:Dim a() As Byte, b As String, c, i, l, r     Open "image.gif" For Binary As 1     l = LOF(1)     b = Space(l)     Get #1, , b     Close 1     ReDim a(l)     For i = 0 To l - 1         a(i) = CByte(Asc(Mid(b, i + 1, 1)))     Next i     Set c = CreateObject("ADODB.Connection")     c.Open "driver={Microsoft Access Driver (*.mdb)};dbq=db1.mdb"     Set r = CreateObject("ADODB.RecordSet")     r.Open "SELECT * FROM Table1", c, 2, 2     If r.EOF Then         r.addnew     End If     r(0) = a     r.Update     r.CloseThis is the ASP code for retrieving the image:    <%     Response.Buffer = true     Response.Clear     Response.Expires = 0     Response.ContentType = "image/gif"     Set c = Server.CreateObject("ADODB.Connection")     c.Open "driver={Microsoft Access Driver *.mdb)};dbq=\inetpub\wwwroot\db1.mdb"     Set r = CreateObject("ADODB.RecordSet")     r.Open "SELECT * FROM Table1", c, 2, 2     Response.BinaryWrite r(0)     r.close     Response.End     %>The only assumption I have now is that the length of the image is not longer than the maximum variable size. Otherwise use the chunk-method.