Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /www/wwwroot/www.angcn.net/source/class/class_core.php on line 27
[教程]Visual Basic .net起步教程(号外)——AngCN 附件上传准备工具:完整源代码 - 张韶涵中文网--论坛 - Powered by Discuz!

张韶涵中文网--论坛

标题: [教程]Visual Basic .net起步教程(号外)——AngCN 附件上传准备工具:完整源代码 [打印本页]

作者: x.n.a    时间: 2008-7-18 18:03
标题: [教程]Visual Basic .net起步教程(号外)——AngCN 附件上传准备工具:完整源代码
Form1.vb
  1. '--------------------------------------
  2. ' Form1.vb
  3. '--------------------------------------
  4. ' AngCN附件上传准备工具主体&算法
  5. ' 版本 1.0.0
  6. ' (c) 2008 xcVista
  7. '--------------------------------------

  8. Imports System
  9. Imports System.ComponentModel
  10. Imports System.Convert
  11. Imports System.Drawing
  12. Imports System.Drawing.Imaging
  13. Imports System.IO
  14. Imports System.IO.Path
  15. Imports System.Math
  16. Imports System.Threading
  17. Imports System.Windows.Forms

  18. ''' <summary>
  19. ''' AngCN 附件上传工具——主窗体
  20. ''' </summary>
  21. ''' <remarks></remarks>
  22. Public Class Form1
  23.   '需要用到的类变量(不需要其他类看到就用Private修饰符)
  24.   Private IsPictureDone As Boolean '图像生成好标志
  25.   Private TempFile As String '临时文件名——很重要
  26.   Private FileExt As String '目标文件扩展名——很重要
  27.   Private FileSize As Long '文件大小——提示用户用

  28.   Private Sub Button2_Click(ByVal sender As System.Object, _
  29.       ByVal e As System.EventArgs) Handles Button2.Click
  30.     '呼叫远程线程——省得操作卡机器
  31.     bwWork.RunWorkerAsync()
  32.   End Sub

  33.   Private Sub bwWork_DoWork(ByVal sender As System.Object, _
  34.     ByVal e As System.ComponentModel.DoWorkEventArgs) _
  35.     Handles bwWork.DoWork

  36.     '先把输入禁掉,免得用户惹事。
  37.     GroupBox1.Enabled = False
  38.     GroupBox2.Enabled = False
  39.     Button2.Enabled = False

  40.     '打开忙碌鼠标指针
  41.     Me.UseWaitCursor = True

  42.     '搞清楚图像是不是准备好了。
  43.     If IsPictureDone Then
  44.       '图像已经生成

  45.       '准备两个buffer
  46.       Dim Buffer(0 To 1)() As Byte
  47.       Dim fs As FileStream

  48.       '读入两个文件
  49.       fs = New FileStream(TempFile, FileMode.Open)
  50.       Buffer(0) = New Byte(fs.Length) {}
  51.       fs.Read(Buffer(0), 0, fs.Length)
  52.       fs.Close()
  53.       fs.Dispose()

  54.       '技巧:引用类型变量可以重复使用——只要重新赋值
  55.       '(一般赋New)
  56.       fs = New FileStream(TextBox1.Text, FileMode.Open)
  57.       Buffer(1) = New Byte(fs.Length) {}
  58.       fs.Read(Buffer(1), 0, fs.Length)
  59.       fs.Close()
  60.       fs.Dispose()

  61.       fs = New FileStream(GetFullPath(TextBox1.Text) _
  62.         + GetFileNameWithoutExtension(TextBox1.Text) _
  63.         + "." + FileExt, FileMode.OpenOrCreate)
  64.       '技巧:重复操作由循环代替
  65.       For Each bf As Byte() In Buffer
  66.         fs.Write(bf, 0, bf.Length)
  67.       Next
  68.       fs.Close()
  69.       fs.Dispose()
  70.     Else
  71.       '图像还需要生成

  72.       '先准备好环境
  73.       Dim fs As New FileStream(TempFile, _
  74.        FileMode.OpenOrCreate)

  75.       '生成空白图片
  76.       Dim img As New Bitmap(CInt(NumericUpDown1.Value), _
  77.        CInt(NumericUpDown2.Value), _
  78.        PixelFormat.Format24bppRgb)

  79.       '保存图片文件
  80.       Select Case ComboBox1.SelectedIndex
  81.         Case 0
  82.           FileExt = "jpg"
  83.           img.Save(fs, ImageFormat.Jpeg)
  84.         Case 1
  85.           FileExt = "gif"
  86.           img.Save(fs, ImageFormat.Gif)
  87.         Case 2
  88.           FileExt = "png"
  89.           img.Save(fs, ImageFormat.Png)
  90.       End Select
  91.       FileSize = fs.Length

  92.       '释放系统资源
  93.       fs.Close()
  94.       img.Dispose()
  95.     End If

  96.     '还原界面
  97.     Label5.Text = GetFileSizeString(120000L - FileSize)
  98.     GroupBox2.Enabled = True
  99.     Button2.Enabled = True
  100.     Me.UseWaitCursor = False
  101.     IsPictureDone = True
  102.   End Sub

  103.   Private Sub Form1_Load(ByVal sender As System.Object, _
  104.       ByVal e As System.EventArgs) Handles MyBase.Load
  105.     '准备开机环境
  106.     ComboBox1.SelectedIndex = 0
  107.     Label5.Text = GetFileSizeString(120000L)
  108.     TempFile = My.Computer.FileSystem.GetTempFileName

  109.     If LCase(Command) = "-gosite" Then
  110.       Me.Hide()
  111.       Shell("""" + _
  112.          My.Computer.FileSystem.SpecialDirectories.ProgramFiles + _
  113.          "/Internet Explorer/iexplore.exe"" ""http://bbs.angcn.com/""", _
  114.         AppWinStyle.NormalFocus)
  115.       Me.Close()
  116.     End If

  117. #If DEBUG Then
  118.     Control.CheckForIllegalCrossThreadCalls = False
  119. #End If
  120.   End Sub

  121.   Private Sub Form1_Closing(ByVal sender As Object, _
  122.       ByVal e As CancelEventArgs) Handles Me.Closing
  123.     '清理临时文件
  124.     File.Delete(TempFile)
  125.   End Sub

  126.   Public Function GetFileSizeString(ByVal Length As Long) _
  127.   As String
  128.     Dim fn() As String = New String() {"字节", "KB", _
  129.       "MB", "GB", "TB", "YB"}
  130.     Dim i As Integer = 0
  131.     Dim size As Double = Length

  132.     Do Until Abs(size) < 1024.0
  133.       size /= 1024.0
  134.       i += 1
  135.     Loop

  136.     Return String.Format("{0:0.00} {1}", size, fn(i))
  137.   End Function

  138.   Private Sub Button1_Click(ByVal sender As System.Object, _
  139.     ByVal e As System.EventArgs) Handles Button1.Click
  140.     With ofd
  141.       .FileName = ""
  142.       If .ShowDialog(Me) = _
  143.       Windows.Forms.DialogResult.Cancel Then Exit Sub
  144.       TextBox1.Text = .FileName

  145.       If New FileInfo(.FileName).Length + FileSize > _
  146.       120000L Then MessageBox.Show(Me, _
  147.       "文件大小超出允许范围,可能无法上传。", _
  148.       Me.Text, MessageBoxButtons.OK, _
  149.       MessageBoxIcon.Warning, _
  150.       MessageBoxDefaultButton.Button1)
  151.     End With
  152.   End Sub
  153. End Class
