获 得 位 图 文 件 的 信 息

博客介绍了在Form中添加Picture和CommandButton控件,加入位图文件后,通过代码获取位图信息。使用了GetObject和GetBitmapBits等函数,将位图信息存到Byte数组,并打印出图形的高度、宽度、每像素位数等相关信息。
在Form中添加一个Picture控件和一个CommandButton控件,在Picture控件中加入一个位图文件,将下面代码加入其中:
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _
(ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) _
As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, _
ByVal dwCount As Long, lpBits As Any) As Long

Private Type BITMAP
  bmType As Long
  bmWidth As Long
  bmHeight As Long
  bmWidthBytes As Long
  bmPlanes As Integer
  bmBitsPixel As Integer
  bmBits As Long
End Type

Private Sub Command1_Click()
  Dim hBitmap As Long
  Dim res As Long
  Dim bmp As BITMAP
  Dim byteAry() As Byte
  Dim totbyte As Long, i As Long
  hBitmap = Picture1.Picture.Handle

  res = GetObject(hBitmap, Len(bmp), bmp)
'取得BITMAP的结构

  totbyte = bmp.bmWidthBytes * bmp.bmHeight
'总共要多少BYTE来存图
  ReDim byteAry(totbyte - 1)
 
'将Picture1中的图信息存到ByteAry
  res = GetBitmapBits(hBitmap, totbyte, byteAry(0))

  Debug.Print "Total Bytes Copied :"; res
  Debug.Print "bmp.bmBits "; bmp.bmBits
  Debug.Print "bmp.bmBitsPixel "; bmp.bmBitsPixel
'每相素位数
  Debug.Print "bmp.bmHeight "; bmp.bmHeight
'以相素计算图象高度
  Debug.Print "bmp.bmPlanes "; bmp.bmPlanes
  Debug.Print "bmp.bmType "; bmp.bmType
  Debug.Print "bmp.bmWidth "; bmp.bmWidth
'以相素计算图形宽度
  Debug.Print "bmp.bmWidthBytes "; bmp.bmWidthBytes
'以字节计算的每扫描线长度
End Sub
• 步骤 2: 添加主要成员, 并通过 findViewById 方法取组引用: private final String FILE_NAME = ” fileDemo . txt ” ; private TextView labelView ; private TextView displayView ; private CheckBox appendBox ; private EditText entryText ; labelView = ( TextView ) findViewById (R. id . label ) ; displayView = ( TextView ) findViewById (R. id . display ) ; appendBox = ( CheckBox ) findViewById (R. id . append ) ; entryText = ( EditText ) findViewById (R. id . entry ) ; Button writeButton = ( Button ) findViewById (R. id . write ) ; Button readButton = ( Button ) findViewById (R. id . read ) ; • 步骤 3: 为 read 绑定事监听器 // 为 read 绑 定 事 监 听 器 View . OnClickListener readButtonListener = new View . OnClickListener ( ) { @Override public void onClick ( View v ) { displayView . setText ( ” ” ) ; // 定 义 输 入 流 FileInputStream f i s = null ; try { // 取 指 定 对 应 的 存 储 目 录 f i s = openFileInput (FILE_NAME) ; i f ( f i s . a v a i l a b l e ( ) == 0) { return ; } // 定 义 临 时 缓 冲 区 4 4.1 实训步骤 《Android 应用程序开发》广西职业师范学院实验教学指导书 byte [ ] readBytes = new byte [ f i s . a v a i l a b l e ( ) ] ; // 读 取 的 内 容 while ( f i s . read ( readBytes ) != −1) { } // 中 的 String text = new String ( readBytes ) ; displayView . setText ( text ) ; labelView . setText ( ” 读 取 成 功, 长 度: ” + text . length ( ) ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } } ; • 步骤 4: 为 wirte 事绑定监听 // 为 write 绑 定 事 监 听 器 View . OnClickListener writeButtonListener = new View . OnClickListener ( ) { @Override public void onClick ( View v ) { // 定 义 输 出 流 FileOutputStream f o s = null ; try { // 判 断CheckBox是 否 以 追 加 方 式 打 开 输 出 流 i f ( appendBox . isChecked ( ) ) { f o s = openFileOutput (FILE_NAME, Context .MODE_APPEND) ; } else { f o s = openFileOutput (FILE_NAME, Context .MODE_PRIVATE) ; } // 取 EditText 组 中 的 String text = entryText . getText ( ) . toString ( ) ; // 将 t e x t 中 的 内 容 写 入 到 中 f o s . write ( text . get
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值