用VB进行GDI+绘图

用VB进行GDI+绘图,第1张

概述正巧处理图形,突然发现一个很强的库GDI+,遂恶补一番!   VERSION 5.00 Begin VB.Form frmMain    BackColor       =   &H8000000A&    BorderStyle     =   1  'Fixed Single    Caption         =   "品雅图片转换工具  Ver 2.0        (Power By

正巧处理图形,突然发现一个很强的库GDI+,遂恶补一番!

VERSION 5.00
Begin VB.Form frmMain
Backcolor = &H8000000A&
borderStyle = 1 'Fixed Single
Caption = "品雅图片转换工具 Ver 2.0 (Power By 赵洪涛 2008.12 Email:[email protected])"
ClIEntHeight = 7590
ClIEntleft = 45
ClIEnttop = 330
ClIEntWIDth = 9480
KeyPrevIEw = -1 'True
linktopic = "Form1"
Maxbutton = 0 'False
ScaleHeight = 379.5
ScaleMode = 2 'Point
ScaleWIDth = 474
StartUpposition = 2 '屏幕中心
Begin VB.Frame Frame1
Backcolor = &H8000000A&
Caption = " 选项设置 "
Height = 1455
left = 240
TabIndex = 5
top = 240
WIDth = 9015
Begin VB.ComboBox Combo1
Height = 300
ItemData = "Form1.frx":0000
left = 6240
List = "Form1.frx":000D
Style = 2 'Dropdown List
TabIndex = 21
top = 1027
WIDth = 855
End
Begin VB.Commandbutton Command4
Backcolor = &H00FFFFFF&
Caption = "清除"
Height = 375
left = 4080
Style = 1 'Graphical
TabIndex = 20
tooltipText = "从列表中移除选定的项"
top = 990
WIDth = 855
End
Begin VB.Commandbutton Command2
Appearance = 0 'Flat
Backcolor = &H00FFFFFF&
Caption = "选择图片"
Height = 375
left = 3120
Style = 1 'Graphical
TabIndex = 19
tooltipText = "插入新图片"
top = 990
WIDth = 855
End
Begin VB.Commandbutton Command1
Caption = "开始转换 ...(&C)"
Height = 375
left = 7275
TabIndex = 0
top = 990
WIDth = 1575
End
Begin VB.TextBox Text3
Enabled = 0 'False
Height = 270
left = 5760
MaxLength = 4
TabIndex = 2
Text = "768"
top = 225
WIDth = 615
End
Begin VB.TextBox Text2
Enabled = 0 'False
Height = 270
left = 3720
MaxLength = 4
TabIndex = 1
Text = "1024"
top = 225
WIDth = 615
End
Begin VB.Optionbutton Option4
Backcolor = &H8000000A&
Caption = "保持原大小,不进行缩放"
Height = 255
left = 240
TabIndex = 10
top = 1110
Value = -1 'True
WIDth = 2295
End
Begin VB.Optionbutton Option3
Backcolor = &H8000000A&
Caption = "自定义尺寸进行等比缩放"
Height = 255
left = 240
TabIndex = 9
top = 820
WIDth = 2295
End
Begin VB.Optionbutton Option2
Backcolor = &H8000000A&
Caption = "以高度为准进行等比缩放"
Height = 255
left = 240
TabIndex = 8
top = 530
WIDth = 2295
End
Begin VB.Optionbutton Option1
Backcolor = &H8000000A&
Caption = "以宽度为准进行等比缩放"
Height = 255
left = 240
TabIndex = 7
top = 240
WIDth = 2295
End
Begin VB.TextBox Text1
Height = 270
left = 8010
MaxLength = 3
TabIndex = 3
Text = "80"
top = 225
WIDth = 615
End
Begin VB.Label Label10
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "转换成:"
Forecolor = &H00FF0000&
Height = 180
left = 5490
TabIndex = 22
top = 1087
WIDth = 720
End
Begin VB.Label Label4
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "0 %"
Forecolor = &H0000FFFF&
Height = 180
left = 5880
TabIndex = 16
top = 645
WIDth = 270
End
Begin VB.Label Label3
Backcolor = &H00FF0000&
Height = 315
left = 3135
TabIndex = 17
top = 585
WIDth = 15
End
Begin VB.Label Label9
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "%"
Forecolor = &H00404040&
Height = 180
left = 8760
TabIndex = 15
top = 270
WIDth = 90
End
Begin VB.Label Label8
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "像素"
Forecolor = &H00404040&
Height = 180
left = 6420
TabIndex = 14
top = 270
WIDth = 360
End
Begin VB.Label Label7
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "高度:"
Forecolor = &H00FF0000&
Height = 180
left = 5160
TabIndex = 13
top = 270
WIDth = 540
End
Begin VB.Label Label6
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "像素"
Forecolor = &H00404040&
Height = 180
left = 4380
TabIndex = 12
top = 270
WIDth = 360
End
Begin VB.Label Label5
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "宽度:"
Forecolor = &H00FF0000&
Height = 180
left = 3120
TabIndex = 11
top = 270
WIDth = 540
End
Begin VB.Label Label2
autoSize = -1 'True
BackStyle = 0 'transparent
Caption = "清晰度:"
Forecolor = &H00FF0000&
Height = 180
left = 7200
TabIndex = 6
top = 270
WIDth = 720
End
Begin VB.Label Label1
Appearance = 0 'Flat
Backcolor = &H00808080&
borderStyle = 1 'Fixed Single
Forecolor = &H80000008&
Height = 345
left = 3120
TabIndex = 18
top = 570
WIDth = 5730
End
End
Begin VB.ListBox List1
Height = 5460
left = 240
MultiSelect = 2 'Extended
TabIndex = 4
top = 1920
WIDth = 9015
End
End
Attribute VB_name = "frmMain"
Attribute VB_GlobalnameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredID = True
Attribute VB_Exposed = False
Option Explicit



