我用如下程序片段处理图片。在未指定新的width和height时,添加水印的程序片段可以成功执行,并输出我想要的加水印后的图片。但是一旦我指定了width或者height后,也就是说resize的程序片段被执行后,添加水印的程序片段依然能够被顺利执行,但是输出的图片能够成功被resize,但是不能加水印。请教一下这是什么原因,怎样解决,谢谢! If IsNothing(Request("image")) Then Throw New Exception("Image file name must be provided!")
Dim strSourceImage As String = Request("image").Replace("/", "\").ToLower()
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
If Not IsNothing(Request("w")) Then iWidth = CInt(Request("w"))
If Not IsNothing(Request("h")) Then iHeight = CInt(Request("h"))
Dim strActions As String = Request("action")
Dim strAction As String()
If Not IsNothing(strActions) Then strAction = strActions.ToLower().Split(",") If strSourceImage.Length < 6 Then Throw New Exception("Invalid Image file name or path!")
Dim strExt As String = Helper.GetExtension(strSourceImage)
If strExt <> "jpg" And strExt <> "bmp" And strExt <> "gif" And strExt <> "png" And strExt <> "jpeg" Then Throw New Exception("Only image format is acceptable!") Dim strPath As String = "" If strSourceImage.Substring(0, 1) = "\" Then strPath = Helper.SiteRootPath() & strSourceImage
If strSourceImage.Substring(0, 4) = "http" Then
strPath = Server.MapPath("~/") & Config.TempDir.Substring(1) & System.Guid.NewGuid().ToString() & "." & strExt
Dim myWebClient As New WebClient()
Response.Write(strSourceImage.Replace("\", "/") & "<br>")
myWebClient.DownloadFile(strSourceImage.Replace("\", "/"), strPath)
End If
If strPath = "" Then strPath = Server.MapPath(strSourceImage) Dim bmpImage As New Bitmap(strPath)
Dim gConvas As Graphics = Graphics.FromImage(bmpImage) Dim f As ImageFormat
Dim strContentType As String = "image/jpeg"
Select Case strExt
Case "jpg"
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
Case "jpeg"
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
Case "gif"
f = ImageFormat.Gif
strContentType = "image/gif"
Case "png"
f = ImageFormat.Png
strContentType = "image/png"
Case "bmp"
f = ImageFormat.Bmp
strContentType = "image/bmp"
Case Else
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
End Select 'Resize image
If iWidth > 0 And iHeight > 0 Then
If bmpImage.Width / bmpImage.Height > iWidth / iHeight Then
iHeight = CInt(iWidth * bmpImage.Height / bmpImage.Width)
Else
iWidth = CInt(iHeight * bmpImage.Width / bmpImage.Height)
End If
gConvas = Graphics.FromImage(bmpImage)
bmpImage = ResizeImage(bmpImage, iWidth, iHeight, f)
End If '加水印
If bmpImage.Width >= 200 And bmpImage.Height >= 200 Then
Dim bmpWaterMark As Bitmap = New Bitmap(Helper.SiteRootPath & ConfigurationManager.AppSettings("WaterMark"))
bmpWaterMark.MakeTransparent(Color.White)
gConvas.SmoothingMode = SmoothingMode.HighQuality
gConvas.DrawImage(bmpWaterMark, 20, 20)
End If 'Special action
If Not IsNothing(strAction) Then
For i As Integer = 0 To strAction.Length - 1
Select Case strAction(i)
Case "gray" Case "roundcorner" Case "frame" '必须最后进行加相框的处理,因为当所有基本处理运行完毕后才有必要放大图像并加相框 Case Else End Select
Next
End If 'Output image
Response.ContentType = strContentType
bmpImage.Save(Response.OutputStream, f) 'Remove temp files
Helper.CleanTempDir()
Dim strSourceImage As String = Request("image").Replace("/", "\").ToLower()
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
If Not IsNothing(Request("w")) Then iWidth = CInt(Request("w"))
If Not IsNothing(Request("h")) Then iHeight = CInt(Request("h"))
Dim strActions As String = Request("action")
Dim strAction As String()
If Not IsNothing(strActions) Then strAction = strActions.ToLower().Split(",") If strSourceImage.Length < 6 Then Throw New Exception("Invalid Image file name or path!")
Dim strExt As String = Helper.GetExtension(strSourceImage)
If strExt <> "jpg" And strExt <> "bmp" And strExt <> "gif" And strExt <> "png" And strExt <> "jpeg" Then Throw New Exception("Only image format is acceptable!") Dim strPath As String = "" If strSourceImage.Substring(0, 1) = "\" Then strPath = Helper.SiteRootPath() & strSourceImage
If strSourceImage.Substring(0, 4) = "http" Then
strPath = Server.MapPath("~/") & Config.TempDir.Substring(1) & System.Guid.NewGuid().ToString() & "." & strExt
Dim myWebClient As New WebClient()
Response.Write(strSourceImage.Replace("\", "/") & "<br>")
myWebClient.DownloadFile(strSourceImage.Replace("\", "/"), strPath)
End If
If strPath = "" Then strPath = Server.MapPath(strSourceImage) Dim bmpImage As New Bitmap(strPath)
Dim gConvas As Graphics = Graphics.FromImage(bmpImage) Dim f As ImageFormat
Dim strContentType As String = "image/jpeg"
Select Case strExt
Case "jpg"
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
Case "jpeg"
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
Case "gif"
f = ImageFormat.Gif
strContentType = "image/gif"
Case "png"
f = ImageFormat.Png
strContentType = "image/png"
Case "bmp"
f = ImageFormat.Bmp
strContentType = "image/bmp"
Case Else
f = ImageFormat.Jpeg
strContentType = "image/jpeg"
End Select 'Resize image
If iWidth > 0 And iHeight > 0 Then
If bmpImage.Width / bmpImage.Height > iWidth / iHeight Then
iHeight = CInt(iWidth * bmpImage.Height / bmpImage.Width)
Else
iWidth = CInt(iHeight * bmpImage.Width / bmpImage.Height)
End If
gConvas = Graphics.FromImage(bmpImage)
bmpImage = ResizeImage(bmpImage, iWidth, iHeight, f)
End If '加水印
If bmpImage.Width >= 200 And bmpImage.Height >= 200 Then
Dim bmpWaterMark As Bitmap = New Bitmap(Helper.SiteRootPath & ConfigurationManager.AppSettings("WaterMark"))
bmpWaterMark.MakeTransparent(Color.White)
gConvas.SmoothingMode = SmoothingMode.HighQuality
gConvas.DrawImage(bmpWaterMark, 20, 20)
End If 'Special action
If Not IsNothing(strAction) Then
For i As Integer = 0 To strAction.Length - 1
Select Case strAction(i)
Case "gray" Case "roundcorner" Case "frame" '必须最后进行加相框的处理,因为当所有基本处理运行完毕后才有必要放大图像并加相框 Case Else End Select
Next
End If 'Output image
Response.ContentType = strContentType
bmpImage.Save(Response.OutputStream, f) 'Remove temp files
Helper.CleanTempDir()
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货