频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

上传图片并生成缩略图(ASP.NET+VB.NET)

来源: 作者: 出处:巧巧读书 2007-03-12 进入讨论组

讨论组http://group.qqread.com

    Sub upimages()
        Dim iname As String
        If Not (fileup.PostedFile Is Nothing) Then

            Dim namestr1 As String = (fileup.PostedFile.FileName)
            If LCase(fileup.PostedFile.ContentType.ToString()) = "image/pjpeg" Or LCase(fileup.PostedFile.ContentType.ToString()) = "image/jpg" Then
              Dim j As Integer = namestr1.LastIndexOf(".")
              Dim newname As String = namestr1.Substring(j) '圖片得到後輟名
              iname = CStr(Now.ToFileTimeUtc) '隨機的文件名(不會重復)
              Dim newnames As String = iname + newname '重新組合文件名
              Dim i As Integer = namestr1.LastIndexOf("\") + 1
              Dim namestr As String = namestr1.Substring(i)
              fileup.PostedFile.SaveAs(Server.MapPath(imagelocal) + "\" + newnames) '保存文件到imagelocal文件夾
              '生成縮略圖()
              Dim image, sImage As System.Drawing.Image
              image = System.Drawing.Image.FromStream(fileup.PostedFile.InputStream)'得到原图
              Dim width As Decimal = image.Width过且过'得到原图的宽
              Dim height As Decimal = image.Height'得到原图的高
              Dim newwidth, newheight As Integer
              '设置缩略图的高和宽
              If (width > height) Then
                  newwidth = 150
                  newheight = CInt(height / width * 150)
              Else
                   newheight = 150
                   newwidth = CInt(width / height * 150)
              End If
              sImage = image.GetThumbnailImage(newwidth, newheight, Nothing, IntPtr.Zero)
                Dim x As Integer = sImage.Width / 2 - 30
                Dim y As Integer = sImage.Height - 20
               Dim output As Bitmap = New Bitmap(sImage)
               Dim g As Graphics = Graphics.FromImage(output)
               '    給縮略圖加上版權信息()
                Dim fonts As New Font("Courier New", 9)
                g.DrawString("版權信息", fonts, New SolidBrush(Color.Red), x, y)
               output.Save(Server.MapPath("Simagelocal") + "\s_" + newnames, System.Drawing.Imaging.ImageFormat.Jpeg) 
              '保存縮略圖到Simagelocal文件夾
                Image1.Visible=true;
               Image1.ImageUrl = "Simagelocal" + "\s_" + newnames

            Else
               Label1.Text = "請選擇jpg類型的圖片"

            End If
        End If
    End Sub

更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册ASP.NET教程专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章