Dynamic image creation problems RRS feed

  • Question

  • User547880637 posted

    Hi there

    I have been building a db driven application where users select a base image that has 1-4 pre-determined areas on it for text or images. Users can lay text and images over the top of the base image and when completed, save the image as a JPG to their local system. As there can be 1-4 per base image I created a repeater and on the button1_click method I updated my code to loop through the repeaeter and create the text overlays (You will see this in the code I posted below).

    NOW, the the basic text creation and image saving is completed and the code I am using has been tested and works 100%, but since moving the code into a FOR loop for the repeater it seems to have broken and nothing is being done at all. I think that the repeater in the .aspx page is maybe empty after the button1_click and so the for loop is breaking.

    I would be grateful for anyones help. Simon


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

    doGlobal.chkClientLogin(Session("socGUID"), Application("logSocialite"))

    Dim sdc As New SocialiteDataContext()TemplateAreasRepeater.DataSource = sdc.SelectTemplateAreas(Request.QueryString("TemplateID"))


    Dim ReturnTemplate As Template = sdc.SelectTemplate(Request.QueryString("TemplateID")).SingleOrDefault

    Dim ReturnCategory As Category = sdc.SelectCategory(Request.QueryString("CategoryID")).SingleOrDefault

    Label1.Text = ReturnCategory.CategoryName

    Label1.Visible = True

    Label2.text = ReturnTemplate.TemplateName

    Label2.Visible = True

    If Not IsPostBack Then

    FileSystem.FileCopy(MapPath("~/imgs/templates/preview/" & ReturnTemplate.ImageFile), MapPath("~/imgs/templates/temp/" & Session("socGUID") & ".jpg"))

    End If

    Image1.ImageUrl = "~/imgs/templates/temp/" & Session("socGUID") & ".jpg"

    HiddenField1.Value = ReturnTemplate.ImageFile

    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim bitMapImage As Bitmap = New Bitmap(MapPath("~/imgs/templates/" & HiddenField1.Value))

    Dim graphicImage As Graphics = Graphics.FromImage(bitMapImage)

    Dim Item As RepeaterItem

    Dim strValues As String = ""

    For Each Item In TemplateAreasRepeater.Items

    Dim AreaXPos As Integer = CType(Item.FindControl("HiddenFieldXPos"), HiddenField).Value

    strValues = "X: " & AreaXPos & "<BR>"

    'Dim AreaXPos As Integer = Int32.Parse(TryCast(Item.FindControl("HiddenFieldXPos"), HiddenField).Value)

    Dim AreaYPos As Integer = CType(Item.FindControl("HiddenFieldYPos"), HiddenField).Value

    strValues = strValues & "Y: " & AreaYPos & "<BR>"

    Dim AreaWidth As Integer = CType(Item.FindControl("HiddenFieldWidth"), HiddenField).Value

    strValues = strValues & "Width: " & AreaWidth & "<BR>"

    Dim AreaHeight As Integer = CType(Item.FindControl("HiddenFieldHeight"), HiddenField).Value

    strValues = strValues & "Height: " & AreaHeight & "<BR>"

    Dim sText As String = CType(Item.FindControl("AdText"), TextBox).Text

    strValues = strValues & "Text: " & sText & "<BR>"

    Dim sFont As String = CType(Item.FindControl("ddlFont"), DropDownList).SelectedValue

    strValues = strValues & "Font: " & sFont & "<BR>"

    Dim sFontSize As Integer = CType(Item.FindControl("ddlSize"), DropDownList).SelectedValue

    strValues = strValues & "Size: " & sFontSize & "<BR>"

    Dim sColour As String = CType(Item.FindControl("ColorPicker1"), CustomControls.ColorPicker).Color

    strValues = strValues & "Color: " & sColour & "<BR>"

    Dim solidBrush As New SolidBrush(System.Drawing.ColorTranslator.FromHtml(sColour)) 'Convert hex to brush!!!

    Dim ddlHAlignment As String = CType(Item.FindControl("ddlHAlignment"), DropDownList).SelectedValue

    strValues = strValues & "Horizontal: " & ddlHAlignment & "<BR>"

    Dim ddlVAlignment As String = CType(Item.FindControl("ddlHAlignment"), DropDownList).SelectedValue

    strValues = strValues & "Vertical: " & ddlVAlignment & "<BR/><br/>"

    Dim stringFormat As New StringFormat()

    stringFormat.Trimming = StringTrimming.EllipsisWord

    If ddlHAlignment = "Near" Then

    stringFormat.Alignment = StringAlignment.Near

    ElseIf ddlHAlignment = "Center" Then

    stringFormat.Alignment = StringAlignment.Center

    ElseIf ddlHAlignment = "Far" Then

    stringFormat.Alignment = StringAlignment.Far

    End If

    If ddlVAlignment = "Near" Then

    stringFormat.LineAlignment = StringAlignment.Near

    ElseIf ddlVAlignment = "Center" Then

    stringFormat.LineAlignment = StringAlignment.Center

    ElseIf ddlVAlignment = "Far" Then

    stringFormat.LineAlignment = StringAlignment.Far

    End If

    Dim font As New Font(sFont, sFontSize)Dim rect As New Rectangle(AreaXPos, AreaYPos, AreaWidth, AreaHeight)

    graphicImage.TextRenderingHint = TextRenderingHint.AntiAliasGridFit

    graphicImage.DrawString(sText, font, solidBrush, rect, stringFormat)



    bitMapImage.Save(MapPath("~/imgs/templates/completed/" & Session("socGUID") & ".jpg"), ImageFormat.Jpeg)


    DownloadLink.Text = "<a href=""/imgs/templates/completed/" & Session("socGUID") & ".jpg"">Right click and select 'Save target as...' to save the image to your computer.</a>"

    DownloadLink.Visible = True


    End Sub

    Tuesday, November 18, 2008 5:23 AM


  • User547880637 posted

    Ok guys I have remedied the issue...to get the values of the textboxes and dropdowns I have to actually create new instances of those objects and then set a variable to the values/selectedvalues, like so:

    Dim ddlFontSize As DropDownList = Item.findcontrol("ddlSize")

    sFontSize = ddlFontSize.SelectedValue

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 18, 2008 8:36 AM