I have a program to draw bar/pie chart based on the data i hard coded in it. However, my image comes with "BLACK" background color. I don't know how to fix this. The code snippet is below:
chartGenerator.vb class:
Expand|Select|Wrap|Line Numbers
- Imports System.Drawing.Imaging
- Public Class chartGenerator
- Private myImage As Bitmap
- Private g As Graphics
- Private p() As Integer = {1000000, 600000, 2500000, 80000}
- Private towns() As String = {"A", "B", "C", "D"}
- Private myBrushes(4) As Brush
- Private _width, _height As Integer
- Private x_start, y_start As Decimal
- Sub New(ByVal width As Integer, ByVal height As Integer)
- _width = width
- _height = height
- x_start = _width / 10
- y_start = _height / 10
- ' The Bitmap is 300 pixels wide and 200 pixels high.
- myImage = New Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
- myImage.MakeTransparent(Color.White)
- ' Get the graphics context for the bitmap.
- g = Graphics.FromImage(myImage)
- ' Create the brushes for drawing
- myBrushes(0) = New SolidBrush(Color.Red)
- myBrushes(1) = New SolidBrush(Color.Blue)
- myBrushes(2) = New SolidBrush(Color.Yellow)
- myBrushes(3) = New SolidBrush(Color.Green)
- End Sub
- Function makeBarChart() As Bitmap
- ' Variables declaration
- Dim i As Integer
- Dim xInterval As Integer = _width / 5
- Dim width As Integer = _width * 9 / 50
- Dim height As Integer
- Dim blackBrush As New SolidBrush(Color.Black)
- For i = 0 To p.Length - 1
- height = (p(i) \ 10000) ' divide by 10000 to adjust barchart to height of Bitmap
- ' Draws the bar chart using specific colours
- g.FillRectangle(myBrushes(i), xInterval * i + 50, 280 - height, width, height)
- ' label the barcharts
- g.DrawString(towns(i), New Font("Verdana", 12, FontStyle.Bold), Brushes.Black, xInterval * i + 50 + (width / 3), 280 - height - 25)
- ' Draw the scale
- g.DrawString(height, New Font("Verdana", 8, FontStyle.Bold), Brushes.Black, 0, 280 - height)
- ' Draw the axes
- g.DrawLine(Pens.Brown, 40, 10, 40, 290) ' y-axis
- g.DrawLine(Pens.Brown, 20, 280, 490, 280) ' x-axis
- Next
- Return myImage
- 'myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
- 'myImage.Dispose()
- End Function
- Function makePieChart() As Bitmap
- ' Variables declaration
- Dim i As Integer
- Dim total As Integer
- Dim percentage As Double
- Dim angleSoFar As Double = 0.0
- ' Caculates the total
- For i = 0 To p.Length - 1
- total += p(i)
- Next
- ' Draws the pie chart
- For i = 0 To p.Length - 1
- percentage = p(i) / total * 360
- g.FillPie(myBrushes(i), 25, 25, 250, 250, CInt(angleSoFar), CInt(percentage))
- angleSoFar += percentage
- ' Draws the lengend
- g.FillRectangle(myBrushes(i), 350, 25 + (i * 50), 25, 25)
- ' Label the towns
- g.DrawString("Town " & towns(i), New Font("Verdana", 8, FontStyle.Bold), Brushes.Brown, 390, 25 + (i * 50) + 10)
- Next
- Return myImage
- 'myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
- 'myImage.Dispose()
- End Function
- End Class
image.aspx.vb
Expand|Select|Wrap|Line Numbers
- Imports System.Drawing.Imaging
- Public Class image
- Inherits System.Web.UI.Page
- Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'Put user code to initialize the page here
- Dim bitmap As Bitmap = CType(Session("image"), Bitmap)
- bitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
- bitmap.Dispose()
- End Sub
- End Class
testChart.aspx.vb
Expand|Select|Wrap|Line Numbers
- Public Class testChart
- Inherits System.Web.UI.Page
- Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'Put user code to initialize the page here
- End Sub
- Private chart As New chartGenerator(500, 300)
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim bitmap As Bitmap = chart.makeBarChart()
- Session("image") = bitmap
- Image1.ImageUrl = "image.aspx"
- End Sub
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- Dim bitmap As Bitmap = chart.makePieChart
- Session("image") = bitmap
- Image1.ImageUrl = "image.aspx"
- End Sub
- End Class
Anyone got idea of what happen out there?