我最近在做一个软件需要用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格式用上面的语句显示一切正常,图像没有被拉伸,请问这个是怎么回事?如何处理?

解决方案 »

  1.   

    '完整代码如下,谢谢。Option ExplicitPrivate Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As LongPrivate Type GdiplusStartupInput
      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
      

  2.   

    楼主为何化简为繁?jpg格式用LoadPicture读入即可,简单可靠.
      

  3.   

    因为我需要用到较多的PNG文件,JPG、GIF较少,这段代码是修改后用来统一读取图像文件的。PS:经过我刚才的测试,用PhotoshopCS5和FireworksCS5处理保存过的JPG文件就会出现拉伸变形的问题,如果用ACDSEE转换出来的JPG就一切正常,不知道为什么?
      

  4.   

    用GetShortPathName获取路径
    用Trim清除多余的字符