Friday, May 2, 2008

USING GDI TO DYNAMICALLY ADD IMAGE OR TEXT ON IMAGE ASP.NET

Using GDI to dynamically add image or text on another image in asp.net/vb.net

Public Function writeimage()

' 'USING GDI TO DYNAMICALLY ADD TEXT AND IMAGES ON IMAGE
' INCULDE THE FOLLOWING :
' Imports System.Drawing
'Imports System.Drawing.Image
'Imports System.Drawing.Drawing2D
'Imports System.Drawing.Imaging

Dim myBitmap As New Bitmap(Server.MapPath(ViewState("Imagepath").ToString))
Dim myGraphic As Graphics = Graphics.FromImage(myBitmap)

'set the smoothing mode
myGraphic.SmoothingMode = SmoothingMode.AntiAlias

'draw the oval
'myGraphic.DrawArc(New Pen(Color.White, 2), 280, 0, 120, 50, 0, 360)

'draw the text
'Session("Text") = AddText.Text

Dim ds As New DataSet
ds = getdata()
Dim i, j As Integer
j = ds.Tables(0).Rows.Count

Dim drow As DataRow
If ds.Tables(0).Rows.Count > 0 Then
For Each drow In ds.Tables(0).Rows
Dim textcheck As String = drow("Text").ToString
Dim imagecheck = drow("imagename").ToString

If textcheck <> "NA" Then
'adding text to the image
myGraphic.DrawString(drow("Text"), New Font("Helvetica", 20, FontStyle.Bold), SystemBrushes.WindowText, New PointF(drow("PosX"), drow("PosY")))

ElseIf imagecheck <> "NA" Then
'adding image to the image
Session("imagepath1") = "Uploadedimages/" & LTrim(drow("imagename"))
Dim myimage1 As New Bitmap(Server.MapPath(Session("Imagepath1").ToString))
Dim myThumbNail As New Bitmap(myimage1, 60, 60) 'this will craete a thumbnail of the image
myGraphic.DrawImage(myThumbNail, New PointF(drow("PosX"), drow("PosY")))
' myGraphic.DrawImage(
End If
Next

End If



'note: enable these 3 lines and disable the previous if you want to see vertical text in action
'Dim stringFormat As New StringFormat()
'stringFormat.FormatFlags = StringFormatFlags.DirectionVertical
'myGraphic.DrawString("Alima!", New Font("Helvetica", 30, FontStyle.Bold), SystemBrushes.WindowText, New PointF(290, 10), stringFormat)

'set the content type
'======Getting Error=========================
'Response.ContentType = "image/jpg"
'============================

'save to the outputstream
Dim myBitmap1 As New Bitmap(myBitmap)

myBitmap1.Save(Server.MapPath("DemoImages/" & Session.SessionID & ".jpg"))
myGraphic.Dispose()
myBitmap.Dispose()
Dim testpath As String = "Demoimages/" & Session.SessionID & ".jpg"
Session("testpath") = testpath
prodimg.ImageUrl = testpath
BindGrid()
AddText.Visible = True

GridView1.Visible = True
Panel1.Visible = False


End Function

No comments: