我无法在图片框中获取图像 [英] I am not able to get image in the picturebox

查看:59
本文介绍了我无法在图片框中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个程序,从列表框中选择一个文档类型,然后在图片框中获取其图像,但是在运行此程序时,当我选择一个文档时,我将其图片显示在图片框中,但是当我在其中选择另一个文档时我的列表框得到它的图像.请帮忙.
;;;;;;;;;;;;;;;;;;;; ---这是我的代码----- ;;;;;;;;

I am doing a program where I select a document type from the listbox and I got its image in the picturebox, but while running this program when I select a document I got its image in the picturebox,but when Iselect another document in the listbox I dint get its image. Please help.
;;;;;;;;;;;;;;;;;;;--- here''s my code-----;;;;;;;;;;;;;;;;;

Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Public Class hbet_update
    Dim con As New SqlConnection("data source= (local); initial catalog= hbet; user id=sa; password=; multipleactiveresultsets=true")
    Dim sid As String
    Dim dt As DataTable
    Private Sub hbet_update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            If con.State <> ConnectionState.Open Then
                con.Open()
            End If
            Dim cmd As New SqlCommand("select distinct studentid + ' ' + studentname,studentid from hbet.dbo.studentmaster ", con)

            Dim dr As SqlDataReader

            dr = cmd.ExecuteReader

            ComboBox1.Text = "---Select---"

            While dr.Read

                ComboBox1.Items.Add(dr(0))
            End While
            dr.Close()

            Dim cmd1 As New SqlCommand(" select distinct schoolname from hbet.dbo.studentmaster", con)
            Dim dr1 As SqlDataReader
            dr1 = cmd1.ExecuteReader
            While dr1.Read
                cb_school.Items.Add(dr1(0))
            End While
            dr1.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        bindd()

        ' End If
    End Sub
    Private Sub bindd()
        GroupBox3.Visible = False

        'If Not IsNothing(ListBox1.SelectedItem) Then
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        ListBox1.Items.Clear()

        Dim cmd As New SqlCommand
        Dim id As String
        id = ComboBox1.Text
        id = id.Substring(0, 8)

        Dim cmd1 As New SqlCommand("select CONVERT(NVARCHAR(10),SNO) + ' ' + doctype  from hbet.dbo.hbetscan where studentid= '" & id & "'", con)
        cmd1.CommandTimeout = 0
        Dim dr As SqlDataReader
        dr = cmd1.ExecuteReader
        While dr.Read
            ListBox1.Items.Add(dr(0))
        End While
        dr.Close()
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        tb_name.Enabled = False
        cb_school.Enabled = False
        cb_doctype.Enabled = False
        Dim test As String = ListBox1.Text
        tb_sno.Text = ListBox1.Text.Remove(ListBox1.Text.IndexOf(" "))
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        GroupBox3.Visible = True
        Dim dr As SqlDataReader
        'Dim sid As String
        sid = ComboBox1.SelectedItem.ToString
        sid = sid.Substring(0, 9)
        ' school = ComboBox2.Text
        'dname = ListBox1.Items.Add(dr(0))
        Dim cmd As New SqlCommand("select * from studentmaster a, hbetscan b where a.studentid='" & sid & "' and a.studentid=b.studentid ", con)
        cmd.CommandTimeout = 0
        cmd.ExecuteNonQuery()
        dr = cmd.ExecuteReader
        If dr.Read Then
            tb_name.Text = dr("studentname")
            cb_school.Text = dr("schoolname")
            cb_doctype.Text = dr("doctype")

            Dim imgdata() As Byte = dr("image")
            Dim img As Image
            Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
            ms.Write(imgdata, 0, imgdata.Length)
            img = Image.FromStream(ms, True)
            PictureBox1.Image = img
            PictureBox1.Image.Save("c:\" & "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            'dr.Close()
            con.Close()
        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        ViewDoc.PictureBox1.Image = PictureBox1.Image
        ViewDoc.ShowDialog()
    End Sub



为代码添加了PRE块[/编辑]



Added PRE blocks for code[/Edit]

推荐答案

好吧,如果它一旦工作就应该再次工作.但是我在您的
上注意到了
ListBox1_SelectedIndexChanged您保存了图像但没有清除?


另外我注意到您没有关闭ListBox1_SelectedIndexChanged中的内存流.只是您可能想考虑的事情

因此,请尝试此代码

我不确定这里的* EXACT *代码,但我想您会明白的.

Well if it worked once it should work again. However I noticed on your

ListBox1_SelectedIndexChanged you saved the image but did not clear it?


Also I noticed a you did not close the memory stream in ListBox1_SelectedIndexChanged. Just something you might want to think about

So try this code

I''m not sure of the *EXACT* code here but I think you will figure it out.

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
 
        tb_name.Enabled = False
        cb_school.Enabled = False
        cb_doctype.Enabled = False
        Dim test As String = ListBox1.Text
        tb_sno.Text = ListBox1.Text.Remove(ListBox1.Text.IndexOf(" "))
        If con.State <> ConnectionState.Open Then
            con.Open()
        End If
        GroupBox3.Visible = True
        Dim dr As SqlDataReader
        'Dim sid As String
        sid = ComboBox1.SelectedItem.ToString
        sid = sid.Substring(0, 9)
        ' school = ComboBox2.Text
        'dname = ListBox1.Items.Add(dr(0))
        Dim cmd As New SqlCommand("select * from studentmaster a, hbetscan b where a.studentid='" & sid & "' and a.studentid=b.studentid ", con)
        cmd.CommandTimeout = 0
        cmd.ExecuteNonQuery()
        dr = cmd.ExecuteReader
        If dr.Read Then
            tb_name.Text = dr("studentname")
            cb_school.Text = dr("schoolname")
            cb_doctype.Text = dr("doctype")
 
            Dim imgdata() As Byte = dr("image")
            Dim img As Image
            Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
            ms.Write(imgdata, 0, imgdata.Length)
            img = Image.FromStream(ms, True)
            PictureBox1.Image = img
            PictureBox1.Image.Save("c:\" & "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            'dr.Close()
            con.Close()
'Close the memory stream
ms.Dispose()
        End If


这篇关于我无法在图片框中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