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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Render Bitmap From Within Custom Control

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
US
I am somewhat new to ASP.NET and newer at creating custom controls. I created a new "dummy control" for testing/learning purposes and have been trying to figure out how to write/render a Bitmap object to a web page from within the custom control. I have tried using Context.Response.BinaryWrite, MemoryStream.WriteTo(Context.Response.OutputStream), and more. Depending on what I do, I get one of two results:

[ul]
[li]The image is displayed correctly, but the page containing the custom control is not rendered. Only the image itself renders correctly.[/li]
[li]The image is displayed within the test page, but the image is colorless and is followed with garbage (random characters).[/li]
[/ul]

All this dummy class does is inherits an Image control and provides the user the ability to alter the color of a PNG.

Thank you in advance for your time/help! The control's code is below:


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

Namespace test
<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
Inherits WebControls.Image

Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
MyBase.RenderContents(writer)

Dim image As Image = Tint(System.Web.Hosting.HostingEnvironment.MapPath(MyBase.ImageUrl), Color.Yellow)

' Save the image to the OutputStream
Using ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

'I have tried clearing Context.Response but that will prevent anything else (e.g. the test page)
'from rendering:
' Context.Response.Clear()
' Context.Response.ContentType = "image/png"

Context.Response.BinaryWrite(ms.GetBuffer())

'I have tried multiple ways of writing to the output, an example of another method shown below:
'ms.WriteTo(Context.Response.OutputStream)
End Using
End Sub

Private Function Tint(filePath As String, c As Color) As Bitmap
'Load from file
Dim original As Image = Image.FromFile(filePath)
original = New Bitmap(original)

'Get a graphics object from the new image
Dim g As Graphics = Graphics.FromImage(original)

'Create the ColorMatrix
Dim colorMatrix As New ColorMatrix(New Single()() {New Single() {0, 0, 0, 0, 0}, New Single() {0, 0, 0, 0, 0}, New Single() {0, 0, 0, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {c.R / 255.0F, c.G / 255.0F, c.B / 255.0F, 0, 1}})

'Create some image attributes
Dim attributes As New ImageAttributes()

'Set the color matrix attribute
attributes.SetColorMatrix(colorMatrix)

'Draw the original image on the new image using the color matrix
g.DrawImage(original, New Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes)

'Dispose the Graphics object
g.Dispose()

'Return the image
Return DirectCast(original, Bitmap)
End Function
End Class
End Namespace
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top