Vb.Net使用GDI+进行绘图的程序
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class GDIImageChart
Inherits SchoolJwSoft
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Sql2008()
End If
Dim T As String
T = SkyReplaceBad(Request.QueryString("t"))
If T = "1" Then
ShowOneIMages()
Else
ShowMIMages()
End If
End Sub
Public Function DrowDirectionVerticalFont() As StringFormat
Dim drawFormat As StringFormat
drawFormat = New StringFormat
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
DrowDirectionVerticalFont = drawFormat
End Function
Public Function ShowMIMages()
'#################################################
'背景上高度,下文字占用高度
'背景上颜色,背景下颜色
'文字1颜色和大小,文字2颜色和大小
'矩形的填充色和线条色 矩形的宽度和间隔
'#################################################
Dim SkyBgTopH, SkyBgFootH As Integer
Dim SkyBgTopColor, SkyBgFootColor As String
Dim Font1Size, Font2Size As Integer
Dim Font1Color, Font2Color As String
Dim rectangleFillColor, rectangleDrawColor As String
Dim rectangleWidth, rectangleSpace, rectangleAll, rectangleOther As Integer
'##############################################################
Dim MyChartConfig(), Sconfig As String
Sconfig = Session("MyChartConfig")
If Sconfig Is Nothing Or Sconfig = "" Then
Sconfig = MyChartConfigString
End If
MyChartConfig = Sconfig.Split(MyChartSplit)
SkyBgTopH = MyChartConfig(1)
SkyBgFootH = MyChartConfig(2)
SkyBgTopColor = MyChartConfig(3)
SkyBgFootColor = MyChartConfig(4)
Font1Size = MyChartConfig(5)
Font2Size = MyChartConfig(6)
Font1Color = MyChartConfig(7)
Font2Color = MyChartConfig(8)
rectangleFillColor = MyChartConfig(9)
rectangleDrawColor = MyChartConfig(10)
rectangleWidth = MyChartConfig(11)
rectangleSpace = MyChartConfig(12)
'###############################################################
rectangleOther = 10
rectangleAll = rectangleWidth + rectangleSpace
'#################################################
Dim objBitmap As Bitmap
Dim objGraphics As Graphics
Dim decmaxData As Decimal
Dim strBackColor As String
Dim strForeColor As String
Dim ObjBackColor, ObjFont1, ObjFont2, ObjJP, ObjJPFill As Color
Dim decMultiplier As Decimal
Dim intWidth As Integer
Dim intHeight As Integer
Dim arrChartData() As String
Dim arrName() As String
Dim intDataIndex As Integer
Dim intDataItem As Integer
Dim objBrush As SolidBrush
Dim MoveN As Integer
MoveN = SkyBgFootH
'############################################################################
decmaxData = 1
'############################################################################
arrChartData = Session("MyChartNumber")
arrName = Session("MyChartString")
Dim MyChartSNumName As String
Dim MyChartSNum As Integer
MyChartSNumName = Session("MyChartSNumName")
MyChartSNum = CInt(Session("MyChartSNum"))
Dim I, J As Integer
Dim Maxd As Single = 1
Dim ZIndex As Integer
Try
ZIndex = UBound(arrChartData)
Catch ex As Exception
ZIndex = -1
End Try
Dim STmpSNum() As String
For I = 0 To ZIndex
STmpSNum = arrChartData(I).Split(MyChartSplit)
For J = 0 To UBound(STmpSNum)
Maxd = CSng(STmpSNum(J))
If Maxd > decmaxData Then
decmaxData = Maxd
End If
Next
Next
'############################################################################
decMultiplier = SkyBgTopH / decmaxData
'#############################################################################
ObjBackColor = ColorTranslator.FromHtml(SkyBgTopColor)
ObjFont1 = ColorTranslator.FromHtml(Font1Color)
ObjFont2 = ColorTranslator.FromHtml(Font2Color)
ObjJP = ColorTranslator.FromHtml(rectangleDrawColor)
'#############################################################################
'arrChartData = SkyReplaceBad(request.QueryString.GetValues("d")
'arrName = SkyReplaceBad(request.QueryString.GetValues("n")
'#############################################################################
intWidth = (arrChartData.Length * rectangleAll) * MyChartSNum + rectangleOther + 150 '+ (arrChartData.Length - 1) * rectangleAll + 200 '多几倍的位置
intHeight = (decmaxData * decMultiplier) + rectangleSpace + MoveN
'#############################################################################
Dim drawFormat As StringFormat = DrowDirectionVerticalFont() '文字格式
If ZIndex >= 0 Then
objBitmap = New Bitmap(intWidth, intHeight)
objGraphics = Graphics.FromImage(objBitmap)
objGraphics.Clear(ObjBackColor)
'底部背景颜色
objGraphics.FillRectangle(New SolidBrush(ColorTranslator.FromHtml(SkyBgFootColor)), 0, intHeight - MoveN, intWidth, MoveN)
Dim objFont As New Font("Arial", Font1Size)
Dim Brush1 As New SolidBrush(ObjFont1)
Dim Brush2 As New SolidBrush(ObjFont2)
Dim Pen As New Pen(ObjJP, 1)
Dim SLAd, SLAd1 As Integer
Dim STmpSInfo As String
For intDataIndex = 0 To arrChartData.Length - 1
STmpSNum = arrChartData(intDataIndex).Split(MyChartSplit)
SLAd = (intDataIndex) * rectangleAll * MyChartSNum
For I = 1 To MyChartSNum
STmpSInfo = STmpSNum(I - 1)
intDataItem = CSng(STmpSInfo) * decMultiplier
SLAd1 = SLAd + rectangleAll * (I - 1) ' + rectangleSpace
ObjJPFill = ColorTranslator.FromHtml(MyChartConfigColor(I))
objBrush = New SolidBrush(ObjJPFill)
'################################################################################################################################
objGraphics.FillRectangle(objBrush, SLAd1, intHeight - intDataItem - MoveN, rectangleWidth, intDataItem)
objGraphics.DrawRectangle(Pen, SLAd1, intHeight - intDataItem - MoveN, rectangleWidth, intDataItem)
objGraphics.DrawString(STmpSInfo, objFont, Brush1, SLAd1, intHeight - intDataItem - MoveN)
Next
'################################################################################################################################
'objGraphics.FillRectangle(objBrush, (intDataIndex * 25) + 5, intHeight - intDataItem, 20, intHeight)
'################################################################################################################################
'##开始绘制文字颜色1和文字颜色2
objGraphics.DrawString(arrName(intDataIndex), objFont, Brush2, SLAd, intHeight - MoveN + rectangleOther, drawFormat)
'################################################################################################################################
'objGraphics.DrawString(Session("A"), New Font("宋体", 9, FontStyle.Bold), New SolidBrush(Color.White), New PointF(160, 50))
Next
Dim ImgW As Integer = 50
Dim ImgH As Integer = 20
SLAd = (arrChartData.Length) * rectangleAll * MyChartSNum + 10
Dim MyCName() As String
MyCName = MyChartSNumName.Split(MyChartSplit)
For I = 1 To MyChartSNum
STmpSInfo = MyCName(I - 1)
SLAd1 = ImgH * I
ObjJPFill = ColorTranslator.FromHtml(MyChartConfigColor(I))
objBrush = New SolidBrush(ObjJPFill)
'################################################################################################################################
objGraphics.FillRectangle(objBrush, SLAd, SLAd1, ImgW, ImgH - 5)
objGraphics.DrawRectangle(Pen, SLAd, SLAd1, ImgW, ImgH - 5)
objGraphics.DrawString(STmpSInfo, objFont, Brush1, SLAd + ImgW + 5, SLAd1)
Next
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
Else
objBitmap = New Bitmap(200, 30)
objGraphics = Graphics.FromImage(objBitmap)
objGraphics.Clear(Color.White)
objGraphics.DrawString("对不起,系统暂时无数据!", New Font("宋体", 12, FontStyle.Regular), New SolidBrush(Color.Black), New PointF(10, 10))
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
End If
End Function
Public Function ShowOneIMages()
'Response.Write(Session("A"))
'#################################################
'背景上高度,下文字占用高度
'背景上颜色,背景下颜色
'文字1颜色和大小,文字2颜色和大小
'矩形的填充色和线条色 矩形的宽度和间隔
'#################################################
Dim SkyBgTopH, SkyBgFootH As Integer
Dim SkyBgTopColor, SkyBgFootColor As String
Dim Font1Size, Font2Size As Integer
Dim Font1Color, Font2Color As String
Dim rectangleFillColor, rectangleDrawColor As String
Dim rectangleWidth, rectangleSpace, rectangleAll, rectangleOther As Integer
'##############################################################
Dim MyChartConfig(), Sconfig As String
Sconfig = Session("MyChartConfig")
If Sconfig Is Nothing Or Sconfig = "" Then
Sconfig = MyChartConfigString
End If
MyChartConfig = Sconfig.Split(MyChartSplit)
SkyBgTopH = MyChartConfig(1)
SkyBgFootH = MyChartConfig(2)
SkyBgTopColor = MyChartConfig(3)
SkyBgFootColor = MyChartConfig(4)
Font1Size = MyChartConfig(5)
Font2Size = MyChartConfig(6)
Font1Color = MyChartConfig(7)
Font2Color = MyChartConfig(8)
rectangleFillColor = MyChartConfig(9)
rectangleDrawColor = MyChartConfig(10)
rectangleWidth = MyChartConfig(11)
rectangleSpace = MyChartConfig(12)
'Response.Write(Sconfig)
'SkyBgTopH = 600
'SkyBgFootH = 60
'SkyBgTopColor = "orange"
'SkyBgFootColor = "#EEEEEE"
'Font1Size = 8
'Font2Size = 8
'Font1Color = "#000000"
'Font2Color = "#000000"
'rectangleFillColor = "#FF0000"
'rectangleDrawColor = "#FFFFFF"
'rectangleWidth = 20
'rectangleSpace = 10
'###############################################################
rectangleOther = 10
rectangleAll = rectangleWidth + rectangleSpace
'#################################################
Dim objBitmap As Bitmap
Dim objGraphics As Graphics
Dim decmaxData As Decimal
Dim strBackColor As String
Dim strForeColor As String
Dim ObjBackColor, ObjFont1, ObjFont2, ObjJP, ObjJPFill As Color
Dim decMultiplier As Decimal
Dim intWidth As Integer
Dim intHeight As Integer
Dim arrChartData() As String
Dim arrName() As String
Dim intDataIndex As Integer
Dim intDataItem As Integer
Dim objBrush As SolidBrush
Dim MoveN As Integer
MoveN = SkyBgFootH
'#################################################
decmaxData = 1
'############################################################################
arrChartData = Session("MyChartNumber")
arrName = Session("MyChartString")
Dim I As Integer
Dim Maxd As Single = 1
Dim ZIndex As Integer
Try
ZIndex = UBound(arrChartData)
Catch ex As Exception
ZIndex = -1
End Try
For I = 0 To ZIndex
Maxd = CSng(arrChartData(I))
If Maxd > decmaxData Then
decmaxData = Maxd
End If
Next
'############################################################################
decMultiplier = SkyBgTopH / decmaxData
'#############################################################################
ObjBackColor = ColorTranslator.FromHtml(SkyBgTopColor)
ObjJPFill = ColorTranslator.FromHtml(rectangleFillColor)
ObjFont1 = ColorTranslator.FromHtml(Font1Color)
ObjFont2 = ColorTranslator.FromHtml(Font2Color)
ObjJP = ColorTranslator.FromHtml(rectangleDrawColor)
objBrush = New SolidBrush(ObjJPFill)
'#############################################################################
'arrChartData = SkyReplaceBad(request.QueryString.GetValues("d")
'arrName = SkyReplaceBad(request.QueryString.GetValues("n")
'#############################################################################
intWidth = (arrChartData.Length * rectangleAll) + rectangleOther
intHeight = (decmaxData * decMultiplier) + rectangleSpace + MoveN
'#############################################################################
Dim drawFormat As StringFormat = DrowDirectionVerticalFont() '文字格式
If ZIndex >= 0 Then
objBitmap = New Bitmap(intWidth, intHeight)
objGraphics = Graphics.FromImage(objBitmap)
objGraphics.Clear(ObjBackColor)
'底部背景颜色
objGraphics.FillRectangle(New SolidBrush(ColorTranslator.FromHtml(SkyBgFootColor)), 0, intHeight - MoveN, intWidth, MoveN)
Dim objFont As New Font("Arial", Font1Size)
Dim Brush1 As New SolidBrush(ObjFont1)
Dim Brush2 As New SolidBrush(ObjFont2)
Dim Pen As New Pen(ObjJP, 1)
For intDataIndex = 0 To arrChartData.Length - 1
intDataItem = arrChartData(intDataIndex) * decMultiplier
'################################################################################################################################
'矩形颜色和边框
objGraphics.FillRectangle(objBrush, (intDataIndex * rectangleAll) + rectangleSpace, intHeight - intDataItem - MoveN, rectangleWidth, intDataItem)
objGraphics.DrawRectangle(Pen, (intDataIndex * rectangleAll) + rectangleSpace, intHeight - intDataItem - MoveN, rectangleWidth, intDataItem)
'################################################################################################################################
'objGraphics.FillRectangle(objBrush, (intDataIndex * 25) + 5, intHeight - intDataItem, 20, intHeight)
'################################################################################################################################
'##开始绘制文字颜色1和文字颜色2
objGraphics.DrawString(arrChartData(intDataIndex), objFont, Brush1, (intDataIndex * rectangleAll) + rectangleSpace, intHeight - intDataItem - MoveN)
objGraphics.DrawString(arrName(intDataIndex), objFont, Brush2, (intDataIndex * rectangleAll) + rectangleSpace, intHeight - MoveN + rectangleOther, drawFormat)
'################################################################################################################################
'objGraphics.DrawString(Session("A"), New Font("宋体", 9, FontStyle.Bold), New SolidBrush(Color.White), New PointF(160, 50))
Next
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
Else
objBitmap = New Bitmap(200, 30)
objGraphics = Graphics.FromImage(objBitmap)
objGraphics.Clear(Color.White)
objGraphics.DrawString("对不起,系统暂时无数据!", New Font("宋体", 9, FontStyle.Regular), New SolidBrush(Color.Black), New PointF(10, 10))
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
End If
End Function
End Class
Public Function ShowIMGInfo(ByVal MyChartConfig As String, ByVal MyChartString() As String, ByVal MyChartNumber() As String)
Session("MyChartNumber") = MyChartNumber
Session("MyChartString") = MyChartString
Session("MyChartConfig") = "File!600!60!orange!#EEEEEE!8!8!#000000!#000000!#FF0000!#FFFFFF!20!10"
Session("MyChartConfig") = MyChartConfig
imgChartImage.ImageUrl = "GDIImageChart.aspx?t=1&0451sky=" + Now.ToString.Replace(":", "").Replace("-", "").Replace(" ", "")
End Function
Public Function ShowIMGInfoM(ByVal MyChartConfig As String, ByVal MyChartString() As String, ByVal MyChartNumber() As String, ByVal MyChartSNum As Integer, ByVal MyChartSNumName As String)
Session("MyChartNumber") = MyChartNumber
Session("MyChartString") = MyChartString
Session("MyChartConfig") = "File!600!60!orange!#EEEEEE!8!8!#000000!#000000!#FF0000!#FFFFFF!20!10"
Session("MyChartConfig") = MyChartConfig
Session("MyChartSNum") = MyChartSNum
Session("MyChartSNumName") = MyChartSNumName
imgChartImage.ImageUrl = "GDIImageChart.aspx?t=2&0451sky=" + Now.ToString.Replace(":", "").Replace("-", "").Replace(" ", "")
End Function