Results 1 to 2 of 2

Thread: text and image button

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    text and image button

    I want to make a text and image button. Ie one where I can set the background image, provide text to be written ontop and also have it respond to onclick events. I looked into creating custom controls based on the button class but I have never been able to actually write ontop of a button....



    Lets start from scratch, any ideas?

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    quick example

    put this into a blank .aspx page
    Code:
    <%@ Page Language="VB" ContentType="image/jpeg" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Drawing2D" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    
    <script language="VB" runat="server">
    
    Sub Page_Load(Sender as Object, E as EventArgs) 
    	Dim rnd As New Random()
    	Dim strText As String
    	' create a font
    	Dim fnt As Font = New Font("Arial", 8, FontStyle.Bold)
    	' Load a bitmap based image from a file
    	'Dim newBitmap As Bitmap = New Bitmap(Server.MapPath("back.jpg"))
    	' This line is for creating a blank bitmap.
    	Dim newBitmap As Bitmap = New Bitmap(100,25,PixelFormat.Format32bppARGB)
    	' Bind the Bitmap to the graphics object
    	Dim g As Graphics = Graphics.FromImage(newBitmap)
            
        strtext = request.querystring("Text")
        
    	' get the length of the string in pixels
    	Dim tempg As Graphics = Graphics.FromImage(new Bitmap(1,1))
    	Dim StringLength As Integer = tempg.MeasureString(strText,fnt).Width
    	tempg.Dispose
    	
    	g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
    	g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
    	
            ' Draw the string to the Graphics object. Subtract the strings pixel length from the 
            ' images width do determine postioning so that the lst character is always along the
            ' right edge of the image.
    	g.DrawString(strText, fnt, New SolidBrush(Color.LightBlue), (newBitmap.Width / 2) - (stringlength /2), newbitmap.height/2 - 2)
            
            ' Get an array of ImageEncoders..array item 1 is Jpeg.
            Dim imgCodecs() As ImageCodecInfo  = ImageCodecInfo.GetImageEncoders()
    	
    	' Set quality Parameter for the Jpeg codec
    	Dim imgParams As EncoderParameters = New EncoderParameters(1)
    	Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100)
    	' Set quality
    	imgParams.Param(0) = imgQuality
    	 
    	' Render BitMap Stream Back To Client
    	newBitmap.Save(Response.OutputStream,imgCodecs(1), imgParams)
    	fnt.Dispose()
    	g.Dispose()
    End Sub
    
    </script>
    then set the ImageUrl property of your image button on another page to the aspx with the above code passsing it a Text querystring with the text for the button

    ImageURL = "mypage.aspx?Text=Click Me"
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width