一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

VB小程序VB代碼:將圖片保存或轉(zhuǎn)變?yōu)镴PG格式

 weikong66 2012-04-19

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type
Private Type GdiplusStartupInput
    GdiplusVersion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
    nGUID As GUID
    NumberOfValues As Long
    Type As Long
    Value As Long
End Type
Private Type EncoderParameters
    Count As Long
    Parameter As EncoderParameter
End Type
Enum PicType
   p_BMP
   p_JPG
   p_GIF
   p_PNG
   p_TIFF
End Enum
Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hPal As Long, BITMAP As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal FileName As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal Str As Long, id As GUID) As Long
Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb As Long) As Long

Public Function SavePicToFile(ByVal nPic As StdPicture, ByVal FileName As String, _
   Optional ByVal nType As PicType = p_JPG, Optional ByVal Quality As Byte = 80, _
   Optional ByVal TIFF_ColorDepth As Long = 24, Optional ByVal TIFF_Compression As Long = 6) As String
   '功能:把圖象保存為 BMP、JPG、GIF、PNG、TIFF 格式。成功返回空字符串,失敗返回錯(cuò)誤信息
   '如果保存的文件名無擴(kuò)展名,則自動(dòng)添加相應(yīng)的擴(kuò)展名
   'StdPicture)          圖象句柄
   'FileName             保存文件名
   'nType                文件格式:0 BMP 1 JPG 2 GIF 3 PNG 4 TIFF
   'Quality              JPG 圖象質(zhì)量
   'TIFF_ColorDepth      TTF 格式的顏色深度
   'TIFF_Compression     TTF 格式的壓縮比

   Dim dl As Long, nGDIP As Long, nBMP As Long
   Dim nGSI As GdiplusStartupInput, B() As Byte
  
   On Error GoTo Cuo
   nGSI.GdiplusVersion = 1   ' 初始化 GDI+
   dl = GdiplusStartup(nGDIP, nGSI)
   If dl <> 0 Then SavePicToFile = "無法創(chuàng)建 GDI 圖像": Exit Function
  
   dl = GdipCreateBitmapFromHBITMAP(nPic.Handle, 0, nBMP)
   If dl <> 0 Then GdiplusShutdown nGDIP: SavePicToFile = "不支持圖片格式": Exit Function
  
   Dim mGUID As GUID, mEP As EncoderParameters '初始化解碼器的 GUID 標(biāo)識(shí)
   Select Case nType
   Case p_JPG
     If LCase(Right(FileName, 4)) <> ".jpg" Then FileName = FileName & ".jpg"
      CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), mGUID
      mEP.Count = 1     ' 設(shè)置解碼器參數(shù)
      With mEP.Parameter
         CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .nGUID '得到 GUID 標(biāo)識(shí)
         .NumberOfValues = 1
         .Type = 4
         .Value = VarPtr(Quality)
      End With
      ReDim B(1 To Len(mEP))
      Call CopyMemory(B(1), mEP, Len(mEP))
   Case p_GIF
      If LCase(Right(FileName, 4)) <> ".gif" Then FileName = FileName & ".gif"
      CLSIDFromString StrPtr("{557CF402-1A04-11D3-9A73-0000F81EF32E}"), mGUID
      ReDim B(1 To Len(mEP))
   Case p_PNG
      If LCase(Right(FileName, 4)) <> ".png" Then FileName = FileName & ".png"
      CLSIDFromString StrPtr("{557CF406-1A04-11D3-9A73-0000F81EF32E}"), mGUID
      ReDim B(1 To Len(mEP))
   Case p_TIFF
      If LCase(Right(FileName, 5)) <> ".tiff" Then FileName = FileName & ".tiff"
      CLSIDFromString StrPtr("{557CF405-1A04-11D3-9A73-0000F81EF32E}"), mGUID
      mEP.Count = 2
      ReDim B(1 To Len(mEP) + Len(mEP.Parameter))
      With mEP.Parameter
         .NumberOfValues = 1
         .Type = 4
          CLSIDFromString StrPtr("{E09D739D-CCD4-44EE-8EBA-3FBF8BE4FC58}"), .nGUID
         .Value = VarPtr(TIFF_Compression)
       End With
       Call CopyMemory(B(1), mEP, Len(mEP))
       With mEP.Parameter
           .NumberOfValues = 1
           .Type = 4
            CLSIDFromString StrPtr("{66087055-AD66-4C7C-9A18-38A2310B8337}"), .nGUID
           .Value = VarPtr(TIFF_ColorDepth)
       End With
       Call CopyMemory(B(Len(mEP) + 1), mEP.Parameter, Len(mEP.Parameter))
   Case Else 'p_BMP 沒有使用 GDI+
       If LCase(Right(FileName, 4)) <> ".bmp" Then FileName = FileName & ".bmp"
       SavePicture nPic, FileName
       Exit Function
   End Select
   dl = GdipSaveImageToFile(nBMP, StrPtr(FileName), mGUID, B(1)) '保存到文件
   GdipDisposeImage nBMP       '銷毀 GDI+ 圖像
   GdiplusShutdown nGDIP       '銷毀 GDI+
   Exit Function

