472,993 Members | 3,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

Bitmap draw image with background color "Black"?

Hi, guys.
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
  1. Imports System.Drawing.Imaging
  2.  
  3. Public Class chartGenerator
  4.  
  5.     Private myImage As Bitmap
  6.     Private g As Graphics
  7.     Private p() As Integer = {1000000, 600000, 2500000, 80000}
  8.     Private towns() As String = {"A", "B", "C", "D"}
  9.  
  10.     Private myBrushes(4) As Brush
  11.     Private _width, _height As Integer
  12.     Private x_start, y_start As Decimal
  13.  
  14.  
  15.     Sub New(ByVal width As Integer, ByVal height As Integer)
  16.  
  17.         _width = width
  18.         _height = height
  19.         x_start = _width / 10
  20.         y_start = _height / 10
  21.  
  22.         ' The Bitmap is 300 pixels wide and 200 pixels high.
  23.         myImage = New Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
  24.  
  25.         myImage.MakeTransparent(Color.White)
  26.  
  27.         ' Get the graphics context for the bitmap.
  28.         g = Graphics.FromImage(myImage)
  29.  
  30.         '   Create the brushes for drawing
  31.         myBrushes(0) = New SolidBrush(Color.Red)
  32.         myBrushes(1) = New SolidBrush(Color.Blue)
  33.         myBrushes(2) = New SolidBrush(Color.Yellow)
  34.  
  35.         myBrushes(3) = New SolidBrush(Color.Green)
  36.     End Sub
  37.  
  38.     Function makeBarChart() As Bitmap
  39.         '   Variables declaration
  40.         Dim i As Integer
  41.         Dim xInterval As Integer = _width / 5
  42.         Dim width As Integer = _width * 9 / 50
  43.         Dim height As Integer
  44.         Dim blackBrush As New SolidBrush(Color.Black)
  45.  
  46.         For i = 0 To p.Length - 1
  47.             height = (p(i) \ 10000) '   divide by 10000 to adjust barchart to height of Bitmap
  48.  
  49.             '   Draws the bar chart using specific colours
  50.             g.FillRectangle(myBrushes(i), xInterval * i + 50, 280 - height, width, height)
  51.  
  52.             '   label the barcharts
  53.             g.DrawString(towns(i), New Font("Verdana", 12, FontStyle.Bold), Brushes.Black, xInterval * i + 50 + (width / 3), 280 - height - 25)
  54.  
  55.             '   Draw the scale
  56.             g.DrawString(height, New Font("Verdana", 8, FontStyle.Bold), Brushes.Black, 0, 280 - height)
  57.  
  58.             '   Draw the axes
  59.             g.DrawLine(Pens.Brown, 40, 10, 40, 290)         '   y-axis
  60.             g.DrawLine(Pens.Brown, 20, 280, 490, 280)       '   x-axis
  61.         Next
  62.  
  63.         Return myImage
  64.         'myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  65.         'myImage.Dispose()
  66.     End Function
  67.  
  68.     Function makePieChart() As Bitmap
  69.         '   Variables declaration
  70.         Dim i As Integer
  71.         Dim total As Integer
  72.         Dim percentage As Double
  73.         Dim angleSoFar As Double = 0.0
  74.  
  75.         '   Caculates the total
  76.         For i = 0 To p.Length - 1
  77.             total += p(i)
  78.         Next
  79.  
  80.         '   Draws the pie chart
  81.         For i = 0 To p.Length - 1
  82.             percentage = p(i) / total * 360
  83.  
  84.             g.FillPie(myBrushes(i), 25, 25, 250, 250, CInt(angleSoFar), CInt(percentage))
  85.  
  86.             angleSoFar += percentage
  87.  
  88.             '   Draws the lengend
  89.             g.FillRectangle(myBrushes(i), 350, 25 + (i * 50), 25, 25)
  90.  
  91.             '   Label the towns
  92.             g.DrawString("Town " & towns(i), New Font("Verdana", 8, FontStyle.Bold), Brushes.Brown, 390, 25 + (i * 50) + 10)
  93.         Next
  94.  
  95.         Return myImage
  96.         'myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  97.         'myImage.Dispose()
  98.     End Function
  99. End Class
  100.  
  101.  

image.aspx.vb
Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing.Imaging
  2. Public Class image
  3.     Inherits System.Web.UI.Page
  4.  
  5.  
  6.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.         'Put user code to initialize the page here
  8.         Dim bitmap As Bitmap = CType(Session("image"), Bitmap)
  9.         bitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
  10.         bitmap.Dispose()
  11.  
  12.     End Sub
  13.  
  14. End Class
  15.  
  16.  

testChart.aspx.vb
Expand|Select|Wrap|Line Numbers
  1. Public Class testChart
  2.     Inherits System.Web.UI.Page
  3.  
  4.  
  5.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.         'Put user code to initialize the page here
  7.  
  8.  
  9.  
  10.     End Sub
  11.  
  12.     Private chart As New chartGenerator(500, 300)
  13.  
  14.  
  15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  16.  
  17.         Dim bitmap As Bitmap = chart.makeBarChart()
  18.         Session("image") = bitmap
  19.         Image1.ImageUrl = "image.aspx"
  20.  
  21.  
  22.     End Sub
  23.  
  24.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  25.         Dim bitmap As Bitmap = chart.makePieChart
  26.         Session("image") = bitmap
  27.         Image1.ImageUrl = "image.aspx"
  28.  
  29.     End Sub
  30. End Class
  31.  
  32.  

Anyone got idea of what happen out there?
Feb 7 '07 #1
0 2768

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John | last post by:
How can I draw any thing within "for" loop? I tried the below but it doesn't draw anything. What should I do? **** Code **** Graphics g = this.CreateGraphics(); Bitmap bm=new Bitmap(1,1);...
0
by: Matthew Belk | last post by:
I am trying to print some 2x6 labels on a SATO CL408e thermal label printer. The 2x6 labels are arranged in "landscape" mode with 2 labels per "sheet." When I attempt to print "x" copies of a 1...
1
by: John | last post by:
I'm trying to use the DrawText() method to draw some very long string text on the Panel with AutoScroll enabled. However, for some unknown reasons, I could not trigger the ScrollBar to show up. ...
2
by: Bernt Fischer | last post by:
Hello I want to render a datagrid with transparent borders (cellspacing = 1) so the underlying background shines through. Unfortunately, ASPNET renders the <table> tag with a 'rules="all"...
3
by: Boki | last post by:
Dear All, I think you all knew my problem. I don't know how to draw line @@ in VB.NET (2005 beta) , very sad. Coudl you please help me? Best regards, Boki.
2
by: Simon Verona | last post by:
If I have a combobox set enabled=false then by default it will have dark grey text on a grey background. I want it to show as blue on white. I'm trying code such as : combobox.enabled=false...
3
by: Peter Proost | last post by:
Hi group, I've got this bit of code (see below) which draws a basketball field in a picturebox (width:198, height:368) but now I was wondering what would be the easiest way to check inside the...
2
by: spifster | last post by:
Hello all, I am building a collapsable tree using Javascript with DOM in IE. In order to make collapsed cells disappear I have been hiding the text. The cells collapse but still leave borders...
4
by: rebeccatre | last post by:
please help me with this, <style> #a, option.message {background-color: green; color: white;} #b, option.message {background-color: yellow; color: black;} </style> <select id="thisselect">...
2
luke14free
by: luke14free | last post by:
Hello guys, I've been searching for a method to hide the console (as c/c++ user would say) under windows, but i found nothing really helpful. What I found is: 1) I need to rename file as pyw. But...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.