Home All Groups Group Topic Archive Search About

Drawing images in asp.net

Author
15 May 2009 2:40 PM
Cory J. Laidlaw, Beyond01.com
Hi,

I am trying to draw a simple graph using ASP.NET without using a 3rd party
control.

I only want to draw a simple horizontal bar chart, save it to a JPG and
render it on my web form.

There are several articles using GDI(?) for this, but they all relate to
windows forms, not web forms. My attempts to convert it to using on a web
form aren't working too well.

Does anyone have an example of how to do this from ASP.NET? Help greatly
appreciated! Thanks!!

Cory

P.s. Here is my first shot at a prototype. as you can see, nothing happens...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

        Dim bmp As New Bitmap(200, 200)
        Dim fg As Graphics = Graphics.FromImage(bmp)

        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)

        bmp.Save("Test.bmp")

        Image1.ImageUrl = "test.bmp"


    End Sub

Author
15 May 2009 3:25 PM
Patrice
What if you add fg.Dispose before saving the file ? (also I would
explicitely mention the format, If I remember the default is png so here you
have a png named with a bmp extension)

--
Patrice


"Cory J. Laidlaw, Beyond01.com"
<CoryJLaidlawBeyond01***@discussions.microsoft.com> a écrit dans le message
de groupe de discussion :
3D1F4D40-229B-43DF-A560-23D60BDD6***@microsoft.com...
Show quoteHide quote
> Hi,
>
> I am trying to draw a simple graph using ASP.NET without using a 3rd party
> control.
>
> I only want to draw a simple horizontal bar chart, save it to a JPG and
> render it on my web form.
>
> There are several articles using GDI(?) for this, but they all relate to
> windows forms, not web forms. My attempts to convert it to using on a web
> form aren't working too well.
>
> Does anyone have an example of how to do this from ASP.NET? Help greatly
> appreciated! Thanks!!
>
> Cory
>
> P.s. Here is my first shot at a prototype. as you can see, nothing
> happens...
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Handles Me.Load
>
>        Dim bmp As New Bitmap(200, 200)
>        Dim fg As Graphics = Graphics.FromImage(bmp)
>
>        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)
>
>        bmp.Save("Test.bmp")
>
>        Image1.ImageUrl = "test.bmp"
>
>
>    End Sub
Author
15 May 2009 3:43 PM
Patrice
I gave this a try. It would give finally :

        Dim bmp As New Bitmap(200, 200)
        Dim fg As Graphics = Graphics.FromImage(bmp)
        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)
        fg.Dispose()
        bmp.Save(Server.MapPath("~/") & "Test.bmp", Imaging.ImageFormat.Bmp)
        bmp.Dispose()
        Image1.ImageUrl = "test.bmp"

I added also the use of a full path to save the file (assuming the default
page is at your web application root) (as the current dir doesn't have much
meaning for IIS and is likely system32).

--
Patrice

"Patrice" <http://www.chez.com/scribe/> a écrit dans le message de groupe de
discussion : 567578E6-98F1-43FC-B991-9CF2EC480***@microsoft.com...
Show quoteHide quote
> What if you add fg.Dispose before saving the file ? (also I would
> explicitely mention the format, If I remember the default is png so here
> you have a png named with a bmp extension)
>
> --
> Patrice
>
>
> "Cory J. Laidlaw, Beyond01.com"
> <CoryJLaidlawBeyond01***@discussions.microsoft.com> a écrit dans le
> message de groupe de discussion :
> 3D1F4D40-229B-43DF-A560-23D60BDD6***@microsoft.com...
>> Hi,
>>
>> I am trying to draw a simple graph using ASP.NET without using a 3rd
>> party
>> control.
>>
>> I only want to draw a simple horizontal bar chart, save it to a JPG and
>> render it on my web form.
>>
>> There are several articles using GDI(?) for this, but they all relate to
>> windows forms, not web forms. My attempts to convert it to using on a web
>> form aren't working too well.
>>
>> Does anyone have an example of how to do this from ASP.NET? Help greatly
>> appreciated! Thanks!!
>>
>> Cory
>>
>> P.s. Here is my first shot at a prototype. as you can see, nothing
>> happens...
>>
>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
>> System.EventArgs)
>> Handles Me.Load
>>
>>        Dim bmp As New Bitmap(200, 200)
>>        Dim fg As Graphics = Graphics.FromImage(bmp)
>>
>>        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)
>>
>>        bmp.Save("Test.bmp")
>>
>>        Image1.ImageUrl = "test.bmp"
>>
>>
>>    End Sub
>
>
Author
15 May 2009 9:32 PM
Cory J. Laidlaw, Beyond01.com
Very good. I'll check these soltuions out.

