◎ 顯示圖片至 PictureBox 控制項

1. 先在表單上加入一個 PictureBox 控制項。
2. 將要顯示的圖形檔建立成一個 Image物件。
3. 將此 Image 物件指派給 PictureBox 控制項的 Image 屬性。
     通常使用以下兩種方法:
    (1) Image.FromFile (str_FileName)
        從指定的圖形檔建立 Image 物件
    (2) Image.FromStream (FileStream)
        從指定的資料流建立 Image 物件

With PictureBox1
    ' 框線設定成單線框
    .BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    ' 框線設定成立體框
    .BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    ' 將大小調成自動符合其所包含影像的大小
    .SizeMode = PictureBoxSizeMode.AutoSize

    ' 方法一
    .Image = Image.FromFile(Application.StartupPath + "\myImg.gif")

    ' 方法二
    Dim mBinaryData As Byte()
    mBinaryData = My.Computer.FileSystem.ReadAllBytes(Application.StartupPath + "\myImg.jpg")
    Dim ms As New IO.MemoryStream(mBinaryData)
    .Image = Image.FromStream(ms)
    ms.Close()

    ' 將圖形清除
    .Image = Nothing
End With


SizeMode 屬性:
1. Normal:由控制項的左上角放起。
2. StretchImage:圖片隨控制項大小伸縮。
3. AutoImage:控制項隨凸面大小伸縮。
4. CenterImage:放在控制項的正中央。

PictureBox 控制項可顯示的圖形檔格式有:
1. BMP
2. GIF 
3. JPG
4. PNG
5. WMF
6. ICO

 

◎ 將 PictureBox 控制項所顯示之圖片存成圖檔

 

Dim ms As IO.MemoryStream = New IO.MemoryStream
Dim imgPicBox As New Bitmap(PictureBox1.Image)
imgPicBox.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytBLOBData(CInt(ms.Length - 1)) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, CInt(ms.Length))
ms.Close()

My.Computer.FileSystem.WriteAllBytes(Application.StartupPath+"\MyImage.jpg", bytBLOBData, False)

 

請參考:寫入二進位檔

 

◎ 自行繪製圖形於 PictureBox 控制項上

以寫入一文字為範例

 

Dim mBmpImg As Bitmap
Dim g As Graphics
Dim b As New SolidBrush(Color.Gray)

mBmpImg = New Bitmap(PictureBox1.Width, PictureBox1.Height)
g = Graphics.FromImage(mBmpImg)
b.Color = Color.White
g.FillRectangle(b, 5, 5, PictureBox1.Width-10, PictureBox1.Height-10)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.SystemDefault
b.Color = Color.Red
g.DrawString("My Image", New Font("Arial", 24), b, 10, 40)

PictureBox1.Image = mBmpImg

 

請參考:Bitmap 類別Graphics 物件Brush 物件

arrow
arrow
    全站熱搜

    tsuozoe 發表在 痞客邦 留言(1) 人氣()