 |
|
สวัสดีครับ ผมมีปัญหา เรื่อง การบันทึกรูปภาพที่เป็น binary เเล้วก็ดึงมาใช้ ครับ ขอตัวอย่างโค๊ดได้มั้ยครับ |
|
 |
|
|
 |
 |
|
สวัสดีครับ ผมมีปัญหา เรื่อง การบันทึกรูปภาพที่เป็น binary เเล้วก็ดึงมาใช้ ครับ ขอตัวอย่างโค๊ดได้มั้ยครับ
Code
Unable to cast object of type 'System.String' to type 'System.Byte[]'. Exception

Code (VB.NET)
Dim ofDlg As New OpenFileDialog()
'Reading a picture from disk and put it in a bytearray
Using fo As New OpenFileDialog With
{.Filter = "JpG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"}
If fo.ShowDialog = DialogResult.OK Then
tmpFileName = fo.FileName
Me.Label35.Text = tmpFileName
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim byteArray = br.ReadBytes(CInt(fs.Length))
br.Close()
Dim ms As New IO.MemoryStream(byteArray)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Using
Code (VB.NET)
Private Sub ShowImage()
Dim sqlImage As String
sqlImage = "SELECT PICTURE FROM land"
sqlImage &= " WHERE LANDID ='1'"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
da = New SqlDataAdapter(sqlImage, Conn)
da.Fill(ds, "land")
da.SelectCommand.CommandText = sqlImage
da.Fill(ds, "land")
If ds.Tables("land").Rows.Count <> 0 Then
IsFindImage = True
CurrentImage = CType(ds.Tables("land").Rows(0).Item("PICTURE"), Byte())
Dim ms As New MemoryStream(CurrentImage, True)
PictureBox1.Image = Image.FromStream(ms)
Else
IsFindImage = False
End If
End Sub
Tag : .NET, Ms SQL Server 2008, Win (Windows App), VB.NET, VS 2012 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2012-07-11 12:01:44 |
By :
pisansri |
View :
2189 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
ds.Table("land").Rows(0)("Picture")
แค่นี้น่าจะพอน่ะครับ
|
 |
 |
 |
 |
Date :
2012-07-11 21:13:18 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังเหมือนเดิมเลยครับ
|
 |
 |
 |
 |
Date :
2012-07-11 23:43:28 |
By :
pisansri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ว่าแต่ข้อมูลมันเป็น Bytes หรือ Binary หรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2012-07-12 06:08:27 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เเบบนี้เค้าเรียกว่าอะไรครับ D:\sspetax\pictureland\15E003.jpg ไม่รู้จริงๆ
|
 |
 |
 |
 |