Thanks both to you Patrice and Cor for your help! :)

Cory

Show quoteHide quote
"Patrice" wrote:

> What if you add fg.Dispose before saving the file ? (also I would
> explicitely mention the format, If I remember the default is png so here you
> have a png named with a bmp extension)
>
> --
> Patrice
>
>
> "Cory J. Laidlaw, Beyond01.com"
> <CoryJLaidlawBeyond01***@discussions.microsoft.com> a crit dans le message
> de groupe de discussion :
> 3D1F4D40-229B-43DF-A560-23D60BDD6***@microsoft.com...
> > Hi,
> >
> > I am trying to draw a simple graph using ASP.NET without using a 3rd party
> > control.
> >
> > I only want to draw a simple horizontal bar chart, save it to a JPG and
> > render it on my web form.
> >
> > There are several articles using GDI(?) for this, but they all relate to
> > windows forms, not web forms. My attempts to convert it to using on a web
> > form aren't working too well.
> >
> > Does anyone have an example of how to do this from ASP.NET? Help greatly
> > appreciated! Thanks!!
> >
> > Cory
> >
> > P.s. Here is my first shot at a prototype. as you can see, nothing
> > happens...
> >
> > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> > System.EventArgs)
> > Handles Me.Load
> >
> >        Dim bmp As New Bitmap(200, 200)
> >        Dim fg As Graphics = Graphics.FromImage(bmp)
> >
> >        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)
> >
> >        bmp.Save("Test.bmp")
> >
> >        Image1.ImageUrl = "test.bmp"
> >
> >
> >    End Sub
>
>
Author
15 May 2009 3:42 PM
Cor Ligthert[MVP]
Cory,

Creating yourself images on ASPNet is not as simple as in Windows Forms.

Every image in HTML has its own url, you can see that sometimes in your
browser.

This is a method how you can do it.

http://www.vb-tips.com/ServerClock.aspx

I think this is an outdated example I made some years ago.

If I would make this example new, then I certainly would try to do it with
Silverlight

Cor




"Cory J. Laidlaw, Beyond01.com"
<CoryJLaidlawBeyond01***@discussions.microsoft.com> wrote in message
Show quoteHide quote
news:3D1F4D40-229B-43DF-A560-23D60BDD6032@microsoft.com...
> Hi,
>
> I am trying to draw a simple graph using ASP.NET without using a 3rd party
> control.
>
> I only want to draw a simple horizontal bar chart, save it to a JPG and
> render it on my web form.
>
> There are several articles using GDI(?) for this, but they all relate to
> windows forms, not web forms. My attempts to convert it to using on a web
> form aren't working too well.
>
> Does anyone have an example of how to do this from ASP.NET? Help greatly
> appreciated! Thanks!!
>
> Cory
>
> P.s. Here is my first shot at a prototype. as you can see, nothing
> happens...
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Handles Me.Load
>
>        Dim bmp As New Bitmap(200, 200)
>        Dim fg As Graphics = Graphics.FromImage(bmp)
>
>        fg.FillRectangle(Brushes.Aqua, 50, 50, 100, 100)
>
>        bmp.Save("Test.bmp")
>
>        Image1.ImageUrl = "test.bmp"
>
>
>    End Sub
Author
16 May 2009 1:47 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> schrieb:
> Creating yourself images on ASPNet is not as simple as in Windows Forms.
>
> Every image in HTML has its own url, you can see that sometimes in your
> browser.
>
> This is a method how you can do it.
>
> http://www.vb-tips.com/ServerClock.aspx
>
> I think this is an outdated example I made some years ago.
>
> If I would make this example new, then I certainly would try to do it with
> Silverlight

Well, the sample could be optimized easily by changing the image format from
bitmap to GIF or PNG, for example.  This would reduce the amount of data
which has to be transferred.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>