Private Type OPENfilename
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrfile As String
nMaxfile As Long
lpstrfileTitle As String
nMaxfileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nfileOffset As Integer
nfileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplatename As String
End Type

Private Type DlgfileInfo
iCount As Long
sPath As String
sfile() As String
picType() As Integer
End Type

Private Type GUID ' 16 bytes (128 bits)
DWData1 As Long ' 4 bytes
wData2 As Integer ' 2 bytes
wData3 As Integer ' 2 bytes
abData4(7) As Byte ' 8 bytes,zero based
End Type



Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type

Private Type EncoderParameters
count As Long
Parameter As EncoderParameter
End Type

Private Type Gdiplusstartupinput
Gdiplusversion As Long
DeBUGEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type

Public Enum GpUnit ' aka Unit
UnitWorld ' 0 -- World coordinate (non-physical unit)
Unitdisplay ' 1 -- Variable -- for Pagetransform only
UnitPixel ' 2 -- Each unit is one device pixel.
UnitPoint ' 3 -- Each unit is a printer's point,or 1/72 inch.
UnitInch ' 4 -- Each unit is 1 inch.
Unitdocument ' 5 -- Each unit is 1/300 inch.
UnitMillimeter ' 6 -- Each unit is 1 millimeter.
End Enum

Public 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 Enum

Public Enum GpPixelFormat
' PixelFormat1bppIndexed = &H30101
' PixelFormat4bppIndexed = &H30402
' PixelFormat8bppIndexed = &H30803
' PixelFormat16bppGreyScale = &H101004
' PixelFormat16bppRGB555 = &H21005
' PixelFormat16bppRGB565 = &H21006
' PixelFormat16bppARGB1555 = &H61007
PixelFormat24bppRGB = &H21808
' PixelFormat32bppRGB = &H22009
' PixelFormat32bppARGB = &H26200A
' PixelFormat32bppPARGB = &HE200B
' PixelFormat48bppRGB = &H10300C
' PixelFormat64bppARGB = &H34400D
' PixelFormat64bppPARGB = &H1C400E
End Enum
Dim cPicPath As String

