我才知道 VB6 下的 PictureBox 有兩種不同的屬性可以取得 PictureBox 上面的影像!
分別是 Pictrue 和 Image!
在這裡練一下,幫助自己增加記憶點!
接著簡單說明一下這次練習的內容物跟使用方法大概是什麼樣的狀況。
首先居裡貓建立了一個 form 裡面包含了三個 PictureBox ,
這三個 PictureBox 的目的主要是:
1. 原始的 PictureBox:負責裝載所有的影像。
2. Picture 屬性的 PictureBox:負責裝載來自原始 PictureBox 上的 Picture 屬性資訊。
3. Image 屬性的 PictureBox:負責裝載來自原始 PictureBox 上的 Image 屬性資訊。
再來是兩個主要產生影像的方法:
1. Load Picture:使用 LoadPicture 方法載入一張影像。
2. Draw Picture:使用 PictureBox.Line 方法話出影像。
另外還有:
1. Save Picture:儲存原始 PictureBox 的 Picture 屬性資訊。
2. Save Image:儲存原始 PictureBox 的 Image 屬性資訊。
3. Get PictureBox.Pictrue:取得原始 PictureBox 的 Picture 屬性資訊,並繪製在新的 PictureBox 上。
4. Get PictureBox.Image:取得原始 PictureBox 的 Image 屬性資訊,並繪製在新的 PictureBox 上。
接下來就讓我們看一下實際的畫面吧!!
上圖就是本次練習的畫面。
現在的狀態是有特別的操作手法而得到的狀態。
先是按下 Draw Picture 後,再按下 Get PictrueBox.Image。
接著按下 Load Pictrue 後,再按下 Draw Picture 後,再按下 Get PictureBox.Pictrue。
經過這樣特別的手順才會得到這樣的狀態。
看到這裡的人會覺得,你在幹麻,為什麼要稿這麼麻煩的操作。
那先等一下阿!!!!請看一下下面另一張圖。
看到了嘛?
上面這一張圖 Get PictureBox.Image 取得了原始 PictureBox 中的所有影像資訊。
這就是本次居裡貓一個比較特別的發現,這兩個屬性在代表上有這樣的差異。
所以在存下來的影像當然也會是不一樣的影像。
接下來就看看存下來的影像分別是什麼吧!
上面這張就是 Picture 屬性存下來的圖片,基本上就是你給他什麼影像,
你就可以存下什麼影像。
上面兩張圖分別是第一次狀態、第二次狀態存下來的 Image 屬性。
這樣就可以大致上了解到這兩個屬性的差異性了。
接著讓我們看一下 DeBug 的畫面吧!
藉由 DeBug 讓我們看看這兩個屬性在得到影像的前後差別分別是什麼,
這樣應該會更有感覺喔!
上面這張是準備要使用 Draw Pictrue 按鈕來產生影像。
程式碼部份停留在 DrawRectangle 還沒執行繪圖的動作,
Image 屬性已經有自己相關的資訊了。Picture 反之。
上面這張圖已經執行完 Draw Pictrue 按鈕產生影像的動作。
在 Image 屬性上並看不出有什麼變化,這裡居裡貓就不專業的解說,
這個 Image 屬性大概就是一開始就存在的記憶體位置,所以我們只是塞了值給他,
所以這樣的方法是看不出有什麼不一樣的。
至於 Picture 依然沒有變化。
上圖是準備要執行 Load Picture 按鈕產生影像。
程式碼部份停留在 LoadImage 還沒執行繪製影像的動作。
Image 屬性依然是同樣的狀態,Picture 亦同。
上圖已經執行完 LoadImage 按鈕產生影像的動作。
在這裡觀察到了什麼嘛?
Image 的 Handle 改變了,而 Picture 裡面資訊也全部多更新了。
看到這裡有沒有更有感覺了呢?
總結一下整個過程,
Image 利用 PictureBox.Line 方法產生,並不會有實質上的從屬性資訊看到變化。
Picture 利用 LoadPicture 方法產生,會看到實質上的屬性資訊變化。
Picture 屬性的影像不會跟 Image 屬性的影像有直接的影響。
Image 屬性的影像會跟 Picture 屬性的影像有直接的影響。
這次居裡貓的練習就到這裡結束。
未來有要使用到 VB6 中 PictureBox Control 的朋友,希望這篇文章可以幫到你。
希望這次的練習分享你們會喜歡。
底下是相關的程式碼:
----------------------------------------------------------------------------------------------------------------------------
Form 的部份:
- VERSION 5.00
- Begin VB.Form frmPictureBoxControlPractice
- Caption = "PicturebBox Control Practice"
- ClientHeight = 10065
- ClientLeft = 60
- ClientTop = 450
- ClientWidth = 17445
- LinkTopic = "Form1"
- ScaleHeight = 10065
- ScaleWidth = 17445
- StartUpPosition = 3 'Windows Default
- Begin VB.CommandButton cndSaveImage
- Caption = "Save Image"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 13.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 3480
- TabIndex = 11
- Top = 7440
- Width = 2175
- End
- Begin VB.CommandButton cmdSavePicture
- Caption = "Save Picture"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 13.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 240
- TabIndex = 10
- Top = 7440
- Width = 2175
- End
- Begin VB.CommandButton cmdDrawPicture
- Caption = "Draw Picture"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 13.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 3480
- TabIndex = 9
- Top = 6240
- Width = 2175
- End
- Begin VB.CommandButton cmdLoadPicture
- Caption = "Load Picture"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 13.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 240
- TabIndex = 8
- Top = 6240
- Width = 2175
- End
- Begin VB.CommandButton cmdGetPictureBoxdotImage
- Caption = "Get PictureBox.Image"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 18
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 12480
- TabIndex = 7
- Top = 6240
- Width = 4455
- End
- Begin VB.CommandButton cmdGetPictureBoxdotPicture
- Caption = "Get PictureBox.Picture"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 18
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 975
- Left = 6480
- TabIndex = 6
- Top = 6240
- Width = 4455
- End
- Begin VB.PictureBox pbImage
- Height = 4935
- Left = 11760
- ScaleHeight = 4875
- ScaleWidth = 5355
- TabIndex = 2
- Top = 1080
- Width = 5415
- End
- Begin VB.PictureBox pbPicture
- Height = 4935
- Left = 6000
- ScaleHeight = 4875
- ScaleWidth = 5355
- TabIndex = 1
- Top = 1080
- Width = 5415
- End
- Begin VB.PictureBox pbOrignal
- Height = 4935
- Left = 240
- ScaleHeight = 4875
- ScaleWidth = 5355
- TabIndex = 0
- Top = 1080
- Width = 5415
- End
- Begin VB.Label lblPictureBoxdotImage
- Caption = "PictureBox.Image"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 19.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 495
- Left = 11760
- TabIndex = 5
- Top = 600
- Width = 5415
- End
- Begin VB.Label lblPictureBoxdotPicture
- Caption = "PictureBox.Picture"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 19.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 495
- Left = 6000
- TabIndex = 4
- Top = 600
- Width = 5415
- End
- Begin VB.Label lblOrginalPictureBox
- Caption = "Orignal PictureBox"
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 19.5
- Charset = 0
- Weight = 400
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- Height = 495
- Left = 240
- TabIndex = 3
- Top = 600
- Width = 5415
- End
- End
- Attribute VB_Name = "frmPictureBoxControlPractice"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- '' ============================================================
- '' This is pictrue box control form.
- '' Have some simply function for picturebox control practice.
- ''
- '' Writer is J.Y.L by 2020/07/24
- '' ============================================================
- Option Explicit
- Dim ret As Boolean
- Private Sub cmdDrawPicture_Click()
- ret = DrawRectangle(pbOrignal, 2500, 2500, 4500, 4500, vbGreen)
- If ret = False Then MsgBox ("The DrawPicture is fail")
- End Sub
- Private Sub cmdGetPictureBoxdotImage_Click()
- ret = GetImageIntoPictureBox(pbImage, pbOrignal.Image)
- If ret = False Then MsgBox ("The GetImageIntoPictureBox is fail")
- End Sub
- Private Sub cmdGetPictureBoxdotPicture_Click()
- ret = GetPictureIntoPictureBox(pbPicture, pbOrignal.Picture)
- If ret = False Then MsgBox ("The GetPictureIntoPictureBox is fail")
- End Sub
- Private Sub cmdLoadPicture_Click()
- ret = LoadImage(pbOrignal, App.Path & "\" & "TestPicture.jpg")
- If ret = False Then MsgBox ("The LoadPicture is fail")
- End Sub
- Private Sub cmdSavePicture_Click()
- ret = SaveImage(pbOrignal.Picture, App.Path, "IsPicture.jpg")
- If ret = False Then MsgBox ("The SaveImage is fail")
- End Sub
- Private Sub cndSaveImage_Click()
- ret = SaveImage(pbOrignal.Image, App.Path, "IsImage.jpg")
- If ret = False Then MsgBox ("The SaveImage is fail")
- End Sub
- Private Sub Form_Load()
- '' down below will that draw image auto rewrok, is very important
- pbOrignal.AutoRedraw = True
- pbPicture.AutoRedraw = True
- pbImage.AutoRedraw = True
- End Sub
PictureBoxControl moudle 的部份:
- Attribute VB_Name = "mPictureBoxControl"
- '' ============================================================
- '' This is pictrue box control module.
- '' Have some simply function for picturebox control practice.
- ''
- '' Writer is J.Y.L by 2020/07/24
- '' ============================================================
- Option Explicit
- Dim fs As New FileSystemObject
- '' =================== Functions Area ===================
- '' pbObj is picturebox object that do draw line B
- '' leftupX is rectangle left X point
- '' leftupY is rectangle left Y point
- '' rightdownX is rectangle right X point
- '' rightdownY is rectangle right Y point
- '' color is pen color
- '' Return True is work success; False is work fail
- Public Function DrawRectangle(ByVal pbObj As PictureBox, ByVal leftupX As Double, ByVal leftupY As Double, ByVal rightdownX As Double, ByVal rightdownY As Double, _
- ByVal color As Long)
- On Error GoTo Err
- pbObj.Line (leftupX, leftupY)-(rightdownX, rightdownY), color, B
- DrawRectangle = True
- Exit Function
- Err:
- DrawRectangle = False
- MsgBox ("DrawRectangle is fail" & vbCrLf & Err.Description)
- End Function
- '' pbObj is picturebox object that do draw line B
- '' filepath is will load image path
- '' Return True is work success; False is work fail
- Public Function LoadImage(ByVal pbObj As PictureBox, ByVal filepath As String)
- On Error GoTo Err
- If (TypeName(fs) <> "FileSystemObject") Then Set fs = CreateObject("Scripting.FileSystemObject")
- If fs.FileExists(filepath) = False Then LoadImage = False: MsgBox ("The file is not exists with : " & filepath): Exit Function
- pbObj.Picture = LoadPicture(filepath)
- LoadImage = True
- Exit Function
- Err:
- LoadImage = False
- MsgBox ("LoadImage is fail" & vbCrLf & Err.Description)
- End Function
- '' img is picturebox display image
- '' folderPath is will save file path, here will check folder exists and create it
- '' filename is image file name, need have extensions
- '' Return True is work success; False is work fail
- Public Function SaveImage(Optional ByVal img As IPictureDisp, Optional ByVal folderPath As String, Optional ByVal filename As String)
- On Error GoTo Err
- If (TypeName(fs) <> "FileSystemObject") Then Set fs = CreateObject("Scripting.FileSystemObject")
- If folderPath = "" Then SaveImage = False: MsgBox ("The folder path is empty"): Exit Function
- If Right$(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
- If fs.FolderExists(folderPath) = False Then fs.CreateFolder (folderPath)
- If img = 0 Or img.Type = 0 Then SaveImage = False: MsgBox ("The image is empty"): Exit Function
- If filename = "" Then SaveImage = False: MsgBox ("The file name is empty"): Exit Function
- Call SavePicture(img, folderPath & filename)
- SaveImage = True
- Exit Function
- Err:
- SaveImage = False
- MsgBox ("SaveImage is fail" & vbCrLf & Err.Description)
- End Function
- '' pbObj is picturebox will show picturebox.picture
- '' img is picturebox display image
- '' Return True is work success; False is work fail
- Public Function GetPictureIntoPictureBox(ByVal pbObj As PictureBox, ByVal img As IPictureDisp)
- On Error GoTo Err
- pbObj.Picture = img
- GetPictureIntoPictureBox = True
- Exit Function
- Err:
- GetPictureIntoPictureBox = False
- MsgBox ("GetPictureIntoPictureBox is fail" & vbCrLf & Err.Description)
- End Function
- '' pbObj is picturebox will show picturebox.image
- '' img is picturebox display image
- '' Return True is work success; False is work fail
- Public Function GetImageIntoPictureBox(ByVal pbObj As PictureBox, ByVal img As IPictureDisp)
- On Error GoTo Err
- '' pbObj.Image = img '' <= object doesn't support property or method
- pbObj.Picture = img
- GetImageIntoPictureBox = True
- Exit Function
- Err:
- GetImageIntoPictureBox = False
- MsgBox ("GetImageIntoPictureBox is fail" & vbCrLf & Err.Description)
- End Function
- '' =================== Functions Area ===================
沒有留言:
張貼留言