设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3833|回复: 6
打印 上一主题 下一主题

Access 代码实现缩略图,求完善

[复制链接]
跳转到指定楼层
1#
发表于 2016-5-15 09:07:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
网上抄了一个缩略图实现代码,但Access中没有PictureBox 控件,用Access下Image控件的,有些方法和属性不能用,求高手完善附件中的程序,模块代码如下:
  1. Option Compare Database
  2. Option Explicit
  3. '----------------------------------------------------------------------
  4. '----------------------------------------------------------------------
  5. '----------------使用者请保留作者版权----------------------------------
  6. '--  作者:BEAR-BEN  ---------------------------------------------------
  7. '--  QQ:453628001  ----------------------------------------------------
  8. '--  天才动力 --- GENIUS POWER  ---------------------------------------
  9. '--  WebSite:www.tcdongli.com  ----------------------------------------
  10. '----------------------------------------------------------------------
  11. '----------------------------------------------------------------------
  12. '****************模块

  13. Public Type ImageInfo
  14.    Height As Long
  15.    Width As Long
  16.    FilePath As String
  17.    ImageName As String
  18.    Type As String
  19.    FileSize As Long   'KB
  20. End Type

  21. Private Type GdiplusStartupInput
  22.     GdiplusVersion As Long
  23.     DebugEventCallback As Long
  24.     SuppressBackgroundThread As Long
  25.     SuppressExternalCodecs As Long
  26. End Type
  27. Private Enum GpStatus   'Status
  28.     Ok = 0
  29.     GenericError = 1
  30.     InvalidParameter = 2
  31.     OutOfMemory = 3
  32.     ObjectBusy = 4
  33.     InsufficientBuffer = 5
  34.     NotImplemented = 6
  35.     Win32Error = 7
  36.     WrongState = 8
  37.     Aborted = 9
  38.     FileNotFound = 10
  39.     ValueOverflow = 11
  40.     AccessDenied = 12
  41.     UnknownImageFormat = 13
  42.     FontFamilyNotFound = 14
  43.     FontStyleNotFound = 15
  44.     NotTrueTypeFont = 16
  45.     UnsupportedGdiplusVersion = 17
  46.     GdiplusNotInitialized = 18
  47.     PropertyNotFound = 19
  48.     PropertyNotSupported = 20
  49. End Enum

  50. Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
  51. Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
  52. Private Declare Function GdipDrawImage Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single) As GpStatus
  53. Private Declare Function GdipDrawImageRect Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single) As GpStatus
  54. Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
  55. Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
  56. Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String, Image As Long) As GpStatus
  57. Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
  58. Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As GpStatus
  59. Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As GpStatus

  60. Private Declare Function GdipDrawImageRectI Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Long, ByVal Y As Long, ByVal Width As Long, ByVal Height As Long) As GpStatus

  61. Dim gdip_Token As Long
  62. Dim gdip_Image As Long
  63. Dim gdip_Graphics As Long

  64. '-------------缩略图函数-----------
  65. Public Function ShowTNImg(PBox As Object, ImagePath As String, WMax As Long, HMax As Long) As ImageInfo
  66. 'WMax为最大宽度,HMax为最大高度,这段代码会根据最大宽度和高度调整图片大小。

  67. Dim Wid As Long, Hgt As Long, Top As Long, Left As Long

  68. LoadGDIP
  69. '
  70. '' If GdipCreateFromHDC(PBox.hDC, gdip_Graphics) <> 0 Then
  71. If GdipCreateFromHDC(PBox.Picture, gdip_Graphics) <> 0 Then
  72.      MsgBox "出现错误!", vbCritical, "错误"
  73.      GdiplusShutdown gdip_Token
  74.      End
  75. End If

  76. '载入图片到内存中
  77. GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image

  78. '获取图片长和宽
  79. GdipGetImageWidth gdip_Image, Wid
  80. GdipGetImageHeight gdip_Image, Hgt

  81. With ShowTNImg
  82.    .Width = Wid
  83.    .Height = Hgt
  84.    .FilePath = ImagePath
  85.    .FileSize = FileLen(ImagePath) / 1024
  86.    .ImageName = Right(ImagePath, Len(ImagePath) - InStrRev(ImagePath, ""))
  87.    .Type = Right(ImagePath, Len(ImagePath) - InStrRev(ImagePath, "."))
  88. End With


  89. '智能调整图片大小和留空处理,根据最长边调整
  90. If (Wid > WMax) Or (Hgt > HMax) Then
  91.    If Wid > Hgt Then
  92.      Hgt = Hgt / Wid * WMax
  93.      Wid = WMax
  94.      Top = (HMax - Hgt) / 2
  95.    Else
  96.      Wid = Wid / Hgt * HMax
  97.      Hgt = HMax
  98.      Left = (WMax - Wid) / 2
  99.    End If
  100. Else
  101.    Top = (HMax - Hgt) / 2
  102.    Left = (WMax - Wid) / 2
  103. End If
  104.    

  105. '使用GDI+直接从内存中缩略并绘图,GDI+有很好的抗锯齿能力
  106. If GdipDrawImageRect(gdip_Graphics, gdip_Image, Left, Top, Wid, Hgt) <> 1 Then Debug.Print "显示失败。。。"

  107. DisposeGDIP

  108. End Function


  109. '加载显示完整图片
  110. Public Sub ShowFullImg(PBox As Picture, ImagePath As String)

  111. LoadGDIP

  112. If GdipCreateFromHDC(PBox.hDC, gdip_Graphics) <> Ok Then
  113.      MsgBox "出现错误!", vbCritical, "错误"
  114.      GdiplusShutdown gdip_Token
  115.      End
  116. End If

  117. GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image

  118. If GdipDrawImage(gdip_Graphics, gdip_Image, 0, 0) <> Ok Then Debug.Print "显示失败。。。"

  119. DisposeGDIP

  120. End Sub


  121. Public Sub LoadGDIP()
  122. Dim GpInput As GdiplusStartupInput
  123. GpInput.GdiplusVersion = 1

  124. If GdiplusStartup(gdip_Token, GpInput) <> 0 Then
  125.      MsgBox "加载GDI+失败!", vbCritical, "加载错误"
  126.      End
  127. End If
  128. End Sub

  129. Public Sub DisposeGDIP()
  130.    GdipDisposeImage gdip_Image
  131.    GdipDeleteGraphics gdip_Graphics
  132.    GdiplusShutdown gdip_Token
  133. End Sub

