使用VB6写了一个显示缩略图的模块文件,模块文件中声明了一下函数:Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiPlusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single) As GpStatus
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
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String, Image As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As GpStatus
Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As GpStatus
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
程序中需要调用一个函数,把原图片加载到内存。
这个函数:GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image就是上面这个函数,有时候可以成功调用,有时候却不可以。后来自己仔细查了一下资料,说是跟调用StrConv()函数返回的字符串有关,还有一个类似StrConv()的函数是StrPrt(),用了以后跟使用StrConv()出现的情况一样,甚至我直接在GdipLoadImageFromFile 中使用图片文件路径作为第一个参数也不行,总之,GdipLoadImageFromFile 这个函数有时候返回成功可以显示缩略图,有时却调用失败。关键是StrConv函数的返回值跟GdipLoadImageFromFile 的调用成功与否关系始终没搞清楚。
希望有过类似经历并且已经解决问题的大侠给以指点,谢谢。
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single) As GpStatus
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
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String, Image As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As GpStatus
Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As GpStatus
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
程序中需要调用一个函数,把原图片加载到内存。
这个函数:GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image就是上面这个函数,有时候可以成功调用,有时候却不可以。后来自己仔细查了一下资料,说是跟调用StrConv()函数返回的字符串有关,还有一个类似StrConv()的函数是StrPrt(),用了以后跟使用StrConv()出现的情况一样,甚至我直接在GdipLoadImageFromFile 中使用图片文件路径作为第一个参数也不行,总之,GdipLoadImageFromFile 这个函数有时候返回成功可以显示缩略图,有时却调用失败。关键是StrConv函数的返回值跟GdipLoadImageFromFile 的调用成功与否关系始终没搞清楚。
希望有过类似经历并且已经解决问题的大侠给以指点,谢谢。
的时候, StrConv(ImagePath, vbUnicode)返回的字符串是:D:\ t e s t p i c \ ^轢褢O? 齆) gP杔Q鳶\ 1 . g i f 怎么还会有乱码呢?
StrConv(ImagePath, vbFromUnicode)
分别试一下:StrConv$(ImagePath, vbFromUnicode)
StrConv$(ImagePath, vbnicode)
分别试一下:StrConv$(ImagePath, vbFromUnicode)
StrConv$(ImagePath, vbUnicode)
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End TypePrivate Enum GpStatus 'Status
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End EnumPrivate Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
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
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String, Image As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatusDim gdip_Token As Long
Dim gdip_Image As Long
Dim gdip_Graphics As Long'-------------缩略图函数-----------
Public Sub ShowTNImg(PBox As Object, ImagePath As String, WidthMax As Long, HeightMax As Long)
LoadGDIP
If GdipCreateFromHDC(PBox.hDC, gdip_Graphics) <> 0 Then
MsgBox "出现错误!", vbCritical, "错误"
GdiplusShutdown gdip_Token
End
End If '载入图片到内存中
GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image '使用GDI+直接从内存中缩略并绘图,GDI+有很好的反锯齿能力
If GdipDrawImageRect(gdip_Graphics, gdip_Image, 0, 0, WidthMax, HeightMax) <> Ok Then Debug.Print "显示失败" DisposeGDIP
End SubPublic Sub LoadGDIP()
Dim GpInput As GdiplusStartupInput
GpInput.GdiplusVersion = 1
If GdiplusStartup(gdip_Token, GpInput) <> 0 Then
MsgBox "加载GDI+失败!", vbCritical, "加载错误"
End
End If
End SubPublic Sub DisposeGDIP()
GdipDisposeImage gdip_Image
GdipDeleteGraphics gdip_Graphics
GdiplusShutdown gdip_Token
End Subhttp://hi.baidu.com/pihi/blog/item/2f66da090b52ac86d1581bab.html
看了Modest的代码,自己又修改了自己程序中的代码,问题已经解决了。
在网上也看到一些资料,貌似不论使用StrConv()函数还是使用StrPrt()函数,都会出现GdipLoadImageFromFile调用失败的情况,唯一比较好的解决办法就是使用Modest提供的方法,把图片的存储路径换成以数组的形式保存,然后跳过GdipLoadImageFromFile函数按照字符串路径加载图片到内存的方法是完全可以解决问题的。再次感谢楼上各位的帮助。继续学习,呵呵~~~结贴。