473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 2829

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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.