Home All Groups Group Topic Archive Search About

want to create custom control using graphics

Author
29 Mar 2006 5:24 AM
rushikesh.joshi
Hi All,

I want to create a webcontrol which will generate a bar (bar chart). I
have done some graphics code in my ASPX page and it's working fine, but
how do i create a Custom Control or User Control for same (custom
control is prefarable).

Below is my code of ASPX page which is working fine....
Here i have used Response, to generate the bar but how do i use it in
the webcontrols...

Thanks & Regards,
Rushikesh

CODE:

<%@ Page Language="C#" debug="true"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)
{
    Bitmap objBitmap = new Bitmap(200,200);

    Graphics objGraphic = Graphics.FromImage(objBitmap);

    SolidBrush redBrush   = new SolidBrush(Color.Red);
    SolidBrush blueBrush  = new SolidBrush(Color.Blue);
    SolidBrush greenBrush = new SolidBrush(Color.Green);
    SolidBrush whiteBrush = new SolidBrush(Color.White);
    Pen blackPen = new Pen(Color.Black, 2);

    objGraphic.FillRectangle(whiteBrush, 0, 0, 200, 200);

    objGraphic.DrawLine(blackPen, new Point(0,195), new Point(195,195));
    objGraphic.DrawLine(blackPen, new Point(5,5),   new Point(5,200));

    Single sngHighestValue, sngHeight1, sngHeight2, sngHeight3;
    Single sngValue1, sngValue2, sngValue3;

    sngValue1 = Convert.ToSingle(Request["v1"]);
    sngValue2 = Convert.ToSingle(Request["v2"]);
    sngValue3 = Convert.ToSingle(Request["v3"]);

    if (sngValue1 > sngValue2)
        sngHighestValue = sngValue1;
    else
        sngHighestValue = sngValue2;

    if (sngValue3 > sngHighestValue)
        sngHighestValue = sngValue3;

    if (sngHighestValue == 0)
        sngHighestValue = 1;

    sngHeight1 = (sngValue1 / sngHighestValue) * 190;
    sngHeight2 = (sngValue2 / sngHighestValue) * 190;
    sngHeight3 = (sngValue3 / sngHighestValue) * 190;

    objGraphic.FillRectangle(redBrush,    10,  194-sngHeight1, 50,
sngHeight1);
    objGraphic.FillRectangle(blueBrush,   70,  194-sngHeight2, 50,
sngHeight2);
    objGraphic.FillRectangle(greenBrush,  130, 194-sngHeight3, 50,
sngHeight3);

        Response.ContentType = "image/gif";

        objBitmap.Save (Response.OutputStream, ImageFormat.Gif);

}

</script>



Thanks
Rushikesh

Author
29 Mar 2006 8:56 AM
Ward Bekker
Hi Rushikesh,

You could also put an image tag on an ASPX and use as source the path to
the aspx that generates the image.


--
Ward Bekker
"Asp.Net Discussions for the Professional Developer"
http://www.dotnettaxi.com

"Free .Net 2.0 C# to/from VB.Net Code Converter"
http://www.dotnettaxi.com/Tools/Converter.aspx
Author
29 Mar 2006 9:01 AM
rushikesh.joshi
Hi Ward,

Thankyou for your quick response,

Yes you are right, but i want to generate this image based on some
parameter. I want to pass the parameters in Array form and based on
that i want to create the image and because of that i want to create a
control.

Is there anyother way around?

thanks & regards
rushikesh