How to use the Graphics class in web forms?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In order to do some UI animation or drawings can I use a Graphics object in a
WEB Forms project (in VisualStudio.NET)
 
Renato said:
In order to do some UI animation or drawings can I use a Graphics
object in a WEB Forms project (in VisualStudio.NET)

Yes, BUT:
you can't just add an image to the html output-stream, you can only add
<img> tags. You can have them point to an aspx of your choice. This aspx
outputs ONLY the image-bytes.

Hans Kesting
 
In order to do some UI animation or drawings can I use a Graphics object
in a
WEB Forms project (in VisualStudio.NET)

yes, you can do your own drawings with Graphics. simplest example: create a
page "page_image.aspx" to store the dynamically created image. in the
Page_Load of "page_image" create a bitmap and a Graphics object attached to
that bitmap:

Bitmap bitmap = new Bitmap(...);
Graphics g = Graphics.FromImage(bitmap) ;

then you can draw on the bitmap and at the end, save the bitmap content to
the page response:

bitmap.Save( Response.Outputstream, ImageFormat.jpg );

reference such page elsewhere (in any other page):

<img src="page_image.aspx">

Regards,
Wiktor Zychla
 
Renato,

This is a sample I made in VBNet, can you understand this one, otherwise I
translate it, that is not that much work?
(It is only to show, sending the image in real live will cost to much time
in my opinion)

\\\Form 1 Needs a imagebox on the page
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Image1.Height = New Unit(32)
Me.Image1.Width = New Unit(200)
Image1.Visible = True
Dim scriptString As String = "<script language=JavaScript>" & _
"setclock(); function setclock(){document.images.Image1.src = " & _
"'http://localhost/WebClock/WebForm2.aspx';setTimeout('setclock()',1000)}</script>"
Page.RegisterStartupScript("setclock", scriptString)
End Sub
///
\\\Form2 needs nothing
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Cache.SetExpires(DateTime.Now.AddTicks(500))
Dim myForeBrush As Brush = Brushes.Black
Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
Dim textHeight As Single
Dim bm As New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim textSize As SizeF = g.MeasureString(now.tostring, myFont)
g.DrawString(Now.ToString, myFont, myForeBrush, _
New RectangleF(0, 0, 120, 20))
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
bm.Save(ms, Imaging.ImageFormat.Bmp)
arrImage = ms.GetBuffer
Response.BinaryWrite(arrImage)
g.dispose
End Sub
///

I hope this helps a little bit?

Cor
 
Back
Top