Friday, August 24, 2012

ASP.NET Server Control Binary to image.

Introduction

This is a useful Control for display image from database.
You can find demo below,it too easy.

Step 1: Add reference to your project.

Note: You can find Reference at bottom of page.

img

Step 2: Register an assemnly to your page.

Get Demo

        <%@ Register Assembly="ZOMDev.Webcontrols" Namespace="ZOMDev.Webcontrols" TagPrefix="cc1" %>
    

Step 3: Place a server control.

        cc1:ImageByteHandler ID="ImageByteHandler1" runat="server" 
    

Step 4: Copy script to your method.

C#
        //Read image and convert to byte[]
        string imagePath = Request.PhysicalApplicationPath + @"\images\5.jpg";
        FileInfo fi = new FileInfo(imagePath);
        int size = (int)fi.Length;
        byte[] imgBytes = new byte[size];
        FileStream stream = new FileStream(imagePath, FileMode.Open);
        BinaryReader reader = new BinaryReader(stream);
        imgBytes = reader.ReadBytes(size);
        reader.Close();
        
        //Add to Control
        ImageByteHandler1.ImageByte = imgBytes;
    
VB
        //Read image and convert to byte[]
        Dim imagePath As String = Request.PhysicalApplicationPath + "\images\5.jpg"
        Dim fi As New FileInfo(imagePath)
        Dim size As Integer = CInt(fi.Length)
        Dim imgBytes As Byte() = New Byte(size - 1) {}
        Dim stream As New FileStream(imagePath, FileMode.Open)
        Dim reader As New BinaryReader(stream)
        imgBytes = reader.ReadBytes(size)
        reader.Close()
        
        //Add to Control
        ImageByteHandler1.ImageByte = imgBytes
    
DownloadDemo here..>>

pls enjoy zomdev.

No comments:

Post a Comment