上传图片并生成缩略图(ASP.NET+VB.NET)
2004-10-08 19:55
615 查看
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
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
相关文章推荐
- 在ASP.NET 2.0中使用页面导航控件(原作)
- ASP.NET中上传文件的方法(一)
- 坚持学asp.net——(十二)
- 用80386汇编来编写asp.net页面。
- 实现一个ASP.NET Back控件
- 怎么用asp解决上传音频视频文件问题
- 上班了~。~
- adodb.stream 上传问题
- 第二部分:使用Jasperreport作为报表控件开发胖客户端报表应用
- asp程序错误详细说明例表
- 函数在asp与asp.net中的运用
- 在ASP.NET中跨页面实现多选
- How To Create Custom Skins For DotText(如何为DotText创建自定义风格的皮肤)
- 浅谈在ASP.NET中数据有效性校验的方法
- Detecting ASP.NET Session Timeouts
- 对ASP.NET的漏洞采取措施
- ASP.NET可交互式位图窗体设计(1)
- ASP.NET可交互式位图窗体设计(2)
- ASP.NET中Cookie编程的基础知识
- ASP.NET应用程序规划与设计