[WM]利用Microsoft.Drawing擴充Graphics & 繪製縮圖

這篇延續上一篇提到的,繼續利用擴充的Graphics類別所提供的新功能來做應用;在使用的時候也是要加入Microsoft.Controls的參考,如果不清楚如何加入的話請參考上一篇
[WM]利用Microsoft.Drawing擴充Graphics & 製作我的按鈕
首先,今天會使用到的是IImagingFactory以及IImage這兩個東東,首先要從檔案把Image抓出來,所以會利用像下面的程式碼來傳回IImage
''' 
''' 取得影像相關動作
'''

'''
'''
'''
Private Function GetImage(ByVal fileName As String) As IImage
Dim imgFactory As IImagingFactory
''建立IImageingFactory
imgFactory = ImagingFactory.GetImaging
''建立IImage
Dim image As IImage = Nothing
imgFactory.CreateImageFromFile(fileName, image)
Return image
End Function

取回之後就可以呼叫IImage的GetThumbnail來取得縮圖,例如
img.GetThumbnail(48, 48, imgThumb)

好,大製了解動作之後就來看看這次測試的程式碼,筆者這邊是建立兩個form,form1會將縮圖顯示在畫面上,而點選縮圖之後會呼叫Form2來顯示比較大的圖形,Form1程式碼會像下面這樣
Imports Microsoft.Drawing
Imports System.IO

Public Class Form1
''儲存圖檔的位置
Dim strImageDir As String = "\Storage card\"
''顯示縮圖用的圖片框
Dim picBoxs() As PictureBox

Private Sub MyButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton1.Click
Dim img As IImage
Dim imgThumb As IImage = Nothing

Dim DirInfo As New DirectoryInfo(strImageDir)
Dim files() As FileInfo
''取得jpg檔案的集合
files = DirInfo.GetFiles("*.jpg")
''重新定義picturebox陣列
ReDim picBoxs(files.Length - 1)
''
Panel1.Controls.Clear()
For i As Integer = 0 To files.Length - 1
picBoxs(i) = New PictureBox
''設定picturebox相關的屬性以及位置
With picBoxs(i)
.Name = files(i).Name
.Size = New Size(48, 48)
.Left = 48 * (i Mod 4) + 20
.Top = 48 * (i \ 4) + 20
.Visible = True
End With
''將控制項新增到panel中
Panel1.Controls.Add(picBoxs(i))
''產生縮圖
img = GetImage(files(i).FullName)
img.GetThumbnail(48, 48, imgThumb)
''將圖片繪製到picturebox上
Using g As Graphics = picBoxs(i).CreateGraphics
g.DrawImageAlphaChannel(imgThumb, 0, 0)
End Using
''將縮圖儲存在tag屬性中給paint事件使用
picBoxs(i).Tag = imgThumb
''掛載相關事件
AddHandler picBoxs(i).Paint, AddressOf PictureBox_Paint
AddHandler picBoxs(i).Click, AddressOf PictureBox_Click
Application.DoEvents()
Next
End Sub

'''
''' 呼叫第二個form來顯示大圖
'''

'''
'''
'''
Private Sub PictureBox_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim f As New Form2
f.FilePath = strImageDir & CType(sender, PictureBox).Name.ToString
f.ShowDialog()
f.Dispose()
End Sub

'''
''' PictureBox的重繪動作
'''

'''
'''
'''
Private Sub PictureBox_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
If CType(sender, PictureBox).Tag IsNot Nothing Then
e.Graphics.DrawImageAlphaChannel(CType(sender, PictureBox).Tag, 0, 0)
End If
End Sub

'''
''' 取得影像相關動作
'''

'''
'''
'''
Private Function GetImage(ByVal fileName As String) As IImage
Dim imgFactory As IImagingFactory
''建立IImageingFactory
imgFactory = ImagingFactory.GetImaging
''建立IImage
Dim image As IImage = Nothing
imgFactory.CreateImageFromFile(fileName, image)
Return image
End Function
End Class

Form2的部分筆者增加了一個屬性,是設定要顯示的圖檔名稱,Form2的程式碼會像是下面這樣
Imports Microsoft.Drawing

Public Class Form2
''暫存檔案位置
Private _filePath As String

Dim img As IImage
Dim imgThumb As IImage = Nothing

'''
''' 取得或設定檔案位置
'''

'''
'''
'''
Public Property FilePath() As String
Get
Return _filePath
End Get
Set(ByVal value As String)
_filePath = value
End Set
End Property

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
''產生縮圖
img = GetImage(_filePath)
img.GetThumbnail(PictureBox1.Width, PictureBox1.Height, imgThumb)
''將縮圖儲存在tag屬性中給paint事件使用
PictureBox1.Tag = imgThumb
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
''將圖片繪製到picturebox上
e.Graphics.DrawImageAlphaChannel(imgThumb, 0, 0)
End Sub

'''
''' 取得影像相關動作
'''

'''
'''
'''
Private Function GetImage(ByVal fileName As String) As IImage
Dim imgFactory As IImagingFactory
imgFactory = ImagingFactory.GetImaging
Dim image As IImage = Nothing
imgFactory.CreateImageFromFile(fileName, image)
Return image
End Function
End Class
執行的結果會像是下面這樣

點擊縮圖之後會跳到Form2,畫面會像下面這樣

留言

這個網誌中的熱門文章

開啟cshtml檔案時,出現『並未將物件參考設定為物件執行個體』的錯誤訊息

無法設定中斷點 尚未載入符號檔

DataGridView欄位計算總合