加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Winform(VB.NET)批量上传图片

(2013-05-06 16:38:00)
分类: VB.NET(Winform)
http://s10/mw690/ab6b8420g7c6743782f79&690
ps:这个上传图片不能OpenFileDialog后不能一次选择多张,必须每次选择一张...

Code:

一:
ps:这里是OpenFileDialog 开始选择并且判断文件是否是图片,是否有重复选择图片
  Private Sub ChooseUpLoadPic()

        'File Upload
        Dim OTxtB As New OpenFileDialog
        OTxtB.Title = "选择上传图片"
        OTxtB.DefaultExt = "JPEG(*.jpg)"
        OTxtB.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg"
        If OTxtB.ShowDialog() = DialogResult.OK Then
            extension = Path.GetExtension(OTxtB.FileName)
            Dim StrExt As String() = New String() {".bmp", ".jpg", ".jpeg", ".gif", ".bmp", ".png"}
            If Not StrExt.Contains(extension) Then
                MessageBox.Show("仅能上传 bmp, jpg, JPEG, gif, bmp, png 格式的图片!", "提示!", MessageBoxButtons.OK)
                Exit Sub
            End If
            Dim FInfo As New FileInfo(OTxtB.FileName)
            If FInfo.Length > 5242880 Then
                MessageBox.Show("上传的图片不能大于5MB", "提示!", MessageBoxButtons.OK)
                Exit Sub
            End If
            Dim TxtContrast As String()
            TxtContrast = TxtBoxUpImg.Text.Trim().Split("||")
            If Not TxtContrast.Contains(OTxtB.FileName) Then
                TxtBoxUpImg.Text += OTxtB.FileName & "||"
                'PicsName += Convert.ToString(Guid.NewGuid()) & extension & "-"
            Else
                MsgBox("您上传了重复的图片,请重新选择!", MsgBoxStyle.OkOnly, "提示!")
                Exit Sub
            End If
        End If

    End Sub

二:这里是压缩图片大小,并且可以调整图片分辨率
  'Reduce Pictures
    Private img_old, img_new As Bitmap
    Private ReName As String
    Private Dpath As String = "D:\\ReducePic"
    Private Sub Reduce(ByVal down As Double)
        ReName = Convert.ToString(Guid.NewGuid())
        Dim factor As Double
        factor = down / img_old.Size.Width
        img_new = New Bitmap(img_old, New Size(img_old.Size.Width * factor, img_old.Size.Height * factor)) '开始重写压缩图片
        If Not Directory.Exists(Dpath) Then '判断文件夹是否存在,如果不在自动生成
            Directory.CreateDirectory(Dpath)
        End If
        img_new.Save(Dpath & "\" & ReName & ".jpg", Imaging.ImageFormat.Jpeg)
        SendFile() '这里开始发送图片到Server and Insert into SQL
        If InsertPicName(ReName) = 0 Then
            MsgBox("上传图片异常(intsert into sql)!", MsgBoxStyle.OkOnly, "提示!")
            Exit Sub
        End If

    End Sub
三:获取图片大小和比例
    Private Sub FromFile(ByVal filename As String) '
        'Get Current Pic Length
        Dim fs As New System.IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
        Dim imgData(fs.Length) As Byte
        fs.Read(imgData, 0, fs.Length)
        fs.Close()
        Try
            img_old = Image.FromStream(New System.IO.MemoryStream(imgData))
        Catch
        End Try
    End Sub

四:发送图片到Server的方法
    Private Sub SendFile()
        'Send The Pictures To Server
        My.Computer.Network.UploadFile(Dpath & "\" & ReName & ".jpg", "ftp://192.168.8.12:8001/kefu/" & ReName & ".jpg", "fur", "3336")
    End Sub

五:写入数据库
    Private Function InsertPicName(ByVal PName As String) As Integer
        'insert PicName to sql
        InsertPicConditionUpdate(PName) '传入InsertPicConditionUpdate这个方法给MAfterAalesRegisterPictures里面的图片名字赋值
        Return braar.InsertPicName(MAfterAalesRegisterPictures) 'MAfterAalesRegisterPictures是数据库Model,这里开始写入。
    End Function


六:开始调用全部方法进行程序
    Private Function UploadPics() As Boolean
        If Not String.IsNullOrEmpty(TxtBoxUpImg.Text.Trim()) Then
            Dim PathN As String() = TxtBoxUpImg.Text.Trim().Split("||")
            For Each s As String In PathN  '循环txtbox里面的原图片地址进行操作
                If Not String.IsNullOrEmpty(s) Then
                    FromFile(s) '获得图片大小比例
                    Reduce(800) '开始压缩图片大小和更改比例
                End If
            Next
            MessageBox.Show("图片上传成功!", "提交!", MessageBoxButtons.OK)
            LockBtn(False) '锁住按钮方法,不让用户操作,避免线程冲突

        Else
            MsgBox("上传失败,请重试或者联系计算机管理员!", MsgBoxStyle.OkOnly, "提示!")
        End If
    End Function

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有