Cuo:
   SavePicToFile = "錯(cuò)誤 " & Err.Number & ":" & Err.Description
End Function

Private Sub Form_Load()
   Me.Caption = "圖片格式轉(zhuǎn)換": Command1.Caption = "轉(zhuǎn)換"
   Text1.Text = "E:\MyPic.bmp"
End Sub

Private Sub Command1_Click()
   Dim nStr As String, F As String
   Picture1.AutoSize = True: Command1.ZOrder
   F = Trim(Text1.Text)
   Picture1.Picture = LoadPicture(F)
   '默認(rèn)保存為 JPG 格式,如果無擴(kuò)展名,則自動(dòng)添加擴(kuò)展名。成功返回空字符串
   F = NoKuo(F) '去掉原擴(kuò)展名
   nStr = SavePicToFile(Picture1.Picture, F)
   If nStr <> "" Then MsgBox nStr
End Sub

Private Function NoKuo(F As String) As String
   Dim I As Long
   For I = Len(F) To 1 Step -1
      If Mid(F, I, 1) = "." Then NoKuo = Left(F, I - 1): Exit Function
   Next
   NoKuo = F
End Function

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    丰满少妇高潮一区二区| 伊人久久青草地婷婷综合| 午夜视频成人在线免费| 欧美加勒比一区二区三区| 一区二区三区四区亚洲专区| 免费人妻精品一区二区三区久久久| 日韩1区二区三区麻豆| 日本午夜福利视频免费观看| 国产偷拍盗摄一区二区| 黄色在线免费高清观看| 国产精品一区二区三区黄色片| 久久热麻豆国产精品视频| 亚洲日本中文字幕视频在线观看 | 国语久精品在视频在线观看| 中文字幕日韩一区二区不卡| 中文字幕在线五月婷婷| 久久国产亚洲精品赲碰热| 久草国产精品一区二区| 国产精品激情对白一区二区| 欧美精品久久一二三区| 久久人人爽人人爽大片av| 国产精品欧美一区二区三区不卡| 国产精品成人又粗又长又爽| 国产一区二区三区午夜精品| 粉嫩国产一区二区三区在线| 午夜精品一区二区av| 中文字幕区自拍偷拍区| 亚洲精品中文字幕在线视频| 日韩精品在线观看完整版| 久久亚洲成熟女人毛片| 国产主播精品福利午夜二区| 老司机激情五月天在线不卡| 久久精品国产一区久久久| 亚洲天堂国产精品久久精品| 精品国产亚洲av久一区二区三区| 欧美精品日韩精品一区| 成人三级视频在线观看不卡| 91精品国产综合久久不卡| 欧美黑人在线精品极品| 亚洲一区二区三区免费的视频| 男人把女人操得嗷嗷叫|