none
picbox無法顯示jpg以外圖檔(bmp.gif) RRS feed

  • 問題

  • Public Class Form1
        Dim b As Bitmap
        Dim g As Graphics
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim picfile As IO.StreamReader
            Dim menulist As String
            Dim tool(10) As ToolStripMenuItem '宣告控制項變數'
            Me.PictureBox1.ContextMenuStrip = ContextMenuStrip1
            picfile = IO.File.OpenText(".\list.txt")
            ContextMenuStrip1.ShowCheckMargin = True '讓選單有check區域'
            ContextMenuStrip1.ShowImageMargin = False '讓選單圖片區域不見'
            For i As Integer = 1 To 10
                menulist = Trim(picfile.ReadLine)
                ContextMenuStrip1.Items.Add(menulist)
                '讓宣告過的tool(i)控制項陣列取代剛成立的menuitem'
                tool(i) = ContextMenuStrip1.Items(i - 1)
                tool(i).CheckOnClick = True '顯示Check選項'
            Next i
           

            FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop '設定起始開啟目錄
            TextBox1.Text = "尚未選取資料夾" 'TextBox1用來顯示開啟資料夾
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Pic, PicDir As String
            Dim fl As String

            ListBox1.Items.Clear()          '清除ListBox
            FolderBrowserDialog1.ShowDialog() '利用FolderBrowserDialog可以選取開啟資料夾
            PicDir = FolderBrowserDialog1.SelectedPath '開啟的資料夾
            TextBox1.Text = FolderBrowserDialog1.SelectedPath 'TextBox1用來顯示開啟資料夾

            For Each fl In My.Computer.FileSystem.GetFiles(PicDir, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg", "*.gif", "*.bmp", "*.png")
                Pic = My.Computer.FileSystem.GetName(fl)         '取得相關圖檔檔案名稱
                ListBox1.Items.Add(Pic) '列入檔名
            Next
        End Sub

        Private Sub ListBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

            If ListBox1.SelectedItem <> Nothing Then '確保已經有檔案名稱在你的ListBox
                PictureBox1.Image = Image.FromFile(TextBox1.Text & "\" & ListBox1.SelectedItem)
                PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
                b = PictureBox1.Image
                g = Graphics.FromImage(b)

            End If

        End Sub
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Dim zoomx, zoomy As Single
            zoomx = PictureBox1.Image.Width / PictureBox1.Width
            zoomy = PictureBox1.Image.Height / PictureBox1.Height

            If ListBox1.SelectedItem <> Nothing Then '確保已經有檔案名稱在你的ListBox
                If e.Button = Windows.Forms.MouseButtons.Right Then
                    g.FillEllipse(Brushes.Green, New RectangleF(e.X * zoomx - 2, e.Y * zoomy - 2, 4 * zoomx, 4 * zoomy))
                    PictureBox1.Image = b
                End If
            End If
        End Sub
    End Class

    -----------------------

    vb2005 express

     

    當嘗試開啟gif圖檔出現一項例外狀況 :    無法從具備索引像素格式的影像中建立圖形物件 (Graphic Object)。

    指向g = Graphics.FromImage(b)

    2007年8月20日 下午 01:30

所有回覆