Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphics / Drawing in asp.net 1

Status
Not open for further replies.

emblewembl

Programmer
May 16, 2002
171
0
0
GB
I am just starting to look into drawing graphics with asp.net. I have found an example that shows how to draw graphics and send them straight to the printer, using something like this:

Sub pd_PrintPage(sender As object, ev As PrintPageEventArgs)

Dim f As Font = new Font("Verdana", 9)
ev.Graphics.Drawstring("hello",f,Brushes.Black,480,65)

End Sub

I'm probably missing something really obvious but how do I draw a string, line or rectangle straight to my screen? All the examples I have seen have a sub with ev As PrintPageEventArgs which i can then only use when printing. Can anyone point me in the right direction?

Thanks. i love chocolate
 
In order to print graphics to a browser, you cannot simply print them to the page (unfortunately). You must use the graphics tools to create a graphics file, and associate that with an image.

I wish I could find the URL of the site that got me started so I could give it to you, but I cannot. However, I complied some sample code that you will probably find a bit easier to follow. It also uses a couple of features that I did not find in the original code:


Private Function GetSimplePictureFile() As String
Dim FilePath As String = Request.PhysicalApplicationPath
Dim PicWidth, PicHeight As Integer
Dim StringObj As SizeF
Dim ImageName As String
Dim FileName As String = "MyTempFile.jpg"
Dim SquareStart, SquareLength As Integer
Dim i As Integer

PicWidth = 400
PicHeight = 300
SquareStart = 50
SquareLength = 200

'Graphics objects:
Dim objBmp As New Bitmap(PicWidth, PicHeight, PixelFormat.Format24bppRgb)
Dim objGraphics As Graphics = Graphics.FromImage(objBmp)
objGraphics.SmoothingMode = SmoothingMode.HighSpeed

'Create a clean slate:
objGraphics.Clear(Color.LightGray)

'Get object to measure dimensions of string, and use to center string on image:
StringObj = objGraphics.MeasureString("Hello!", New Font("Times New Roman", 12))
objGraphics.DrawString("Hello!", New Font("Times New Roman", 12), SystemBrushes.WindowText, (PicWidth \ 2) - StringObj.Width \ 2, 4)

'Create blue rectangle with black border:
objGraphics.FillRectangle(New SolidBrush(Color.Blue), SquareStart, SquareStart, SquareLength, SquareLength)
objGraphics.DrawRectangle(New Pen(Color.Black), SquareStart, SquareStart, SquareLength, SquareLength)

'Fill rectangle with horizontal lines:
For i = 10 To SquareLength Step 10
objGraphics.DrawLine(New Pen(Color.Red), SquareStart, i + SquareStart, SquareStart + SquareLength, i + SquareStart)
Next

'Save picture as a .jpg and set image webcontrol to use this image
objBmp.Save(FilePath & "tempimage\" & FileName, ImageFormat.Jpeg)
ImageName = "tempimage\" & FileName

objGraphics.Dispose()
objBmp.Dispose()

Return ImageName
End Function

Sub LoadPicture()
image1.imageurl = GetSimplePictureFile()
End Sub


I know this may be a lot to digest, but it will get you going. If you have VS.net, it will be a little easier, since it will pop up with all of the functions that the graphics object is able to do.

Remember to also include:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

These are needed for the above code to work!

HTH!

Kevin B.
.Net Programmer [thumbsup]
 
Hi Kevin,

Thanks so much for posting this code for me. I have copied it and have also presumed that in order to show the image I'll need to set up an image tag in my html which I have done like this:

<asp:image id=&quot;image1&quot; runat=&quot;server&quot; />

However, the page loads and I see an image marker but no actual image. Have I missed something else?
i love chocolate
 
Hi Kevin,

Ignore my last post - I was being a muppet and it is all working fine. Thanks so much for your help!
i love chocolate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top