This is a migrated thread and some comments may be shown as answers.

Howto display Image from Bitmap Object

1 Answer 556 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Rene
Top achievements
Rank 1
Rene asked on 02 Feb 2011, 10:10 AM
Hello,

please give me some information about loading a bitmap from memory.

I have tried it with

 

Dim stream As New System.IO.MemoryStream

 

Bmp1.Save(stream, System.Drawing.Imaging.ImageFormat.Png)

Image1.DataValue = stream.GetBuffer()

Image1.DataBind()


in CodeBehind. Bmp1 is a bitmap that is created and modified in code before.

No image is displayed. What do I have to do? I think I have not already understood the idea of binary image.

Thanks in advance

René

1 Answer, 1 is accepted

Sort by
0
Rene
Top achievements
Rank 1
answered on 02 Feb 2011, 12:37 PM
Solved.

The problem is solved with following code in vb:

1) Necessary to tell RadBinaryImage to add your own HTTPHandler; in my code it's inside of an UpdatePanel

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SCEImage1.ascx.vb" Inherits="SCE.SCEImage1" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register src="Canvas.ascx" tagname="Canvas" tagprefix="uc1" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div id="Div1" runat="server">
            <telerik:RadBinaryImage ID="Image1" runat="server" HttpHandlerUrl="~/MyHandler.axd" /> 
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

2) The custom HTTP handler is a standard class file with the following definition

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Telerik.Web.UI
  
  
Public Class MyHandler
    Inherits RadBinaryImageHandler
    Protected Overrides Sub ProcessRequestInternal()
        'base.ProcessRequestInternal();
  
        Dim dataContainer As BinarImageDataContainer = ImagePersister.LoadImage()
        If dataContainer Is Nothing Then
            Return
        End If
  
        Dim image As Byte() = dataContainer.Data
  
        If image Is Nothing Then
            Return
        End If
  
        Dim response = HttpContext.Current.Response
        response.Clear()
  
        response.BinaryWrite(image)
        response.ContentType = "image/png"
  
        response.Flush()
    End Sub
End Class

3) The code to bind the memory bitmap to BinaryImage control. It is a need to save Bitmap in same format like handled in HTTPHandler above

Dim stream As New System.IO.MemoryStream, Bmp1 As Bitmap

...do some stuff to create Bmp1
 
Bmp1.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Image1.DataValue = stream.ToArray()
Image1.DataBind()

4) Entry in web.config for the need of another HTTPHandler

<httpHandlers>
.....      
<add path="MyHandler.axd" type="MyHandler" verb="*" validate="false" />
</httpHandlers>

Hope it helps other vb user.

René
Tags
BinaryImage
Asked by
Rene
Top achievements
Rank 1
Answers by
Rene
Top achievements
Rank 1
Share this question
or