Private Const OFN_Readonly = &H1 '“以只读方式”为选中
Private Const OFN_OVERWRITEPROMPT = &H2 '隐藏“以只读方式”
Private Const OFN_HIDEReadonly = &H4 '出现“是否覆盖”对话框
Private Const OFN_NOCHANGEDIR = &H8 '不能改变目录
Private Const OFN_SHOWHELP = &H10 '显示“帮助”
Private Const OFN_ENABLEHOOK = &H20 '使对话框钩子函数生效
Private Const OFN_ENABLETEMPLATE = &H40 '模板生效
Private Const OFN_ENABLETEMPLATEHANDLE = &H80 '模板句柄生效??
Private Const OFN_NovalIDATE = &H100 '允许非法字符
Private Const OFN_ALLOWMulTISELECT = &H200 '允许选择多个文件
Private Const OFN_EXTENSIONDIFFERENT = &H400
Private Const OFN_PATHMUSTEXIST = &H800 '路径必须存在
Private Const OFN_fileMUSTEXIST = &H1000 '文件必须存在
Private Const OFN_CREATEPROMPT = &H2000 '出现“是否建立文件”对话框
Private Const OFN_SHAREAWARE = &H4000 '忽略共享冲突
Private Const OFN_NOReadonlyRETURN = &H8000
Private Const OFN_NOTESTfileCREATE = &H10000 '不进行文件创建测试
Private Const OFN_NONETWORKbutton = &H20000 '没有网络按键(旧风格专用)
Private Const OFN_NolONGnameS = &H40000 '不使用长文件名(旧风格专用)
Private Const OFN_EXPLORER = &H80000 '资源管理器风格(新风格)
Private Const OFN_NODEREFERENCElinkS = &H100000 '使*.lnk可以选中
Private Const OFN_LONGnameS = &H200000 '使用长文件名(旧风格专用)
Private Const OFN_ENABLEINCLUDENOTIFY = &H400000 '准许包括通知??
Private Const OFN_ENABLESIZING = &H800000 '可改变大小
Private Const OFN_USEMONIKERS = &H1000000
Private Const OFN_DONTADDTORECENT = &H2000000
Private Const OFN_FORCESHOWHIDDEN = &H10000000


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 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 GdipdisposeImage lib "gdiplus" (ByVal image As Long) As GpStatus
Private Declare Function GdipCreateImageAttributes lib "gdiplus" (imageattr As Long) As GpStatus
Private Declare Function GdipdisposeImageAttributes lib "gdiplus" (ByVal imageattr 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 GdipDrawImageRectRect lib "gdiplus" (ByVal graphics As Long,ByVal dstx As Single,ByVal dsty As Single,ByVal dstwIDth As Single,ByVal dstheight As Single,ByVal SrcX As Single,ByVal SrcY As Single,ByVal srcwIDth As Single,ByVal srcheight As Single,ByVal srcUnit As GpUnit,Optional ByVal imageAttributes As Long = 0,Optional ByVal callback As Long = 0,Optional ByVal callbackData As Long = 0) As GpStatus
Private Declare Function GdipCreateBitmapFromScan0 lib "gdiplus" (ByVal WIDth As Long,ByVal Height As Long,ByVal strIDe As Long,ByVal PixelFormat As Long,scan0 As Any,bitmap As Long) As GpStatus
Private Declare Function GdipCreateBitmapFromHBITMAP lib "gdiplus" (ByVal hbm As Long,ByVal hPal As Long,bitmap As Long) As GpStatus
Private Declare Function GdipSaveImagetofile lib "gdiplus" (ByVal image As Long,ByVal filename As Long,clsIDEncoder As GUID,encoderParams As Any) As GpStatus
Private Declare Function GdipGetimageGraphicsContext lib "gdiplus" (ByVal image As Long,ByRef graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromfile lib "gdiplus" (ByVal filename As Long,image As Long) As GpStatus
Private Declare Function GdipGetimageDimension lib "gdiplus" (ByVal image As Long,ByRef WIDth As Single,ByRef Height As Single) As GpStatus

Private Declare Function GdipGraphicsClear lib "gdiplus" (ByVal graphics As Long,ByVal lcolor As Long) As GpStatus

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

Private Declare Function Getopenfilename lib "comdlg32.dll" Alias "GetopenfilenameA" (pOpenfilename As OPENfilename) As Long
Private Declare Function GetDC lib "user32" (ByVal hwnd As Long) As Long


Private Function GetDlgSelectfileInfo(strfilename As String) As DlgfileInfo

'思路: 用CommonDialog控件选择文件后,其filename属性值如下:
' 1、如果选择的是"C:/Test.txt", filename="C:/Test.txt", CurDir()="C:/"
' 2、如果选择的是"C:/1/Test.txt",filename="C:/1/Test.txt", CurDir()="C:/1"
' 3、如果选择的是"C:/1.txt"和"C:/2.txt",则:
' filename="C:/1 1.txt 2.txt", CurDir()="C:/1"
' 因此先将路径分离开,再利用多文件之间插入的Chr$(0)字符分解各个文件名即可。

Dim sPath,tmpStr As String
Dim sfile() As String
Dim iCount As Integer
Dim i As Integer,n As Integer,nold As Integer

tmpStr = Trim(strfilename)
If Len(tmpStr) = 1 Then Exit Function

i = 1
nold = 0
n = 1

do while i > 0
n = InStr(nold + 1,tmpStr,Chr$(0),vbBinaryCompare)
If n - nold > 1 Then
iCount = iCount + 1
ReDim Preserve sfile(iCount)
sfile(iCount) = MID$(tmpStr,nold + 1,n - nold - 1)
nold = n
Else
i = 0
End If
Loop

If iCount <> 1 Then Exit Function

If iCount = 1 Then
n = InStrRev(sfile(1),"/")

GetDlgSelectfileInfo.iCount = 1
GetDlgSelectfileInfo.sPath = MID(sfile(1),1,n)

ReDim GetDlgSelectfileInfo.sfile(1)
GetDlgSelectfileInfo.sfile(1) = MID(sfile(1),n + 1)
ReDim GetDlgSelectfileInfo.picType(1)
Select Case UCase(Right(GetDlgSelectfileInfo.sfile(1),4))
Case ".BMP"
GetDlgSelectfileInfo.picType(1) = 1
Case ".GIF"
GetDlgSelectfileInfo.picType(1) = 2
Case Else
GetDlgSelectfileInfo.picType(1) = 3
End Select
Else
GetDlgSelectfileInfo.iCount = iCount - 1
ReDim GetDlgSelectfileInfo.sfile(iCount - 1)
ReDim GetDlgSelectfileInfo.picType(iCount - 1)

If Right$(sfile(1),1) <> "/" Then sfile(1) = sfile(1) & "/"
GetDlgSelectfileInfo.sPath = sfile(1)

For i = 2 To iCount
GetDlgSelectfileInfo.sfile(i - 1) = sfile(i)
Select Case UCase(Right(GetDlgSelectfileInfo.sfile(i - 1),4))
Case ".BMP"
GetDlgSelectfileInfo.picType(i - 1) = 1
Case ".GIF"
GetDlgSelectfileInfo.picType(i - 1) = 2
Case Else
GetDlgSelectfileInfo.picType(i - 1) = 3
End Select
Next i
End If
End Function


'*************************************************************************
'** 作 者 : lavIEwpbt
'** 函 数 名 : SavePic
'** 输 入 : pic(StdPicture) - 图象句柄
'** : filename(String) - 保存路径
'** : Quality(Byte) - JPG图象质量
'** : TIFF_colorDepth(Long) - TTF格式的颜色深度
'** : TIFF_Compression(Long) - TTF格式的压缩比
'** 输 出 : 无
'** 功能描述 : 把图象保存为JPG、TIFF、PNG、GIF、BMP格式
'** 日 期 :
'** 修 改 人 : lavIEwpbt
'** 日 期 : 2005-10-23 14.43.52
'** 版 本 : Version 1.2.1
'*************************************************************************
Private Sub SavePic(ByVal pict As String,ByVal filename As String,picType As String,_
Optional ByVal Quality As Byte = 80,_
Optional ByVal TIFF_colorDepth As Long = 24,_
Optional ByVal TIFF_Compression As Long = 6)


Dim tSI As Gdiplusstartupinput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long
Dim aEncParams() As Byte

Screen.MousePointer = vbHourglass

' On Error GoTo ErrHandle:

tSI.Gdiplusversion = 1 ' 初始化 GDI+
lRes = Gdiplusstartup(lGDIP,tSI)

If lRes = 0 Then ' 从句柄创建 GDI+ 图像
' lRes = GdipCreateBitmapFromHBITMAP(pict.Handle,lBitmap)
lRes = GdipLoadImageFromfile(StrPtr(pict),lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters '初始化解码器的GUID标识

Select Case picType
Case "jpg"
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"),tJpgEncoder
tParams.count = 1 ' 设置解码器参数
With tParams.Parameter ' Quality
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"),.GUID ' 得到Quality参数的GUID标识
.NumberOfValues = 1
.type = 4
.Value = VarPtr(Quality)
End With

ReDim aEncParams(1 To Len(tParams))
Call copyMemory(aEncParams(1),tParams,Len(tParams))

Case "png"
CLSIDFromString StrPtr("{557CF406-1A04-11D3-9A73-0000F81EF32E}"),tJpgEncoder
ReDim aEncParams(1 To Len(tParams))

Case "gif"
CLSIDFromString StrPtr("{557CF402-1A04-11D3-9A73-0000F81EF32E}"),tJpgEncoder
ReDim aEncParams(1 To Len(tParams))
End Select

If Option4.Value Then '不缩放
lRes = GdipSaveImagetofile(lBitmap,StrPtr(filename),tJpgEncoder,aEncParams(1)) '保存图像
Else
Dim nW As Single,nH As Single,nBL As Single '原始宽、高、比例
Dim nCurrW As Integer,nCurrH As Integer '新的宽、高
Dim GDIcopyBitmap As Long,GDIGraphics As Long

Dim nTmpW As Integer,nTmpH As Integer,imgAttr As Long

If GdipGetimageDimension(lBitmap,nW,nH) = 0 Then
'''''''''''''''''''''''不执行'''''''
If Option1.Value Then
nBL = nW / CInt(Text2.Text)
nCurrW = CInt(Text2.Text)
nCurrH = CInt(nH / nBL)

Call GdipCreateBitmapFromScan0(nCurrW,nCurrH,PixelFormat24bppRGB,ByVal 0&,GDIcopyBitmap)
Call GdipGetimageGraphicsContext(GDIcopyBitmap,GDIGraphics)
Call GdipDrawImageRect(GDIGraphics,lBitmap,nCurrW,nCurrH)
End If

If Option2.Value Then
nBL = nH / CInt(Text3.Text)
nCurrW = CInt(nW / nBL)
nCurrH = CInt(Text3.Text)

Call GdipCreateBitmapFromScan0(nCurrW,nCurrH)
End If
''''''''''''''''''''不执行'''''''''

'自定义尺寸进行等比缩放
If Option3.Value Then
If (nW / CInt(Text2.Text)) > (nH / CInt(Text3.Text)) Then
nBL = nW / CInt(Text2.Text)
nCurrW = CInt(Text2.Text)
nCurrH = CInt(nH / nBL)
Else
nBL = nH / CInt(Text3.Text)
nCurrW = CInt(nW / nBL)
nCurrH = CInt(Text3.Text)
End If

nTmpW = CLng(Text2.Text)
nTmpH = CLng(Text3.Text)

Call GdipCreateBitmapFromScan0(nTmpW,nTmpH,GDIcopyBitmap)
' Call GdipCreateImageAttributes(imgAttr)
Call GdipGetimageGraphicsContext(GDIcopyBitmap,GDIGraphics)
GdipGraphicsClear GDIGraphics,&HFFFFFFFF

GdipDrawImageRect GDIGraphics,nTmpW,nTmpH '拉伸到100*200

'Call GdipdisposeImageAttributes(imgAttr)
End If

Call GdipSaveImagetofile(GDIcopyBitmap,aEncParams(1))

Call GdipdisposeImage(GDIcopyBitmap)
Call GdipDeleteGraphics(GDIGraphics)
End If
End If

GdipdisposeImage lBitmap ' 销毁GDI+图像
End If

GdiplusShutdown lGDIP '销毁 GDI+
End If

Screen.MousePointer = vbDefault
Erase aEncParams
Exit Sub

ErrHandle:
Screen.MousePointer = vbDefault
MsgBox "在保存图片的过程中发生错误:" & vbCrLf & vbCrLf & "错误号: " & Err.Number & vbCrLf & "错误描述: " & Err.Description,vbinformation Or vbOKOnly,"错误"
End Sub

Private Sub Combo1_Click()
If Combo1.ListIndex > 0 Then
Text1.Enabled = False
Else
Text1.Enabled = True
End If
End Sub



Private Sub Command1_Click()
If List1.ListCount < 1 Then
MsgBox "请选择要转换的图片文件!",0 + 48,"错误信息"
Command2.SetFocus
Exit Sub
End If

Command1.Enabled = False
Command2.Enabled = False
Command4.Enabled = False

Text1.Enabled = False
Combo1.Enabled = False

Dim i As Integer,cTmp As String,nQuality As Byte

nQuality = CInt(Text1.Text)

For i = 1 To List1.ListCount
cTmp = left(List1.List(i - 1),Len(List1.List(i - 1)) - 4)

If Dir(cTmp & "_pvIEw." & LCase(Combo1.Text)) <> "" Then
Kill cTmp & "_pvIEw." & LCase(Combo1.Text)
End If

DoEvents
' Call SavePic(LoadPicture(cPicPath & List1.List(i)),cPicPath & cTmp & "_pvIEw.jpg",".jpg",CcInt(Text1.Text))
Call SavePic(List1.List(i - 1),cTmp & "_pvIEw." & LCase(Combo1.Text),LCase(Combo1.Text),nQuality)

Label4.Caption = CInt(i / (List1.ListCount) * 100) & " %"
Label3.WIDth = CInt(i / (List1.ListCount) * 5700)
DoEvents
Next i

MsgBox "共转换了 " & List1.ListCount & " 个图片 !",0 + 64,"提示信息"

Label4.Caption = "0 %"
Label3.WIDth = 0

Text1.Enabled = True
Combo1.Enabled = True
Command1.Enabled = True
Command2.Enabled = True
Command4.Enabled = True
End Sub

Private Sub Command2_Click()
Dim Openfile As OPENfilename
Dim lReturn As Long,n As Integer
Static strFilter As String
Dim cTmp As String

strFilter = "All Pictures" & Chr(0) & "*.bmp;*.gif;*.jpg;*.jpeg;*.tif;*.png" & Chr(0) & _
"Bitmap (*.bmp)" & Chr(0) & "*.bmp" & Chr(0) & _
"GIF (*.gif)" & Chr(0) & "*.gif" & Chr(0) & _
"JPG (*.jpg;*.jpeg)" & Chr(0) & "*.jpg;*.jpeg" & Chr(0) & _
"TIF (*.tif)" & Chr(0) & "*.tif" & Chr(0) & _
"PNG (*.png)" & Chr(0) & "*.png" & Chr(0)

Openfile.lStructSize = Len(Openfile)
Openfile.hwndOwner = Me.hwnd
Openfile.hInstance = App.hInstance
Openfile.lpstrFilter = strFilter
Openfile.nFilterIndex = 1
Openfile.lpstrfile = String(8192,0)
Openfile.nMaxfile = 8192
Openfile.lpstrfileTitle = Space(254)
Openfile.nMaxfileTitle = 255
Openfile.lpstrInitialDir = cPicPath
Openfile.lpstrTitle = "选择文本文件"
Openfile.flags = OFN_LONGnameS Or OFN_NODEREFERENCElinkS Or OFN_EXPLORER Or OFN_HIDEReadonly Or _
OFN_ENABLESIZING Or OFN_ALLOWMulTISELECT Or OFN_PATHMUSTEXIST Or OFN_fileMUSTEXIST

lReturn = Getopenfilename(Openfile)

n = GetDlgSelectfileInfo(Openfile.lpstrfile).iCount
If n > 0 Then
' If cPicPath <> GetDlgSelectfileInfo(Openfile.lpstrfile).sPath Then
' List1.Clear
' End If

cPicPath = GetDlgSelectfileInfo(Openfile.lpstrfile).sPath

For lReturn = 1 To n
List1.AddItem cPicPath & GetDlgSelectfileInfo(Openfile.lpstrfile).sfile(lReturn)
Next
End If
End Sub

Private Sub Command4_Click()
If List1.ListCount = 1 Then Exit Sub
Dim i As Integer
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
List1.RemoveItem i
End If
Next i
End Sub

Private Sub Form_Initialize()
Combo1.ListIndex = 0
End Sub

Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer)
If KeyCode = 13 Then
SendKeys "{Tab}"
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Unload Form1
End Sub

Private Sub List1_Click()
If List1.ListCount < 1 Then Exit Sub

Dim w As Single,h As Single
Dim nB As Single,nB1 As Single,i As Integer
Dim gdip_Graphics As Long,gdip_Image As Long
Dim tSI As Gdiplusstartupinput,lRes As Long,lGDIP As Long
Dim nCurrX As Integer,nCurrY As Integer


tSI.Gdiplusversion = 1 ' 初始化 GDI+
lRes = Gdiplusstartup(lGDIP,tSI)
If lRes = 0 Then ' 从句柄创建 GDI+ 图像
lRes = GdipCreateFromHDC(Me.hdc,gdip_Graphics)
If lRes = 0 Then
lRes = GdipLoadImageFromfile(StrPtr(List1.Text),gdip_Image)
If lRes = 0 Then
Call GdipGetimageDimension(gdip_Image,w,h)
If Option1.Value Then
Me.Label6.Caption = "转前:" & w & " X " & h & " 像素 转后:" & Text2.Text & " X " & Int(h / w * CInt(Text2.Text)) & " 像素"
End If

If Option2.Value Then
Me.Label6.Caption = "转前:" & w & " X " & h & " 像素 转后:" & Int(w / h * CInt(Text3.Text)) & " X " & Text3.Text & " 像素"
End If

If Option3.Value Then
Me.Label6.Caption = "转前:" & w & " X " & h & " 像素 转后:" & Text2.Text & " X " & Text3.Text & " 像素"
End If

If Option4.Value Then
Me.Label6.Caption = "转前:" & w & " X " & h & " 像素 转后:" & w & " X " & h & " 像素"
End If

Me.Label6.left = (Me.WIDth / 15 - Me.Label6.WIDth) / 2
If w = 284 And h = 164 Then
nCurrX = List1.left + List1.WIDth - w
nCurrY = List1.top + List1.Height - h
Call GdipDrawImageRect(gdip_Graphics,gdip_Image,Int((284 - w) / 2) + 1,Int((164 - h) / 2) + 1,h)
Else
nB = 284 / w
nB1 = 164 / h

If nB > nB1 Then
Call GdipDrawImageRect(gdip_Graphics,Int((164 - h * nB) / 2) + 1,284,Int(h * nB))
Else
Call GdipDrawImageRect(gdip_Graphics,Int((284 - w * nB1) / 2) + 1,Int(w * nB1),164)
End If
End If
Call GdipdisposeImage(gdip_Image)
End If
Call GdipDeleteGraphics(gdip_Graphics)
End If
GdiplusShutdown lGDIP '销毁 GDI+
End If

Me.Refresh

End SubPrivate Sub Option1_Click() Call CheckOptionEnd SubPrivate Sub Option2_Click() Call CheckOptionEnd SubPrivate Sub Option3_Click() Call CheckOptionEnd SubPrivate Sub Option4_Click() Call CheckOptionEnd SubPrivate Sub Text1_ValIDate(Cancel As Boolean) Call CheckText(Text1,80,10,100)End SubPrivate Sub Text2_ValIDate(Cancel As Boolean) Call CheckText(Text2,1024,2560)End SubPrivate Sub Text3_ValIDate(Cancel As Boolean) Call CheckText(Text3,768,1600)End SubFunction CheckText(oTxt As TextBox,nDef As Integer,nMin As Integer,nMax As Integer) Dim cTmp As String cTmp = Trim(oTxt.Text) If cTmp = "" Then oTxt.Text = nDef Exit Function End If If Not IsNumeric(cTmp) Then oTxt.Text = nDef Exit Function End If If CInt(cTmp) < nMin Or CInt(cTmp) > nMax Then oTxt.Text = nDef Exit Function End IfEnd FunctionFunction CheckOption() If Option1.Value Then Text2.Enabled = True Text3.Enabled = False Exit Function End If If Option2.Value Then Text2.Enabled = False Text3.Enabled = True Exit Function End If If Option3.Value Then Text2.Enabled = True Text3.Enabled = True Exit Function End If If Option4.Value Then Text2.Enabled = False Text3.Enabled = False Exit Function End IfEnd Function

总结

以上是内存溢出为你收集整理的用VB进行GDI+绘图全部内容,希望文章能够帮你解决用VB进行GDI+绘图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1293186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存