ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4092|ตอบกลับ: 1

[VB.NET] การอัพโหลดภาพขึ้นสู่เว็บโฮสติ้ง และแสดงผลใน PictureBox Control

[คัดลอกลิงก์]

310

กระทู้

501

โพสต์

6045

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6045



โค้ดชุดนี้แอดมินใช้ Chilkat.NET ในการอัพโหลดไฟล์ภาพ และทดสอบกับฟรีเว็บโฮสติ้ง www.freehostia.com ก็ลองไปสมัครใช้ฟรีกันดูน่ะครับ ...

Add References ...


มาดูโค้ดกันเถอะ ...
  1. Imports System.Net
  2. Imports System.IO
  3. Imports Chilkat.Ftp2

  4. Public Class frmUploadImageURL
  5.     Dim MyURL As String = "http://www.thongkorn.com/upload/"
  6.     Dim RemoteDir = "thongkorn.com/upload/"
  7.     '//
  8.     Dim picFileName As String = String.Empty
  9.     Dim picFullPath As String = String.Empty
  10.     Dim LocalFolder As String = String.Empty

  11.     Private Sub frmUploadImageURL_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  12.         '// สั่งปลดล็อคก่อนใช้งาน
  13.         Call UnlockChilkat()
  14.     End Sub

  15.     '// นำภาพจาก URL มาแสดงผล
  16.     Public Sub ShowImgURL()
  17.         Try
  18.             Dim wc As New WebClient
  19.             Dim imgByte() As Byte = wc.DownloadData(txtRemote.Text)
  20.             Dim imgStream As New MemoryStream(imgByte)
  21.             With picRemote
  22.                 .SizeMode = PictureBoxSizeMode.StretchImage
  23.                 .WaitOnLoad = True ' False
  24.                 .Image = Image.FromStream(imgStream)
  25.             End With

  26.         Catch ex As Exception
  27.             MessageBox.Show(ex.Message)
  28.         End Try
  29.     End Sub

  30.     Private Sub btnUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnUpload.Click
  31.         If txtLocal.Text.Trim = "" Or txtLocal.Text.Length = 0 Then Return
  32.         '//
  33.         Call UploadImageToURL()
  34.         txtRemote.Text = MyURL & picFileName
  35.         '//
  36.         Call ShowImgURL()
  37.     End Sub

  38.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  39.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  40.         ' / Open File Dialog
  41.         With dlgImage
  42.             .Title = "Select images"
  43.             .Filter = "Images types (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  44.             .FilterIndex = 1
  45.             .RestoreDirectory = False 'True
  46.         End With
  47.         ' Select OK after Browse ...
  48.         If dlgImage.ShowDialog() = DialogResult.OK Then
  49.             Dim img As New Bitmap(dlgImage.FileName)
  50.             If img.Size.Width > 600 Or img.Size.Height > 600 Then
  51.                 MessageBox.Show("รูปภาพต้องมีขนาดกว้างและยาวไม่เกิน 600 พิกเซล.", "รายงานความผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
  52.                 Exit Sub
  53.             End If
  54.             '// แยกโฟลเดอร์ และ ชื่อไฟล์
  55.             Dim iArr() As String = {}
  56.             iArr = Split(dlgImage.FileName, "")
  57.             For i = 0 To UBound(iArr) - 1
  58.                 LocalFolder = LocalFolder & iArr(i) & ""
  59.             Next
  60.             '// Get Local folder
  61.             If Microsoft.VisualBasic.Right(LocalFolder, 1) <> "" Then LocalFolder += ""
  62.             '// Get only Filename.
  63.             picFileName = iArr(UBound(iArr))
  64.             '// Image
  65.             picFullPath = dlgImage.FileName
  66.             txtLocal.Text = picFullPath
  67.             picLocal.Image = Image.FromFile(picFullPath)
  68.         End If

  69.     End Sub

  70.     Public Sub UploadImageToURL()
  71.         If txtLocal.Text.Trim = "" Or IsNothing(txtLocal.Text.Trim) Then Return
  72.         '//
  73.         Dim ftp As New Chilkat.Ftp2
  74.         ftp.Hostname = "thongkorn.com"
  75.         ftp.Username = "FTP USERNAME"
  76.         ftp.Password = "FTP PASSWORD"

  77.         '/ Connect and login to the FTP server.
  78.         Dim success As Boolean = ftp.Connect()
  79.         If (success <> True) Then
  80.             MessageBox.Show(ftp.LastErrorText)
  81.             Exit Sub
  82.         End If
  83.         '/ Change to the remote directory where the file will be uploaded.
  84.         success = ftp.ChangeRemoteDir(RemoteDir)
  85.         If (success <> True) Then
  86.             MessageBox.Show(ftp.LastErrorText)
  87.             Exit Sub
  88.         End If
  89.         '/ Upload a file.
  90.         'picFullPath = --> "c:/temp/hamlet.xml"
  91.         'picFileName = --> "hamlet.xml"
  92.         success = ftp.PutFile(picFullPath, picFileName)
  93.         If (success <> True) Then
  94.             MessageBox.Show(ftp.LastErrorText)
  95.             Exit Sub
  96.         End If
  97.         success = ftp.Disconnect()

  98.         MessageBox.Show("File Uploaded Successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  99.     End Sub

  100.     '// Unlock Chilkat.
  101.     Public Sub UnlockChilkat()
  102.         Dim glob As New Chilkat.Global
  103.         Dim success As Boolean = glob.UnlockBundle("CHILKAT KEY")
  104.         If (success <> True) Then
  105.             MessageBox.Show(glob.LastErrorText)
  106.             Exit Sub
  107.         End If
  108.         Dim status As Integer = glob.UnlockStatus
  109.         If (status = 2) Then
  110.             'MessageBox.Show("Unlocked using purchased unlock code.")
  111.         Else
  112.             MessageBox.Show("Unlocked in trial mode.")
  113.         End If
  114.     End Sub

  115.     Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
  116.         picLocal.Image = Nothing
  117.         picFileName = Nothing
  118.         txtLocal.Text = ""
  119.         txtRemote.Text = ""
  120.     End Sub

  121.     Private Sub txtLocal_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocal.KeyPress
  122.         e.Handled = True
  123.     End Sub

  124.     Private Sub txtRemote_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemote.KeyPress
  125.         e.Handled = True
  126.     End Sub
  127. End Class
คัดลอกไปที่คลิปบอร์ด

คลิ๊กดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:28:15 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-24 16:22 , Processed in 0.285130 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้