复制代码




























本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2016-5-16 00:11:30 | 只看该作者
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus

这个hDC应该是设备场景的句柄,应该等同于hwnd之类。没时间一步步帮你调试了。
picture如何获取句柄,请参考下小宝发的帖子:
http://www.office-cn.net/thread-106783-1-1.html
不过这个不是用GDI+,而是GDI的。
---------------------------------------------------------------------
'另外可以考虑使用这个API。
' 从内存中用指定大小和格式建立位图,位图句柄在Bitmap参数内返回
Private Declare Function GdipCreateBitmapFromScan0 Lib "gdiplus" (ByVal Width As Long, ByVal Height As Long, ByVal Stride As Long, ByVal PixelFormat As GpPixelFormat, Scan0 As Any, Bitmap As Long) As Long

-----------------------------------------------------------------------
不过,在VBA里使用GDI+困难始终很大。
3#
发表于 2016-5-16 11:29:26 | 只看该作者
很在意性能吗?
如果不那么在意,可以参看一下冬瓜汤的这篇文章,用法要简单地多了
http://www.office-cn.net/home.ph ... o=blog&id=15233
4#
 楼主| 发表于 2016-5-16 11:30:13 | 只看该作者
谢谢roych,对API不懂,还是搞不起来,但是还是要谢谢,希望有高手继续完善……
5#
 楼主| 发表于 2016-5-16 14:17:57 | 只看该作者
谢谢ganlinlao,WIA确实很强大,也很方便,虽然不能用代码动态来显示缩略图,而且还有锯齿,但也提供了另一种解决方法
6#
发表于 2016-5-18 21:19:44 | 只看该作者
本帖最后由 cgsilicone 于 2016-5-18 21:34 编辑

API其实很简单的,采用API按以下三步,简单修改就可以实现动态显示缩略图:

1、先新建立一个没有记录选择器、导航按钮、滚动条的窗体(特别注意,必需完全按以上要求建新窗体),然后将Image控件改为子窗体控件(新建窗体为子窗体),子窗体控件名为“子窗体”。

2、然后在模块程序中加入三句代码:
Public Declare Function 获取DC Lib "user32" Alias "GetDC" (ByVal 窗体句柄 As Long) As Long

Public Declare Function 释放DC Lib "user32" Alias "ReleaseDC" (ByVal 窗体句柄 As Long, ByVal 设备场景句柄 As Long) As Long

Public Declare Function 寻找窗体 Lib "user32" Alias "FindWindowExA" (ByVal 窗体句柄 As Long, ByVal 起始窗体句柄 As Long, ByVal 窗体类名 As String, ByVal 窗体名称 As String) As Long
   
同时修改代码: PBox As Object,为 hDC as long,PBox.hDC 为hDC。

3、将窗体Click()中代码修改为如下代码:

Dim 窗体句柄 As Long

Dim 绘图场景DC As Long

窗体句柄 = 0

窗体句柄 = 寻找窗体(Me.子窗体.Form.hWnd, 窗体句柄, vbNullString, vbNullString)

窗体句柄 = 寻找窗体(Me.子窗体.Form.hWnd, 窗体句柄, vbNullString, vbNullString)

绘图场景DC = 获取DC(窗体句柄)

ShowTNImg 绘图场景DC, "文件名", 100, 200 ' 根据实际修改文件名

释放DC Me.hWnd, 绘图场景DC




7#
发表于 2016-7-10 00:15:57 | 只看该作者
序        设备编号        设备名称        制造厂        出厂编号        安装地点        原值        启用时间        保养周期        设备类别 3        B123456        家用        滨江        789        黄山        ¥50.00        2004/9/4        6个月        办公用品
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-29 04:41 , Processed in 0.121785 second(s), 31 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表