VB中使用GDI+进行图像缩放的实例

前端之家收集整理的这篇文章主要介绍了VB中使用GDI+进行图像缩放的实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VISUAL BASIC(VB)对图形图像的处理一直以来是弱项,并受到很多人的垢病。关于图形图像的放大缩小,一般使用PICTUREBox的PAINTPICTURE方法来处理。但这个处理方法最大的问题就是图像的失真。比方说图像中原来有网格线的,处理以后网格线会丢失,处理效果不好。后来在网上找到几个材料,是关于GDI+的用法的。试验了一下,果然效果非凡。以下为程序处理的关键代码

  1. Private Type GdiplusStartupInput
  2. GdiplusVersion As Long
  3. DebugEventCallback As Long
  4. SuppressBackgroundThread As Long
  5. SuppressExternalCodecs As Long
  6. End Type
  7. Private Enum GpStatus 'Status
  8. Ok = 0
  9. GenericError = 1
  10. InvalidParameter = 2
  11. OutOfMemory = 3
  12. ObjectBusy = 4
  13. InsufficientBuffer = 5
  14. NotImplemented = 6
  15. Win32Error = 7
  16. WrongState = 8
  17. Aborted = 9
  18. FileNotFound = 10
  19. ValueOverflow = 11
  20. AccessDenied = 12
  21. UnknownImageFormat = 13
  22. FontFamilyNotFound = 14
  23. FontStyleNotFound = 15
  24. NotTrueTypeFont = 16
  25. UnsupportedGdiplusVersion = 17
  26. GdiplusNotInitialized = 18
  27. PropertyNotFound = 19
  28. PropertyNotSupported = 20
  29. End Enum
  30. Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long,inputbuf As GdiplusStartupInput,Optional ByVal outputbuf As Long = 0) As GpStatus
  31. Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
  32. 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
  33. Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long,graphics As Long) As GpStatus
  34. Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
  35. Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String,Image As Long) As GpStatus
  36. Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
  37. Dim gdip_Token As Long
  38. Dim gdip_Image As Long
  39. Dim gdip_Graphics As Long
  40. '--------------------------------------
  41. '-- 使用者请保留作者版权
  42. '-- 作者:BEAR-BEN
  43. '-- QQ:453628001
  44. '--------------------------------------
  45. '-------------缩略图函数-----------
  46. Public Sub ShowTNImg(PBox As Object,ImagePath As String,WidthMax As Long,HeightMax As Long)
  47. LoadGDIP
  48. If GdipCreateFromHDC(PBox.hDC,gdip_Graphics) <> 0 Then
  49. MsgBox "出现错误!",vbCritical,"错误"
  50. GdiplusShutdown gdip_Token
  51. End
  52. End If
  53. '载入图片到内存中
  54. GdipLoadImageFromFile StrConv(ImagePath,vbUnicode),gdip_Image
  55. '使用GDI+直接从内存中缩略并绘图,GDI+有很好的反锯齿能力
  56. If GdipDrawImageRect(gdip_Graphics,gdip_Image,WidthMax,HeightMax) <> Ok Then Debug.Print "显示失败。。。"
  57. DisposeGDIP
  58. End Sub
  59. Public Sub LoadGDIP()
  60. Dim GpInput As GdiplusStartupInput
  61. GpInput.GdiplusVersion = 1
  62. If GdiplusStartup(gdip_Token,GpInput) <> 0 Then
  63. MsgBox "加载GDI+失败!","加载错误"
  64. End
  65. End If
  66. End Sub
  67. Public Sub DisposeGDIP()
  68. GdipDisposeImage gdip_Image
  69. GdipDeleteGraphics gdip_Graphics
  70. GdiplusShutdown gdip_Token
  71. End Sub

猜你在找的VB相关文章