转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Store Images in Your Database         ★★★★

Store Images in Your Database

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2028 更新时间:2009/4/23 16:39:56

 

February 2001

Getting Started

Store Images in Your Database

 

Create an easy way to save and retrieve images in your VB application and Microsoft Access database.

by Andy Rosebrock and Stan Schultes

Reprinted with permission from Visual Basic Programmer''''s Journal, February 2001, Volume 11, Issue 2, Copyright 2001, Fawcette Technical Publications, Palo Alto, CA, USA. To subscribe, call 1-800-848-5523, 650-833-7100, visit www.vbpj.com, or visit The Development Exchange.

Whether you plan to include graphics within your program, create data entry forms, or design a database for your Web site, providing an easy method to modify and add images in a database allows your applications to be more flexible and portable. However, you need to decide carefully which method is best for your particular application. In this column, we''''ll focus on different ways to manipulate images as objects or file pointers within your application''''s database.

What you need:
Visual Basic 5.0 with Service Pack 3 or Visual Basic 6.0 with Service Pack 4
Access 97 or 2000
Microsoft Data Access Components (MDAC) 2.1

Customers often ask for a way to display their company logo and images in numerous locations within an application. Likewise, Web projects frequently require that each record have an image associated with the data. Images provide an easy way to visually associate an item to record data. The problem is that these images change constantly, and customers add new ones continuously. Your application should allow customers to have the flexibility they require.

Visual Basic offers several ways to store images, such as using the ImageList control, but these methods require that you recompile the application, and they''''re generally restricted to a limited number of images. The best way to store images is in a database. You can do this in a couple ways: by storing the image as a Binary Large Object (BLOB) in a database field, or by simply storing a "pointer" to the file location on disk. Each method has advantages and disadvantages. Storing an image as a BLOB can inflate your database size, although all your images will be in a single, central location. Storing the image as a file pointer allows your database to be considerably smaller, but missing or invalid files can cause problems later.

  Figure 1 | Improve Your Image. Deciding how you want to save images to your database is an important consideration. Click here.

Depending on your preference, you need to balance database size and efficiency to decide which method is best for a particular application. We''''ll outline both methods using Microsoft ActiveX Data Objects (ADO) to save and retrieve images to and from an Access 97 database. The code is identical if you want to use an Access 2000 database, but you need to download and install the latest Microsoft Data Access Components (MDAC) package.

Create the Database
You''''ll create a simple application to store a rudimentary image library. Before getting started with the VB project, you need to create the database you''''ll use to store the data. Start Access and create a new table called ImageLibrary. Add the necessary fields and properties (see Table 1).

After creating the database, start VB, choose Project | References, and select Microsoft ActiveX Data Objects 2.1 Library (or you can use the ADO 2.5 library if it''''s installed). Name the default form of the project frmMain. Design your form to match your needs for storing images. The ImageLibrary project is set up to allow you to select how you''''ll store images to the database (see Figure 1, and download the sample project).

You need to add the variables that reference the database connection and recordset. You''''ll use these variables to navigate through the database, and also to add and edit records. Add these variables to frmMain''''s Declarations section:

Private Conn As ADODB.Connection
Private rs As ADODB.Recordset

Next, you need to establish the connection to the database and recordset. Make sure the ImageLib.mdb file is in your application path, and add this code to the frmMain_Load event:

''''Establish the connection.
Set Conn = New ADODB.Connection
Conn.ConnectionString = _
	"Provider=Microsoft.Jet.OLEDB" & _
	".3.51;Data Source=" & App.Path & _
	"\ImageLib.mdb"
	Conn.Open
	
''''Open the recordset
Set rs = New ADODB.Recordset
rs.Open "ImageLibrary", Conn, _
	adOpenKeyset, adLockPessimistic, adCmdTable

Now that you''''ve opened the recordset, you can start viewing and modifying the record data. We''''ll start by looking at how to save and retrieve the actual image to the database using the GetChunk and Append-Chunk methods. You can use two methods to display a BLOB field stored in the database. In the first method, simply set the Image control''''s DataSource and Data-Field properties to the already opened recordset and BLOB field:

Set imgDBImage.DataSource = rs
imgDBImage.DataField = "ImageBLOB"

Following this method is generally the simplest way to start displaying the images stored in the database. However, larger files might start taxing your system memory as the Image control attempts to load from the database. If memory usage is a concern for your application because of large image files, you should use the second method—the ADO GetChunk method—to retrieve binary objects from the database instead. The GetChunk method allows you specify the number of bytes (chunks) to retrieve from the BLOB field. You can load the stored object in these smaller chunks (for larger images) or as a single chunk:

strData = rs("ImageBLOB"). _
	GetChunk(rs("ImageBLOB").ActualSize)

The drawback to the GetChunk method: Because the Image control''''s picture property can''''t read the byte array you load from the database, you first need to save a temporary file from the field data, then read that file with the LoadPicture method. In spite of this shortcoming, the GetChunk method is actually faster than setting the Image control''''s data properties.

Determine the Best Method
We did a small test to determine which method was the fastest by creating a table with 100 records. Each record contained binary data that was approximately 50K in size. We looped through all the records in the table while displaying the stored data in an Image control. The DataField method completed in 58 seconds, while the GetChunk method completed in 27 seconds (with a chunk size of 100 bytes). Because these particular images weren''''t overly large, we retrieved the entire object in a single chunk. This method resulted in the quickest time of 18 seconds. You need to determine the best method to use in your particular applications. For the purpose of this article, use the GetChunk method with a chunk size of 100 bytes (see Listing 1).

As with retrieving stored images, saving an image to the database requires you to first convert the image into a byte array. Afterward, you can write the bytes to the database field using the AppendChunk method. In the code, you can do this by writing the data to the field in a single swipe by setting the chunk size equal to the image file''''s total size (see Listing 2).

In the sample project, only the file path is stored to the database if you choose to save the image as a file pointer, reducing the database size and increasing the application''''s speed. The FillFields procedure in Listing 1 first determines whether the database field ImagePath contains a value. If you saved a path with the record, then the image is stored as a file pointer. If you did not save a path, then the image is stored as a BLOB (see the GetChunk method described earlier). You can display the stored pointer easily by simply using the Image control''''s LoadPicture method.

The obvious advantage to storing images as a file pointer is that only the file path is saved. As a result, your database won''''t grow as dramatically as it would if you stored the image in a BLOB field. In the example described earlier, with 100 records of 50K images stored in BLOB fields, the database grew to more than 4 MB. The same database using file pointers instead was under 100K. In speed comparisons, the file pointer method is the winner, completing the test in five seconds. These advantages generally make file pointers the preferred method of saving images.

The main disadvantage to using file pointers is that if file locations change, your application can''''t load the image. Another disadvantage: lack of application portability. If you move the application elsewhere, you''''d have to move the database and all the image files it points to. One way to alleviate these problems is to save the file''''s Universal Naming Convention (UNC) instead of the actual disk location (such as \\andyr\images\test.jpg instead of c:\images\test.jpg). Of course, if you were to send the application to outside customers, they probably wouldn''''t have access to the same resources as you (unless you saved the file pointer as an image located on the Internet, such as www.yoursite.com/images/test.jpg). This is pr

[1] [2]  下一页


[办公软件]PowerPoint模板使用经验之谈  [办公软件]教你在Powerpoint中设置页眉页脚
[办公软件]在Powerpoint中如何插入Flash动画  [办公软件]如何在Powerpoint 中(实现)输入上标、下标
[办公软件]如何在PowerPoint同一张幻灯片中显示大量文字  [办公软件]这样来修改PowerPoint超级链接的文字颜色
[办公软件]PowerPoint小小操作技巧,让您工作更轻松  [办公软件]如何在office(PowerPoint,Word,Excel)中制作带圈的…
[办公软件]保留PowerPoint超链接,但是取消超链接的下划线  [办公软件]挖掘PowerPoint图片自动压缩功能在不失真的情况下…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台