复制代码
Form1.Designer.vb
  1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  2. Partial Class Form1
  3.   Inherits System.Windows.Forms.Form

  4.   'Form 重写 Dispose,以清理组件列表。
  5.   <System.Diagnostics.DebuggerNonUserCode()> _
  6.   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  7.     Try
  8.       If disposing AndAlso components IsNot Nothing Then
  9.         components.Dispose()
  10.       End If
  11.     Finally
  12.       MyBase.Dispose(disposing)
  13.     End Try
  14.   End Sub

  15.   'Windows 窗体设计器所必需的
  16.   Private components As System.ComponentModel.IContainer

  17.   '注意: 以下过程是 Windows 窗体设计器所必需的
  18.   '可以使用 Windows 窗体设计器修改它。
  19.   '不要使用代码编辑器修改它。
  20.   <System.Diagnostics.DebuggerStepThrough()> _
  21.   Private Sub InitializeComponent()
  22.     Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
  23.     Me.GroupBox1 = New System.Windows.Forms.GroupBox
  24.     Me.Label3 = New System.Windows.Forms.Label
  25.     Me.NumericUpDown2 = New System.Windows.Forms.NumericUpDown
  26.     Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown
  27.     Me.Label2 = New System.Windows.Forms.Label
  28.     Me.ComboBox1 = New System.Windows.Forms.ComboBox
  29.     Me.Label1 = New System.Windows.Forms.Label
  30.     Me.GroupBox2 = New System.Windows.Forms.GroupBox
  31.     Me.Button1 = New System.Windows.Forms.Button
  32.     Me.TextBox1 = New System.Windows.Forms.TextBox
  33.     Me.Label6 = New System.Windows.Forms.Label
  34.     Me.Label5 = New System.Windows.Forms.Label
  35.     Me.Label4 = New System.Windows.Forms.Label
  36.     Me.Button2 = New System.Windows.Forms.Button
  37.     Me.bwWork = New System.ComponentModel.BackgroundWorker
  38.     Me.ofd = New System.Windows.Forms.OpenFileDialog
  39.     Me.GroupBox1.SuspendLayout()
  40.     CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).BeginInit()
  41.     CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
  42.     Me.GroupBox2.SuspendLayout()
  43.     Me.SuspendLayout()
  44.     '
  45.     'GroupBox1
  46.     '
  47.     Me.GroupBox1.Controls.Add(Me.Label3)
  48.     Me.GroupBox1.Controls.Add(Me.NumericUpDown2)
  49.     Me.GroupBox1.Controls.Add(Me.NumericUpDown1)
  50.     Me.GroupBox1.Controls.Add(Me.Label2)
  51.     Me.GroupBox1.Controls.Add(Me.ComboBox1)
  52.     Me.GroupBox1.Controls.Add(Me.Label1)
  53.     Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
  54.     Me.GroupBox1.Name = "GroupBox1"
  55.     Me.GroupBox1.Size = New System.Drawing.Size(307, 79)
  56.     Me.GroupBox1.TabIndex = 0
  57.     Me.GroupBox1.TabStop = False
  58.     Me.GroupBox1.Text = "外框图像"
  59.     '
  60.     'Label3
  61.     '
  62.     Me.Label3.AutoSize = True
  63.     Me.Label3.Location = New System.Drawing.Point(223, 53)
  64.     Me.Label3.Name = "Label3"
  65.     Me.Label3.Size = New System.Drawing.Size(15, 16)
  66.     Me.Label3.TabIndex = 5
  67.     Me.Label3.Text = "X"
  68.     '
  69.     'NumericUpDown2
  70.     '
  71.     Me.NumericUpDown2.Location = New System.Drawing.Point(244, 51)
  72.     Me.NumericUpDown2.Maximum = New Decimal(New Integer() {65, 0, 0, 0})
  73.     Me.NumericUpDown2.Name = "NumericUpDown2"
  74.     Me.NumericUpDown2.Size = New System.Drawing.Size(57, 22)
  75.     Me.NumericUpDown2.TabIndex = 4
  76.     Me.NumericUpDown2.Value = New Decimal(New Integer() {15, 0, 0, 0})
  77.     '
  78.     'NumericUpDown1
  79.     '
  80.     Me.NumericUpDown1.Location = New System.Drawing.Point(160, 51)
  81.     Me.NumericUpDown1.Maximum = New Decimal(New Integer() {65, 0, 0, 0})
  82.     Me.NumericUpDown1.Name = "NumericUpDown1"
  83.     Me.NumericUpDown1.Size = New System.Drawing.Size(57, 22)
  84.     Me.NumericUpDown1.TabIndex = 3
  85.     Me.NumericUpDown1.Value = New Decimal(New Integer() {15, 0, 0, 0})
  86.     '
  87.     'Label2
  88.     '
  89.     Me.Label2.AutoSize = True
  90.     Me.Label2.Location = New System.Drawing.Point(6, 53)
  91.     Me.Label2.Name = "Label2"
  92.     Me.Label2.Size = New System.Drawing.Size(52, 16)
  93.     Me.Label2.TabIndex = 2
  94.     Me.Label2.Text = "图像大小"
  95.     '
  96.     'ComboBox1
  97.     '
  98.     Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
  99.     Me.ComboBox1.FormattingEnabled = True
  100.     Me.ComboBox1.Items.AddRange(New Object() {"联合图象专家组(JPEG)", "图形交换格式(GIF)", _
  101.     "W3C 可移植网络图像(PNG)"})
  102.     Me.ComboBox1.Location = New System.Drawing.Point(64, 21)
  103.     Me.ComboBox1.Name = "ComboBox1"
  104.     Me.ComboBox1.Size = New System.Drawing.Size(237, 24)
  105.     Me.ComboBox1.TabIndex = 1
  106.     '
  107.     'Label1
  108.     '
  109.     Me.Label1.AutoSize = True
  110.     Me.Label1.Location = New System.Drawing.Point(6, 24)
  111.     Me.Label1.Name = "Label1"
  112.     Me.Label1.Size = New System.Drawing.Size(52, 16)
  113.     Me.Label1.TabIndex = 0
  114.     Me.Label1.Text = "图像格式"
  115.     '
  116.     'GroupBox2
  117.     '
  118.     Me.GroupBox2.Controls.Add(Me.Button1)
  119.     Me.GroupBox2.Controls.Add(Me.TextBox1)
  120.     Me.GroupBox2.Controls.Add(Me.Label6)
  121.     Me.GroupBox2.Controls.Add(Me.Label5)
  122.     Me.GroupBox2.Controls.Add(Me.Label4)
  123.     Me.GroupBox2.Enabled = False
  124.     Me.GroupBox2.Location = New System.Drawing.Point(12, 91)
  125.     Me.GroupBox2.Name = "GroupBox2"
  126.     Me.GroupBox2.Size = New System.Drawing.Size(307, 66)
  127.     Me.GroupBox2.TabIndex = 1
  128.     Me.GroupBox2.TabStop = False
  129.     Me.GroupBox2.Text = "上传文件"
  130.     '
  131.     'Button1
  132.     '
  133.     Me.Button1.Location = New System.Drawing.Point(226, 37)
  134.     Me.Button1.Name = "Button1"
  135.     Me.Button1.Size = New System.Drawing.Size(75, 23)
  136.     Me.Button1.TabIndex = 4
  137.     Me.Button1.Text = "浏览(&B)"
  138.     Me.Button1.UseVisualStyleBackColor = True
  139.     '
  140.     'TextBox1
  141.     '
  142.     Me.TextBox1.Location = New System.Drawing.Point(42, 37)
  143.     Me.TextBox1.Name = "TextBox1"
  144.     Me.TextBox1.ReadOnly = True
  145.     Me.TextBox1.Size = New System.Drawing.Size(178, 22)
  146.     Me.TextBox1.TabIndex = 3
  147.     '
  148.     'Label6
  149.     '
  150.     Me.Label6.AutoSize = True
  151.     Me.Label6.Location = New System.Drawing.Point(6, 40)
  152.     Me.Label6.Name = "Label6"
  153.     Me.Label6.Size = New System.Drawing.Size(30, 16)
  154.     Me.Label6.TabIndex = 2
  155.     Me.Label6.Text = "文件"
  156.     '
  157.     'Label5
  158.     '
  159.     Me.Label5.Location = New System.Drawing.Point(108, 18)
  160.     Me.Label5.Name = "Label5"
  161.     Me.Label5.Size = New System.Drawing.Size(193, 16)
  162.     Me.Label5.TabIndex = 1
  163.     Me.Label5.Text = "120 KB"
  164.     Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
  165.     '
  166.     'Label4
  167.     '
  168.     Me.Label4.AutoSize = True
  169.     Me.Label4.Location = New System.Drawing.Point(6, 18)
  170.     Me.Label4.Name = "Label4"
  171.     Me.Label4.Size = New System.Drawing.Size(96, 16)
  172.     Me.Label4.TabIndex = 0
  173.     Me.Label4.Text = "最大单个文件大小"
  174.     '
  175.     'Button2
  176.     '
  177.     Me.Button2.Location = New System.Drawing.Point(244, 163)
  178.     Me.Button2.Name = "Button2"
  179.     Me.Button2.Size = New System.Drawing.Size(75, 23)
  180.     Me.Button2.TabIndex = 2
  181.     Me.Button2.Text = "生成(&G)"
  182.     Me.Button2.UseVisualStyleBackColor = True
  183.     '
  184.     'bwWork
  185.     '
  186.     '
  187.     'ofd
  188.     '
  189.     Me.ofd.FileName = "OpenFileDialog1"
  190.     '
  191.     'Form1
  192.     '
  193.     Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 16.0!)
  194.     Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  195.     Me.ClientSize = New System.Drawing.Size(331, 198)
  196.     Me.Controls.Add(Me.Button2)
  197.     Me.Controls.Add(Me.GroupBox2)
  198.     Me.Controls.Add(Me.GroupBox1)
  199.     Me.Font = New System.Drawing.Font("微软雅黑", 8.25!, System.Drawing.FontStyle.Regular, _
  200.     System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  201.     Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
  202.     Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
  203.     Me.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
  204.     Me.MaximizeBox = False
  205.     Me.Name = "Form1"
  206.     Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  207.     Me.Text = "AngCN 论坛附件上传准备工具"
  208.     Me.GroupBox1.ResumeLayout(False)
  209.     Me.GroupBox1.PerformLayout()
  210.     CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).EndInit()
  211.     CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
  212.     Me.GroupBox2.ResumeLayout(False)
  213.     Me.GroupBox2.PerformLayout()
  214.     Me.ResumeLayout(False)
  215.   End Sub

  216.   Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  217.   Friend WithEvents Label3 As System.Windows.Forms.Label
  218.   Friend WithEvents NumericUpDown2 As System.Windows.Forms.NumericUpDown
  219.   Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
  220.   Friend WithEvents Label2 As System.Windows.Forms.Label
  221.   Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
  222.   Friend WithEvents Label1 As System.Windows.Forms.Label
  223.   Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
  224.   Friend WithEvents Label5 As System.Windows.Forms.Label
  225.   Friend WithEvents Label4 As System.Windows.Forms.Label
  226.   Friend WithEvents Label6 As System.Windows.Forms.Label
  227.   Friend WithEvents Button1 As System.Windows.Forms.Button
  228.   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  229.   Friend WithEvents Button2 As System.Windows.Forms.Button
  230.   Friend WithEvents bwWork As System.ComponentModel.BackgroundWorker
  231.   Friend WithEvents ofd As System.Windows.Forms.OpenFileDialog
  232. End Class
复制代码
下载地址:http://bbs.angcn.com/viewthread.php?tid=23084&extra=page%3D1&page=2(20楼)

[ 本帖最后由 xcvista 于 2008-7-18 06:08 PM 编辑 ]
作者: 涵涵永远幸福    时间: 2008-7-18 18:18
谢谢了
不好意思
vb学起来头晕
作者: x.n.a    时间: 2008-7-18 18:26
引用模块:
  1. System.dll
  2. System.Drawing.dll
  3. System.Windows.Forms.dll
复制代码
在集成环境(从.net 2005开始即可使用)里创建新空白项目,引入以上三个程序集,把这两个文件加入项目,重新选择图标,编译——成品就来了——这个可是完整源代码!!!
作者: 直角圆形    时间: 2008-7-18 20:50
毕竟这是专业人士写的

顶起
作者: x.n.a    时间: 2008-7-18 21:41
标题: 回复 4# 的帖子
谢谢~~~~~~




欢迎光临 张韶涵中文网--论坛 (http://www.angcn.net/) Powered by Discuz! X3.3