|
p align="justify">在Powerbuilder6.0程序中有时要用到BMP图象文件,比如人事系统中人员的相片、用扫描仪扫描进去的人事档案等等。但在Powerbuilder6.0中,没有直接的方法将BMP文件读入内存并显示或写入到数据库中。但Powerbuilder支持二进制文件的读写操作,可将BMP文件整个读入BLOB类型的变量中来实现,现在经下面两个例子来讲解
例子一、图象的显示
步骤如下:
1 取得位图文件名。
2 取得文件长度。
计算要用fileread函数读文件的次数(一次最多只能读32K数据)
3 打开文件
使用FOR循环将文件中的数据读入到BLOB变量中
4 建立一Picture控件
5 使用SetPicture函数显示BMP图象
现在编写程序代码
PowerBuilder要求每个应用程序的启动(Open)事件中具有一定代码,进入应用程序绘图器,在此例中我们为pic应用程序的Open事件中添加以下代码:
open(w_pic) //打开程序窗口
在W_pic窗体中建立一个“选择并显示位图”的按纽控件和建立P_1(Picture控件)
在“选择位图”的按纽控件的“open”事件中写入如下代码:
/* 本 段 代 码 将 读 取 一 个 超 过327680字 节 的 文 件, 这 个代 码 可 以 用 以 读 取Text 文 本 和Bitmap 图 象。*/
(“//------ //*****” 中间为注解的内容)
String Ls_path,ls_file,ls_parms,ls_dir,ls_command
integer ls_num,ls_cs,i
integer fh, ret
blob is_pic_blob
blob{327680} ls_blob //必须指明ls_blob的大小
string defext = "BMP"
string Filter = "bitmap Files (*.bmp), *.bmp"
ulong nbr,ls_len
ret = GetFileOpenName("Open Bitmap", ls_path,ls_name, defext, filter) //选择获取打开的位图文件
IF ret = 1 THE /p>
ls_len=filelength(ls_path) //取得文件的字节长度
//------计算出读取文件的次数
ls_cs=ls_len/(32*1024)
if mod(ls_len,32)=0 then
else
ls_cs=ls_cs+1
end if
//*****
ls_num=FileOpen(ls_path,StreamMode!) //打开选取的文件
//------循环将文件中的数据读入到BLOB变量中
IF ls_num$#@60;$#@62; -1 THEN
nbr=1
for i=1 to ls_cs
fileread(ls_num,is_pic_blob)
nbr = BlobEdit(ls_blob,nbr,is_pic_blob)
next
//*****
p_1.SetPicture(ls_blob) //显示位图
FileClose(ls_num)
end if
END IF
在此例子的基础来实现怎么在程序中将照片存入数据库之中
例子二、在程序中将照片存入数据库之中
数据库进行写入和后台数据库有关:以SQLANYWAY为例:一般用 UPDATEBLOB 和 SELECTBLOB 两个SQL语句来实现。 在本例中我们只是简单地执行位图的存入与取出操作,所以程序窗口也比较简单。其功能是通过“位图入库”按纽来选择写入数据库的图象,写入数据库的图象可以通过点击一旁的便列表来显视。
首先在应用脚本中连数据库及运动运行窗口:
SQLCA.DBMS =ProfileString("regedit.ini","INI","dbms"," ") sqlca.Database=ProfileString("regedit.ini","INI","database"," ") sqlca.userid= ProfileString ("regedit.ini", "INI", "userid", "") sqlca.dbpass= ProfileString ("regedit.ini", "INI", "dbpass", "") sqlca.logid = ProfileString ("regedit.ini", "INI", "logid", "") sqlca.logpass = ProfileString ("regedit.ini", "INI", "logpassword", "") sqlca.servername = ProfileString ("regedit.ini", "INI", "servername", "") SQLCA.DBParm =ProfileString("regedit.ini","INI","dbparm"," ") CONNECT USING sqlca ; open(w_pic)
在W_pic窗体中建立一个“位图入库”的按纽控件,数据窗口控件(用于选择显视的图片)和建立P_1(Picture控件)
在窗体W_pic的"OPEN"脚本中写入:
dw_1.settransobject(sqlca) dw_1.retrieve()
在“位图入图”的按纽控件的“click"脚本中写入:
String Ls_path,ls_file,ls_parms,ls_dir,ls_command integer ls_num,ls_cs,i integer fh, ret blob ls_blob //-----选取图片进个BLOB类型的变量,这里读取的图片不大于32K string defext = "BMP" string Filter = "bitmap Files (*.bmp), *.bmp" ulong nbr,ls_len,nbr1
ret = GetFileOpenName("Open Bitmap", ls_path,ls_file, defext, filter) IF ret = 1 THEN ls_num=FileOpen(ls_path,StreamMode!) IF ls_num$#@60;$#@62; -1 THEN fileread(ls_num,ls_blob) FileClose(ls_num) end if END IF
//*******
//取得序列号 SELECT num INTO :i FROM number where no=1 using sqlca;
//新增一条记录 INSERT INTO pic ( no ) VALUES ( :i ) using sqlca ;
//图片写入数据库 UPDATEBLOB pic SET pics=:ls_blob WHERE no=:i using sqlca; i=i+1
update number set num=:i where no=1 using sqlca; dw_1.retrieve() 在“数据窗口DW_1”的按纽控件的“doubleclick"脚本中写入(实现显示双击选中的图片):
blob ls_blob int i i=int(this.getrow()) if i$#@62;0 then messagebox("","fffff") SELECTblob pics INTO :ls_blob FROM pic where no=:i using sqlca ; messagebox(string(sqlca.sqlcode),string(len(ls_blob))) p_1.SetPicture(ls_blob) end if
以上两个例子的源程序可在这里下载。
[办公软件]在sybase中插入图片、PDF、文本文件 [办公软件]安装Sybase ASE [办公软件]linux指令大全(完整篇) [办公软件]Linux新手入门常用命令大全 [办公软件]在RedHat Linux 9里安装gaim0.80 [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法 [办公软件]Linux程序员必读:中文化与GB18030标准 [办公软件]linux指令大全 [办公软件]制作Linux启动盘的四种方法 [办公软件]Linux文件系统的反删除方法
|