Advertisement
deSantoz

Menyimpan dan mengupdate gambar di SQL Server

Dec 26th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.35 KB | None | 0 0
  1. Public Function ConvertImagetoByte(MyImage As Image) As Byte()
  2.         Dim MyByte As Byte() = Nothing
  3.         Dim MyMemoryStream As MemoryStream = New MemoryStream()
  4.         MyImage.Save(MyMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  5.         MyByte = MyMemoryStream.GetBuffer()
  6.         Return MyByte
  7.     End Function
  8. -------------------------------------------
  9.     Public Function ConvertBytetoImage(MyByte As Byte()) As Image
  10.         Dim MyImage As Image
  11.         Dim MyMemoryStream As MemoryStream = New MemoryStream(MyByte)
  12.         MyImage = Drawing.Image.FromStream(MyMemoryStream)
  13.         Return MyImage
  14.     End Function
  15.  
  16. --------------------------------------------
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. Private Sub LinkLabel3_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
  26.         Dim pilihgambar As New OpenFileDialog
  27.         pilihgambar.Filter = "All Images|*.BMP;*.DIB;*.RLE;*.JPG;*.JPEG;*.JPE;*.JFIF;*.GIF;*.TIF;*.TIFF;*.PNG"
  28.         pilihgambar.ShowDialog()
  29.         Dim photo As Image = Nothing
  30.         If pilihgambar.FileName <> "" Then
  31.             photo = Image.FromFile(pilihgambar.FileName)
  32.             PictureBox2.Image = photo
  33.             Try
  34.                 conn.Open()
  35.                 Dim MyByte As Byte() = ConvertImagetoByte(PictureBox2.Image)
  36.                 Dim UpdateImage As String = "UPDATE tb_useraccount SET user_image = @user_image WHERE user_account = @user_account AND user_password = @user_password"
  37.                 Dim MyCommand As New SqlCommand
  38.                 With MyCommand
  39.                     .Connection = conn
  40.                     .CommandText = UpdateImage
  41.                     .Parameters.Add(New SqlParameter("@user_image", SqlDbType.VarBinary)).Value = MyByte
  42.                     .Parameters.AddWithValue("@user_account", UserManager.UserAccountName)
  43.                     .Parameters.AddWithValue("@user_password", UserManager.UserPasssword)
  44.                 End With
  45.                 MyCommand.ExecuteNonQuery()
  46.             Catch ex As Exception
  47.                 MsgBox(ex.ToString)
  48.             Finally
  49.                 conn.Close()
  50.             End Try
  51.         Else
  52.             If UserManager.UserImage IsNot Nothing Then
  53.                 PictureBox2.Image = UserManager.UserImage
  54.             Else
  55.                 PictureBox2.Image = PictureBox2.InitialImage
  56.             End If
  57.         End If
  58.  
  59. -------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement