Tuesday, December 23, 2008

Image to Byte Array and Byte Array to Image

Level: Intermediate

Knowledge Required:
Image Class

Description:
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\>" +
"</object\>";
document.getElementById(htmlobjectid).innerHTML = script;
}


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

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.

Tuesday, November 18, 2008

Setting Window / Form Position Programmatically

Level: Beginner

Knowledge Required:
Windows Forms

Description:
While working with Windows Forms we sometimes require to set the Window/Form Position manually before we show it. Consider the following code:
Dim frmNew as Form2
frmNew = New Form2()
frmNew.Location = New Point(10, 10)
frmNew.Show()

If we execute the above code, it is NOT sure that window will be displayed at 10, 10. Because we haven't set the StartPosition property. By default this property is equal to WindowsDefaultLocation that is why whenever we display the Form, Windows Operating system choose where to put it. And if we change Location after the frmNew.Show(), then our location will set.

To set the Location programmatically we should first set the StartPosition property as,

Dim frmNew as Form2
frmNew = New Form2()
frmNew.StartPosition = FormStartPosition.Manual
frmNew.Location = New Point(10, 10)
frmNew.Show()

Thursday, October 16, 2008

HTML Anchor Tag (<a> Link) with NO Redirection

Although this Blog is purely for Visual Basic Windows Forms Application Development. But I think we can discuss other stuff too.

So meanwhile working with HTML, I was trying to create an Anchor with a href defined but the link should NOT work. Click the following link,

Google

As you can see the above link should redirect this page to Google, but it is NOT redirecting it. OK now try to open the link in a new Tab (if you are using a borwser which has tabbed browsing support, i.e. Ctrl+Click). This time it should work. Here is the HTML,
<a href="http://www.google.com" onclick="return false;">Google</a>

As you can see in above "onclick" event we have executed a little javascript which returns false. This is the statement which is suppressing the default behaviour of anchor. Some sites handle it like this,

Google
<a href="javascript:void(0);" onclick="alert('You Clicked the Link');">Google</a>