我最近在做一个软件需要用GDI+打开图像,使用如下语句: Picname = GetShortName("E:\1.jpg")
GdipLoadImageFromFile StrConv(Picname, vbUnicode), Gdip_Image“1.jpg”的分辨率是700x500,显示在Picture1里面,Picture1也设置为700x500,读取之后发现图像没有显示完全,大概只显示了500x350左右的图像,而且这个图像是经过被拉伸之后占满700x500空间的,然后我把这个1.jpg转换为bmp、png、gif格式用上面的语句显示一切正常,图像没有被拉伸,请问这个是怎么回事?如何处理?
GdipLoadImageFromFile StrConv(Picname, vbUnicode), Gdip_Image“1.jpg”的分辨率是700x500,显示在Picture1里面,Picture1也设置为700x500,读取之后发现图像没有显示完全,大概只显示了500x350左右的图像,而且这个图像是经过被拉伸之后占满700x500空间的,然后我把这个1.jpg转换为bmp、png、gif格式用上面的语句显示一切正常,图像没有被拉伸,请问这个是怎么回事?如何处理?
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Enum GpStatus
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 Enum
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 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 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 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
Private Declare Function GdipLoadImageFromStream Lib "GDIPlus" (ByVal Stream As Any, ByRef Image As Long) As LongDim Gdip_Token As Long, Gdip_Image As Long, Gdip_Graphics As Long, Picname As StringPrivate Sub Command1_Click()
Picname = GetShortName("E:\1.jpg")
GdipLoadImageFromFile StrConv(Picname, vbUnicode), Gdip_Image
If GdipDrawImage(Gdip_Graphics, Gdip_Image, 0, 0) <> Ok Then MsgBox "显示失败"
Picture1.Refresh
End SubFunction GetShortName(ByVal sLongFileName As String) As String
Dim lRetVal&, sShortPathName$
sShortPathName = Space(255)
Call GetShortPathName(sLongFileName, sShortPathName, 255)
If InStr(sShortPathName, Chr(0)) > 0 Then
GetShortName = Trim(Mid(sShortPathName, 1, InStr(sShortPathName, Chr(0)) - 1))
Else
GetShortName = Trim(sShortPathName)
End If
End FunctionPrivate Sub Form_Load()
Dim GpInput As GdiplusStartupInput
GpInput.GdiplusVersion = 1
If GdiplusStartup(Gdip_Token, GpInput) <> Ok Then MsgBox "GDI初始失败!": Unload Me
Picture1.AutoRedraw = True
If GdipCreateFromHDC(Picture1.hDC, Gdip_Graphics) <> Ok Then GdiplusShutdown Gdip_Token: Unload Me
End SubPrivate Sub Form_Unload(Cancel As Integer)
GdipDisposeImage Gdip_Image
GdipDeleteGraphics Gdip_Graphics
GdiplusShutdown Gdip_Token
End
End Sub
用Trim清除多余的字符