Date :
2012-07-12 11:37:13 |
By :
pisansri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เเบบนี้เค้าเรียกว่าอะไรครับ D:\sspetax\pictureland\15E003.jpg ไม่รู้จริงๆ = Path ครับผม
คงไม่จบถ้าไม่สานต่อ 555+++
Code (VB.NET)
Private tmpFileName As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Reading a picture from disk and put it in a bytearray
Using fo As New OpenFileDialog With
{.Filter = "JpG (*.jpg)|*.jpg|Gif (*.gif)|*.gif|All files (*.*)|*.*"}
If fo.ShowDialog = DialogResult.OK Then
tmpFileName = fo.FileName
Call LoadImage(tmpFileName)
End If
End Using
End Sub
Private Sub LoadImage(ByRef picturePath As String)
Me.Label2.Text = tmpFileName
Dim fs As New IO.FileStream(picturePath, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim byteArray = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(byteArray)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
Private Sub ShowImage()
Dim sqlImage As String
sqlImage = "SELECT PICTURE FROM land"
sqlImage &= " WHERE LANDID ='1'"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
da = New SqlDataAdapter(sqlImage, Conn)
da.Fill(ds, "land")
da.SelectCommand.CommandText = sqlImage
da.Fill(ds, "land")
If ds.Tables("land").Rows.Count <> 0 Then
IsFindImage = True
Call LoadImage(ds.Tables("land").Rows(0).Item("PICTURE"))
Else
IsFindImage = False
End If
End Sub
|
 |
 |
 |
 |
Date :
2012-07-12 12:35:32 |
By :
ch_b |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้เเล้วครับปัญหาอยู่ที่การบันทึก เเก้ได้ซักที่ ครับ ขอบคุณมากครับ
Code (VB.NET)
Dim strSQL As String
Dim cmd As SqlCommand
Dim lngRows As Integer
Dim photo As Byte()
Dim strConnstring As String = "Data Source=.\SQLSERVER;Initial Catalog=GIS;Integrated Security=True;User ID=sa;Pwd=sspe"
Dim cn As New SqlConnection(strConnstring)
photo = GetPhoto(tmpFileName) ' ---- อ่านไฟล์ภาพเข้าตัวแปร Byte array
' TextBox1.Text = tmpFileName
' ---- เทคนิคการนำข้อมูล binary (byte array) เข้าฐานข้อมูล ให้ใช้พารามิเตอร์นะครับ
TextBox1.Text = tmpFileName
strSQL = "INSERT INTO land (PICTURE,PICTURENAME) "
strSQL &= " VALUES ('" & tmpFileName & "',"
strSQL &= "@photo)"
cmd = New SqlCommand(strSQL, cn) ' ---- ตัวแปร cn นี่ผมลากใส่ฟอร์มกำหนด Connection String จาก wizard นะ
' ---- ดูวิธีการส่งค่าให้กับพารามิเตอร์ดีๆ Path.GetFileName เป็นวิธีการเอามาแต่ชื่อไฟล์ จากพาธยาวๆ
cmd.Parameters.Add("@filename", SqlDbType.NVarChar, 500).Value = Path.GetFileName(tmpFileName)
cmd.Parameters.Add("@photo", SqlDbType.Image, photo.Length).Value = photo
If (cn.State = ConnectionState.Closed) Then cn.Open()
lngRows = cmd.ExecuteNonQuery() ' ---- ยิง SQL ไป execute.
MsgBox(lngRows & "บันทึกเรียบร้อย")
cn.Close()
PictureBox1.Image = Nothing ' ---- ลบภาพพรีวิวออก.
End Sub
|
 |
 |
 |
 |
Date :
2012-07-12 16:11:33 |
By :
pisansri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืมตามนั้นครับ มันเป็นแค่ Path จะต้องแปลงให้เป็น Byte , Binary ก่อนครับ
|
 |
 |
 |
 |
Date :
2012-07-12 16:14:31 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามอีกนิดนึงครับ เหลือ update ครับรูปยังไม่ขึ้นเลยครับ
Code (VB.NET)
Dim cmd As SqlCommand
Dim lngRows As Integer
Dim photo As Byte()
Dim strConnstring As String = "Data Source=.\SQLSERVER;Initial Catalog=GIS;Integrated Security=True;User ID=sa;Pwd=sspe"
Dim cn As New SqlConnection(strConnstring)
photo = GetPhoto(tmpFileName) ' ---- อ่านไฟล์ภาพเข้าตัวแปร Byte array
Dim sqlupdate As String
sqlupdate = "update land "
sqlupdate &= " set PICTURENAME= '" & "@photo" & "',"
sqlupdate &= " PICTURE= '" & tmpFileName & "'"
sqlupdate &= " where LANDID = '" & txtLANDID.Text & "'"
cmd = New SqlCommand(sqlupdate, cn) ' ---- ตัวแปร cn นี่ผมลากใส่ฟอร์มกำหนด Connection String จาก wizard นะ
' ---- ดูวิธีการส่งค่าให้กับพารามิเตอร์ดีๆ Path.GetFileName เป็นวิธีการเอามาแต่ชื่อไฟล์ จากพาธยาวๆ
' cmd.Parameters.Add("@filename", SqlDbType.NVarChar, 500).Value = Path.GetFileName(tmpFileName)
cmd.Parameters.Add("@photo", SqlDbType.Image, photo.Length).Value = photo
If (cn.State = ConnectionState.Closed) Then cn.Open()
lngRows = cmd.ExecuteNonQuery() ' ---- ยิง SQL ไป execute.
MsgBox(lngRows & "บันทึกเรียบร้อย")
cn.Close()
|
 |
 |
 |
 |
Date :
2012-07-12 18:26:10 |
By :
pisansri |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|