|
What diagram do you want to generate?Here is an example, hope will help you.
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Insight_cs.WebCharts
{
public class PieChart
{
public PieChart()
{
}
public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
{
const int SIDE_LENGTH = 400;
const int PIE_DIAMETER = 200;
DataTable dt = chartData.Tables[0];
float sumData = 0;
foreach(DataRow dr in dt.Rows)
{
sumData += Convert.ToSingle(dr[1]);
}
Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);
g.ScaleTransform((Convert.ToSingle(width))/SIDE_LENGTH,(Convert.ToSingle(height))/SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0,SIDE_LENGTH-1,SIDE_LENGTH-1);
g.DrawString(title,new Font("Tahoma",24),Brushes.Black,new PointF(5,5));
g.DrawString(subTitle,new Font("Tahoma",14),Brushes.Black,new PointF(7,35));
float curAngle = 0;
float totalAngle = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
curAngle = Convert.ToSingle(dt.Rows[i][1]) / sumData * 360;
g.FillPie(new SolidBrush(ChartUtil.GetChartItemColor(i)),100,65,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle);
g.DrawPie(Pens.Black,100,65,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle);
totalAngle += curAngle;
}
g.DrawRectangle(Pens.Black,200,300,199,99);
g.DrawString("Legend",new Font("Tahoma",12,FontStyle.Bold),Brushes.Black,new PointF(200,300));
PointF boxOrigin = new PointF(210,330);
PointF textOrigin = new PointF(235,326);
float percent = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
percent = Convert.ToSingle(dt.Rows[i][1]) / sumData * 100;
g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString() + " (" + percent.ToString("0") + "%)",new Font("Tahoma",10),Brushes.Black,textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
bm.Save(target, ImageFormat.Gif);
bm.Dispose();
g.Dispose();
}
}
public class BarChart
{
public BarChart()
{
}
public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
{
const int SIDE_LENGTH = 400;
const int CHART_TOP = 75;
const int CHART_HEIGHT = 200;
const int CHART_LEFT = 50;
const int CHART_WIDTH = 300;
DataTable dt = chartData.Tables[0];
float highPoint = 0;
foreach(DataRow dr in dt.Rows)
{
if(highPoint<Convert.ToSingle(dr[1]))
{
highPoint = Convert.ToSingle(dr[1]);
}
}
Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);
g.ScaleTransform((Convert.ToSingle(width))/SIDE_LENGTH,(Convert.ToSingle(height))/SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0,SIDE_LENGTH-1,SIDE_LENGTH-1);
g.DrawString(title,new Font("Tahoma",24),Brushes.Black,new PointF(5,5));
g.DrawString(subTitle,new Font("Tahoma",14),Brushes.Black,new PointF(7,35));
float barWidth = CHART_WIDTH / (dt.Rows.Count * 2);
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);
float barHeight = dt.Rows.Count;
for(int i=0;i<dt.Rows.Count;i++)
{
barHeight = Convert.ToSingle(dt.Rows[i][1]) * 200 / highPoint;
barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,barHeight);
barOrigin.X = barOrigin.X + (barWidth * 2);
}
g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT));
g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT),new Point(CHART_LEFT + CHART_WIDTH,CHART_TOP + CHART_HEIGHT));
g.DrawRectangle(new Pen(Color.Black,1),200,300,199,99);
g.DrawString("Legend",new Font("Tahoma",12,FontStyle.Bold),Brushes.Black,new PointF(200,300));
PointF boxOrigin = new PointF(210,330);
PointF textOrigin = new PointF(235,326);
for(int i=0;i<dt.Rows.Count;i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString(),new Font("Tahoma",10),Brushes.Black,textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
bm.Save(target, ImageFormat.Gif);
bm.Dispose();
g.Dispose();
}
}
public class ChartUtil
{
public ChartUtil()
{
}
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.Purple;
break;
default:
selectedColor = Color.Green;
break;
}
return selectedColor;
}
}
}
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 21:58pm.
|
|
|
|
|
hello every body
i m very glad that the team of code project help me solving my problem i wish in future it will be continue.....
|
|
|
|
|
can anyone plz suggest me a code to record and play a file of mp4 or avi format......
|
|
|
|
|
If you Google for c# play avi or c# play mp4 you will get loads and loads of hits. Some have code, some only descriptions of how to do it.
Have a go yourself.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi, I'm currently using Visual c# 2008 express edition. I'm trying to save an image over a file with the same file name. When it attempts to do this is bombs out because it says either it's a generic GDI+ error or the process is already in use. This makes perfect sense why it would throw those errors, but how can I overwrite the file so that it doesn't give me this problem. I've tried Opening a file stream around it then closeing it. useing the bmpPicture.Dispose(), and GC.Collect(), none of those work.Is there something else I could try? or perhaps something I'm doing wrong?
here is a snippet of my code:
<br />
private void SaveFile()<br />
{<br />
bmpPicture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);<br />
gfxScreenShot = Graphics.FromImage(bmpPicture);<br />
gfxScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y + 25, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);<br />
bmpPicture.Save(FilePath, ImageFormat.Bmp);
}<br />
|
|
|
|
|
Hi,
a likely cause for bmpPicture.Save(FilePath, ImageFormat.Bmp); to throw an exception is the file is locked "by another process", which typically means your own process (or maybe some other) has the file open. Typically you would have loaded it either with Image.FromFile() or PictureBox.Load()
Images loaded that way keep their file open for as long as they exist. There are three remedies:
1. quick and dirty
get the picture, make sure you don't need it anymore, call dispose:
Image img=PictureBox.Image;
PictureBox.Image=null;
img.Dispose();
img=null;
2. quick and dirty
when you initially get the image, make a copy and throw away the original:
Bitmap bm2=null;
{
Bitmap bm1=Image.FromFile("image.jpeg");
bm2=new Bitmap(bm1);
bm1.Dispose();
}
3. clean and easy
don't use Image.FromFile nor PictureBox.Load; instead use Image.FromStream
Bitmap bm=Image.FromStream(File.OpenRead("image.jpeg"));
[EDIT] Bug fixed in my other reply ]/EDIT]
modified on Tuesday, September 8, 2009 1:53 PM
|
|
|
|
|
vandel212 wrote: GC.Collect()
Is that really necessary? It usually isn't..
|
|
|
|
|
Sorry, the third option ("clean and easy") wasn't entirely correct, as the stream does not get closed/disposed of. Here is a better way:
Bitmap bm=null;
using (FileStream stream=File.OpenRead("image.jpeg")) {
bm=Image.FromStream(stream);
}
So in the end, there isn't a single-line solution.
|
|
|
|
|
My goal is to preform various image processing tasks on an image taken by a webcam...
After googling, and trying different examples... I failed to find a simple solution. Example code is either overly complicated or it only supports WIA webcams only.
I like how "Motion Detection Algorithms[^]" works (for stills) however the code is overly complicated for the task I want to preform with a mind blowing 48 .cs files...
I tried trimming down but ended up with 26 files of code I have no clue on...
All I want to do is take a still or two every five minutes... Any suggestions on this simple sounding yet strangely complicated task?
|
|
|
|
|
My problem is simply that I am trying to make a simple command line RSA encryption program and was trying out the number 123^17 and don't seem to be able to store the value anywhere because of its size. How can I work with numbers that are bigger than 10^20?
|
|
|
|
|
integers or reals?
search CodeProject (or google) for either BigInt or BigNumber, and you will find several classes that take care of it, not all at the same quality level though.
This[^] one you may like, no guarantees.
C# 4.0 has a BigInt class built-in, I haven't had a look yet.
|
|
|
|
|
Try the System.Decimal type which can hold values from ±1.0 × 10^−28 to ±7.9 × 10^28 with precision of up to 28-29 significant digits.
|
|
|
|
|
I've found that fixed-length number structs are usually better for relatively short numbers. Looks like 128bit should do in your case - and that's easy to make (just put 2 ulongs together in a struct, all operations except division and modulo are easy to extend)
|
|
|
|
|
I did try this ages ago but can't remember much about it. J# has a big integer class - see here[^], you can use this from C#.
There is also IntX[^] which I've never used but seen recommended.
|
|
|
|
|
Hi,
I am using VS 2008 to develop an application. In this application
i have List<> of Contacts. This list may contain thousands of contact's.
I have
class Contact
{
......
.........
};
i want to give a signal to all instances of this class that a changed had happen. How can i do this.
thanks
Nitheesh
Jose Jo Martin
http://www.simpletools.co.in
|
|
|
|
|
|
how can i compile file from DOS and make if an exe file?
|
|
|
|
|
|
|
alternative for this:
MemoryStream ms = new MemoryStream();
ms.Write("one exe file");
Process.Start(ms);
)
i have an encoded exe, i want to decode it on memorystream and then run it, so nobody can see original exe file! so i can not use a temp file on HDD!
other way?
modified on Tuesday, September 8, 2009 10:06 AM
|
|
|
|
|
Write it to a temporary file?
|
|
|
|
|
This may sound like a stupid question, but why would you want to do that? If you have the process name, path and parameters why put it to a stream fisrt? If the stream contains more than the name, path and parameters, then what do you expect to happen with the excess data?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
There is no other way. You have to write it to a temporary file. Besides, if your app requires that kind of security, you're still not going to stop the right person from grabbing it straight out of memory.
|
|
|
|
|
you might create a (temporary) memory-resident file system, create a file in there, and execute it. Google + some stamina required.
klik[^]
modified on Tuesday, September 8, 2009 1:04 PM
|
|
|
|
|
Luc Pattyn wrote: you might create a (temporary) memory-resident file system, create a file in there, and execute it. Google + some stamina required.
I was going to suggest writing his own Loader in place of Windows version, but I thought it would have been a bit too overboard.
|
|
|
|
|