all groups > dotnet drawing api > march 2007 >
You're in the

dotnet drawing api

group:

How to display image from memory stream to an image button?



How to display image from memory stream to an image button? Snow
3/21/2007 7:11:33 PM
dotnet drawing api: Hi, I'm creating a asp.net web application using vb.net.

I have 7 images from stream and I put it inside a array. The problem now is how can I put those images inside my image button web server control?

I tried using response.outputstream it works but it overwrite my layout only one image is being display. Is there a way to output this images to another page and use my image button to reference the image from that page?

Re: How to display image from memory stream to an image button? maxnis NO[at]SPAM gmail.com
4/8/2007 4:23:52 PM
[quoted text, click to view]

Option Explicit On
Option Strict On

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Namespace ASPNetCookbook.VBExamples
''' <summary>
''' This class provides the code-behind for
''' CH15CreateButtonVB.aspx
''' </summary>
Partial Class CH15CreateButtonVB
Inherits System.Web.UI.Page

'constants used to create URLs to this page
Public Const PAGE_NAME As String = "CH15CreateButtonVB.aspx"
Public Const QS_BUTTON_TEXT As String = "ButtonText"

'''***********************************************************************
''' <summary>
''' This routine provides the event handler for the page load event.
It
''' is responsible for initializing the controls on the page.
''' </summary>
'''
''' <param name="sender">Set to the sender of the event</param>
''' <param name="e">Set to the event arguments</param>
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim buttonText As String
Dim button As Bitmap
Dim ms As MemoryStream
'get button text from the query string and create the image
buttonText = Request.QueryString(QS_BUTTON_TEXT)
button = makeButton(buttonText)

'write image to response object
ms = New MemoryStream
button.Save(ms, ImageFormat.Jpeg)
Response.ContentType = "image/jpg"
Response.BinaryWrite(ms.ToArray( ))
End Sub 'Page_Load

'''***********************************************************************
''' <summary>
''' This routine creates a button with the passed text.
''' </summary>
'''
''' <param name="buttonText">
''' Set to the text to place on the button
''' </param>
'''
''' <returns>Bitmap of button that was created</returns>
Private Function makeButton(ByVal buttonText As String) As Bitmap
'define the space around the text on the button
Const HORT_PAD As Integer = 10
Const VERT_PAD As Integer = 2

Dim button As Bitmap
Dim g As Graphics
Dim bfont As Font
Dim tSize As SizeF
Dim buttonHeight As Integer
Dim buttonWidth As Integer

'create the font that will used then create a dummy button to get
'a graphics object that provides the ability to measure the height
'and width required to display the passed string
bfont = New Font("Trebuchet MS", 10)
button = New Bitmap(1, 1, PixelFormat.Format32bppRgb)
g = Graphics.FromImage(button)
tSize = g.MeasureString(buttonText, bfont)

'calculate the size of button required to display the text adding
'some space around the text
buttonWidth = CInt(Math.Ceiling(tSize.Width + (HORT_PAD * 2)))
buttonHeight = CInt(Math.Ceiling(tSize.Height + (VERT_PAD * 2)))

'create a new button using the calculated size
button = New Bitmap(buttonWidth, _
buttonHeight, _
PixelFormat.Format32bppRgb)
'fill the button area
g = Graphics.FromImage(button)
g.FillRectangle(New
SolidBrush(ColorTranslator.FromHtml("#F0F0F0")), _
0, _
0, _
buttonWidth - 1, _
buttonHeight - 1)

'draw a rectangle around the button perimeter using a pen width of
2
g.DrawRectangle(New Pen(Color.Navy, 2), _
0, _
0, _
buttonWidth - 1, _
buttonHeight - 1)

'draw the text on the button (centered)
g.DrawString(buttonText, _
bfont, _
New SolidBrush(Color.Navy), _
HORT_PAD, _
VERT_PAD)
g.Dispose( )
Return (button)
End Function 'makeButton
End Class 'CH15CreateButtonVB
End Namespace

AddThis Social Bookmark Button