16,717
社区成员
发帖
与我相关
我的任务
分享
Public Sub Dram(ByVal p_img_Original As Image, ByVal p_int_Width As Integer, ByVal p_int_Height As Integer)
Dim l_int_OriWidth As Integer = p_img_Original.Width
Dim l_int_OriHeight As Integer = p_img_Original.Height
Dim l_int_IntWidth As Integer = 0
Dim l_int_IntHeight As Integer = 0
Dim l_int_OffsetX As Integer = 0
Dim l_int_OffsetY As Integer = 0
' 以寬度為基準,保持寬高比計算縮放之後的高度
l_int_IntWidth = p_int_Width
l_int_IntHeight = l_int_OriHeight * l_int_IntWidth / l_int_OriWidth
' 如果縮放之後的高度不夠新高度,則改用以高度為基準,保持寬高比計算縮放之後的寬度
If l_int_IntHeight < p_int_Height Then
l_int_IntHeight = p_int_Height
l_int_IntWidth = l_int_OriWidth * l_int_IntHeight / l_int_OriHeight
' 計算圖片偏移位置
Else
' 計算圖片偏移位置
l_int_OffsetY = (p_int_Height - l_int_IntHeight) / 4 ' 垂直居中偏上
End If
' 準備畫布
Dim l_bmp_New As Bitmap = New Bitmap(p_int_Width, p_int_Height)
Dim l_gph_New As Graphics = Graphics.FromImage(l_bmp_New)
l_gph_New.InterpolationMode = InterpolationMode.High
l_gph_New.SmoothingMode = SmoothingMode.HighQuality
l_gph_New.Clear(Color.Transparent)
' 生成圖片
l_gph_New.DrawImage(p_img_Original, New Rectangle(l_int_OffsetX, l_int_OffsetY, l_int_IntWidth, l_int_IntHeight))
'输出图片
'prepare output stream buffer
Dim l_ms_Buffer As MemoryStream = New MemoryStream()
l_bmp_New.Save(l_ms_Buffer, ImageFormat.Jpeg)
' output to browser
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "image/jpeg"
HttpContext.Current.Response.BinaryWrite(l_ms_Buffer.ToArray())
End Sub
Public Sub Dram(ByVal p_img_Original As Image, ByVal p_int_Width As Integer, ByVal p_int_Height As Integer, ByVal p_int_Direction As Integer, ByVal p_str_SavePath As String)
Dim l_int_OriginalWidth As Integer = p_img_Original.Width
Dim l_int_OriginalHight As Integer = p_img_Original.Height
Dim l_int_TargetWidth As Integer = 0
Dim l_int_TargetHight As Integer = 0
If p_int_Width <= p_int_Height Then
l_int_TargetHight = p_int_Height
l_int_TargetWidth = l_int_OriginalWidth * l_int_TargetHight / l_int_OriginalHight
Else
l_int_TargetWidth = p_int_Width
l_int_TargetHight = l_int_OriginalHight * l_int_TargetWidth / l_int_OriginalWidth
End If
Dim l_bmp_TempOut As New Bitmap(l_int_TargetWidth, l_int_TargetHight)
Dim l_grp_TempGraphics As Graphics = Graphics.FromImage(l_bmp_TempOut)
l_grp_TempGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
l_grp_TempGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
l_grp_TempGraphics.Clear(Color.Transparent)
l_grp_TempGraphics.DrawImage(p_img_Original, New Rectangle(0, 0, l_int_TargetWidth, l_int_TargetHight), New Rectangle(0, 0, l_int_OriginalWidth, l_int_OriginalHight), GraphicsUnit.Pixel)
Dim l_bmp_TargetOut As New Bitmap(p_int_Width, p_int_Height)
Dim l_grp_TargetGraphics As Graphics = Graphics.FromImage(l_bmp_TargetOut)
l_grp_TargetGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
l_grp_TargetGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
l_grp_TargetGraphics.Clear(Color.Transparent)
Dim obj As Object = l_bmp_TempOut
Dim l_img_TragImage As System.Drawing.Image = CType(l_bmp_TempOut, System.Drawing.Image)
Dim l_int_x As Integer = 0
Dim l_int_y As Integer = 0
If p_int_Direction = 0 Then
l_int_x = (l_int_TargetWidth - p_int_Width) / 2
l_int_y = (l_int_TargetHight - p_int_Height) / 2
ElseIf p_int_Direction = 1 Then '左上
l_int_x = 0
l_int_y = 0
ElseIf p_int_Direction = 2 Then '左下
l_int_x = 0
l_int_y = l_int_TargetHight - p_int_Height
ElseIf p_int_Direction = 3 Then '右上
l_int_x = l_int_TargetWidth - p_int_Width
l_int_y = 0
ElseIf p_int_Direction = 4 Then '右下
l_int_x = l_int_TargetWidth - p_int_Width
l_int_y = l_int_TargetHight - p_int_Height
End If
l_grp_TargetGraphics.DrawImage(l_img_TragImage, New Rectangle(0, 0, p_int_Width, p_int_Height), New Rectangle(l_int_x, l_int_y, p_int_Width, p_int_Height), GraphicsUnit.Pixel)
'保存图片
Dim DirectoryStr As String = p_str_SavePath.Substring(0, p_str_SavePath.LastIndexOf("\") + 1)
If Not Directory.Exists(DirectoryStr) Then
Directory.CreateDirectory(DirectoryStr)
End If
l_bmp_TargetOut.Save(p_str_SavePath)
l_bmp_TempOut.Dispose()
l_bmp_TargetOut.Dispose()
l_grp_TempGraphics.Dispose()
l_grp_TargetGraphics.Dispose()
p_img_Original.Dispose()
''输出图片
''prepare output stream buffer
'Dim l_ms_Buffer As MemoryStream = New MemoryStream()
'l_bmp_TargetOut.Save(l_ms_Buffer, ImageFormat.Jpeg)
''output to browser
'HttpContext.Current.Response.Clear()
'Image1.ImageUrl = Server.MapPath("~") + "\TestImage\test.jpg"
'HttpContext.Current.Response.ContentType = "image/jpeg"
'HttpContext.Current.Response.BinaryWrite(l_ms_Buffer.ToArray())
End Sub