Tuesday, December 23, 2008

Image to Byte Array and Byte Array to Image

Level: Intermediate

Knowledge Required:
Image Class

Here are simple functions that convert an Image into Byte Array and Byte Array into Image. An image is required to be converted into bytes array when we save it in Database, hence on reading the bytes from database, it needs to be converted back into image.

Private Function BytesToImage(ByVal ImageBytes() As Byte) As Image
    Dim imgNew As Image
    Dim memImage As New System.IO.MemoryStream(ImageBytes)
    imgNew = Image.FromStream(memImage)
    Return imgNew
End Function

Private Function ImageToBytes(ByVal Image As Image) As Byte()
    Dim memImage As New System.IO.MemoryStream
    Dim bytImage() As Byte

    Image.Save(memImage, Image.RawFormat)
    bytImage = memImage.GetBuffer()

    Return bytImage
End Function

Thursday, December 18, 2008

Java Script Function to Show Flash Banner

Usually I develop Desktop Application. But sometimes I also get involve in Web Development. So we were developing a Website in which we were trying to create a Flash Banner rotator. To accomplish this there are several techniques, but I created the following Java Script function for our Web Developer.

function showflashbanner(htmlobjectid, flashfilename, width, height) {
var script;
script = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' " +
"codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' " +
"width='" + width + "' height='" + height + "' \>" +
"<param name='movie' " +
"value='" + flashfilename + "' /\>" +
"<param name='quality' value='high' /\>" +
"<embed src='" + flashfilename + "' " +
"quality='high' " +
"pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' " +
"type='application/x-shockwave-flash'" +
"width='" + width + "'" +
"height='" + height + "'\>'" +
"</embed\>" +
document.getElementById(htmlobjectid).innerHTML = script;

<body onload="showflashbanner('bannercontainer', 'http://www.mywebsite.com/banners/banner1.swf', 720, 100)">
<div id="bannercontainer">Loading...</div>

As you can see the above example we have used "div" as a Banner container. Then we are passing this div's id in our function so the Flash File will be loaded in it.

The Web developer then used this function by creating an Array of Flash Files. He also created another function in which he used setTimeout function. So the function calls itself after some time with new Flash File.