Alternative to animated GIF’s: AnimateImages jQuery Plugin

by Marcel Wijnands 28. August 2010 16:06

I know of two cases where you would want an animated GIF, but it won’t do the trick:

  • Browsing the web using an Android phone.
  • Showing some kind of “processing..” animation when clicking a link/button and make it work in Internet Explorer.

I created an alternative using some images and a piece of javascript, and turned it into a jQuery plugin.

What you need is images like these:

circleball

Name them like:

circleball-1.png, circleball-2.png … circleball-8.png

Put a container on your page, like a span or a div, and use the plugin like this:

$(".circleball").animateImages("/images/circleball-@.png", 8, 90);

In the above line, the @ gets replaced with the imagenumbers, 8 means there are 8 images, 90 means the animation-interval is 90 ms.

You can check out a working example here! This could be optimized to use sprites instead, but it's a start.

jquery.animateImages can be downloaded below;

jquery.animateImages.zip (1.01 kb)

Tags:

General | JavaScript

Streetview InfoWindow GControl for Google Maps API V2

by Marcel Wijnands 27. August 2010 00:13

Since I couldn’t find an existing Google Maps Streetview GControl, I decided to roll my own. This GControl will put a streetview button next to the default maptype buttons. Clicking it will show a “streetview guy” marker which you can drag around your map. Click the guy to open an infowindow showing the street view!

After adding the maps.streetviewcontrol.js to your page, all you have to do to add it to your map is:

map.addControl(new StreetViewControl);

Check out a working example here. Feel free to modify it to fit your needs.

StreetViewControl can be downloaded below;

maps.streetviewcontrol.zip (3.84 kb)

Tags:

Google Maps | JavaScript

Read/Write JPEG metadata

by Marcel Wijnands 10. November 2008 00:46

I expected to find a lot of information and examples about reading and writing JPEG metadata when I started searching the net. However I couldn't find much. After finding a great blog entry by Robert A. Wlodarczyk I created a useful Class that can be used in both ASP.NET and Windows Forms / WPF applications. Please note I'm always using VB 9.0's type inference, I love it because it's less typing ;)

To read or write metadata to a JPEG, use the JpegMetaData's constructor to open the file;

Dim myMetaData = New JpegMetaData(Server.MapPath("Photo.jpg"))

My basic class supports only 5 properties, but you could extend it;

myMetaData.Title = "My Title"
myMetaData.Subject = "My Subject"
myMetaData.Rating = 4
myMetaData.Keywords = (New String() {"Keyword1", "Keyword2"}).ToList
myMetaData.Comments = "My Comments"

the metadata is not saved until the save function is called, which will return a boolean to indicate wether the save succeeded;

Dim isSaved = myMetaData.Save()

JpegMetaData can be downloaded below;

VB: JpegMetaData.zip (1.71 kb)
C#: JpegMetaData C#.zip (1.63 kb)

UPDATE: Added the C# version thanks to koffiemokje.

Tags:

ASP.NET | VB.NET | WPF

Manage IIS 6 in .NET

by Marcel Wijnands 20. August 2008 00:19

If you ever needed to automate the creation of web applications, application pools, virtual directories, etc. you might find this IIsManager ClassLibrary I made to be very useful. Using the System.DirectoryServices namespace to manage IIS 6 is not very straightforward, so I decided to wrap IIS 6 into these classes;

  • IIsService
  • IIsApplicationPool
  • IIsSite
  • IIsDirectory
  • IIsFile
  • IIsVirtualDirectory

After instantiating the IIsService class, you can use it to iterate through sites (using LINQ if you want), adding or removing them, and set properties on them.

The following example will create a website, set it to use ASP.NET 2.0, create an application pool and set the website to use it, changes access permissions on a directory and creates a virtual directory containing a web application.

' Instantiate an IIsService which represents the W3SVC service on the localhost.
Using IIsSvc = New IIsService

    ' Creates an IIsSite.
    Using testsite = IIsSvc.AddSite("TestSite", "c:\inetpub\testsite", "test.site.nl")

        ' Sets AccessPermissions to allow reading and executing scripts (.aspx).
        testsite.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Script

        ' Sets the website to use ASP.NET 2.0
        testsite.ASPNETVersion = ASPNETVersions.v2_0_50727

        ' Create an IIsApplicationPool.
        Dim testpool = IIsSvc.AddAppPool("testpool")

        ' Set the site to use the new application pool.
        testsite.ApplicationPoolId = testpool.Id

        ' Creates an IIsDirectory and changes the AccessPermissions to allow writing to it.
        ' Note that the physical directory already has to exist since we're just creating
        ' metabase information.
        Using images = testsite.AddDirectory("images")
            images.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Write
        End Using

        ' Creates an IIsVirtualDirectory and creates a Web application in it.
        Using newapp = testsite.AddVirtualDirectory("newapp", "c\inetpub\newapp")
            newapp.CreateApplication()
            newapp.AccessPermissions = AccessPermissionFlags.Read + AccessPermissionFlags.Script
        End Using

        ' Starts the website. It is stopped at creation by default.
        testsite.StartSite()

    End Using

    ' Because the classes implement IDisposable, the Dispose method is automatically called
    ' at 'End Using'.
End Using

You can download the solution below. It's a Visual Studio 2008 solution, but the compiled assembly is also included. It contains some more examples for you to explain the usage of the ClassLibrary.

IIsManager.zip (65.34 kb)

The project is now also hosted on codeplex at http://www.codeplex.com/IIsManager.

Tags:

IIS 6 | VB.NET

Impersonator

by Marcel Wijnands 8. August 2008 23:49

As an addition to my previous post, I wrote a little Class that makes it very easy and safe to execute a piece of code as a different user in ASP.NET (of course it works in Forms as well). Because it implements the IDisposable interface, you can use a Using block in which the code is executed as the supplied user like this;

Using (New Impersonator("DOMAIN", "UserName", "Password"))

    For Each file As String In System.IO.Directory.GetFiles("\\SOMESERVER\SomeShare")

        Response.Write(file)
        Response.Write("<br />")

    Next

End Using

If you need to do this often in your application, and decide to keep a WindowsIdentity somewhere like I demonstrated in my previous post (to increase performance), you can use the other Constructor of the Impersonator Class.

First you can use the Shared Function of the Impersonation Class to get a WindowsIdentity;

Application("FixedIdentity") = Impersonator.GetFixedIdentity("DOMAIN", "UserName", "Password")

Now use the other Constructor;

' Get the WindowsIdentity from where you keep it (in this case the Application Class).
Dim FixedIdentity As WindowsIdentity = Application("FixedIdentity")

Using (New Impersonator(FixedIdentity))

    For Each file As String In System.IO.Directory.GetFiles("\\SOMESERVER\SomeShare")

        Response.Write(file)
        Response.Write("<br />")

    Next

End Using

Nice and simple! Download it below:

Impersonator.zip (1.06 kb)

The idea is based on the C# Impersonator Class written by Uwe Keim.

Tags:

ASP.NET | VB.NET

Accessing network shares with ASP.NET

by Marcel Wijnands 5. August 2008 23:51

Recently, I had to develop a web application that had to grab pictures off a network share, manipulate them, and show them on a page. The users of this application would log in automatically with their Windows user credentials and based on the groups they were a member of, they would or would not see certain content.

To do this, I had to use the 'Integrated Windows Authentication' option in IIS and add <identity impersonate="true"/> to the Web.config.

The thing I noticed is that even though the logged on user had the required rights to access the network share, I would be getting 'Access denied' or  'Path not found' error messages.

After some research I found out that, using impersonation like this, the IIS worker process would get a so called 'impersonation token' of the logged on user, while the token needed to access network resources has to be a 'primary token'. One workaround is to add credentials to the impersonation tag, like <identity impersonation="true" userName="DOMAIN\User" password="Pass"/>. This would cause the IIS worker process to run all code in the security context of 'User' using a primary token.

This workaround was useless for my application because I needed to run most code in the context of the logged on user. What I needed was to get a primary token of a user that has access to the network share, and run pieces of code under the security context of this user.

To do this I wrote a little function to get a WindowsIndentity containing a primary token;

Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0

Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
                        ByVal lpszDomain As String, _
                        ByVal lpszPassword As String, _
                        ByVal dwLogonType As Integer, _
                        ByVal dwLogonProvider As Integer, _
                        ByRef phToken As IntPtr) As Integer

Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                        ByVal ExistingTokenHandle As IntPtr, _
                        ByVal ImpersonationLevel As Integer, _
                        ByRef DuplicateTokenHandle As IntPtr) As Integer

Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

Public Function GetFixedIdentity(ByVal Domain As String, _
                                 ByVal UserName As String, _
                                 ByVal Password As String) As WindowsIdentity

    Dim tempIdentity As WindowsIdentity = Nothing
    Dim token As IntPtr = IntPtr.Zero
    Dim tokenDuplicate As IntPtr = IntPtr.Zero

    If RevertToSelf() Then
        If LogonUserA(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, _
                      LOGON32_PROVIDER_DEFAULT, token) <> 0 Then

            If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                tempIdentity = New WindowsIdentity(tokenDuplicate)
            End If

        End If
    End If
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then
        CloseHandle(tokenDuplicate)
    End If
    If Not token.Equals(IntPtr.Zero) Then
        CloseHandle(token)
    End If

    Return tempIdentity

End Function

Now, this piece of unmanaged code could be a performance hit if it is used often, because it will validate the user credentials against the domain controller every single time. I figured I only get this WindowsIdentity on application startup and keep it in the Application Class, so I added this in Global.asax.vb;

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    Application("FixedIdentity") = GetFixedIdentity("DOMAIN", "user", "pass")

End Sub

So whenever I needed access to a network share, I would run that piece of code in the ImpersonationContext of the WindowsIdentity I kept in the Application Class, like this;

Dim ImpersonationContext As WindowsImpersonationContext = Nothing

Try

    If Not Application("FixedIdentity") Is Nothing Then _
        ImpersonationContext = CType(Application("FixedIdentity"),  _
                                        WindowsIdentity).Impersonate

    'This code is executed by the Fixed Identity
    For Each File In Directory.GetFiles("\\SERVER02\D$\Images")

        Debug.WriteLine(File)

    Next

Catch ex As Exception

    Debug.WriteLine(ex.Message)

Finally

    ' This always has to be executed, so put it in Finally. Otherwise
    ' you will keep executing code as the Fixed Identity.
    If Not ImpersonationContext Is Nothing Then ImpersonationContext.Undo()

End Try

Now you will be able to access the share, and be happy! :D

Tags:

ASP.NET | IIS 6 | VB.NET

Very Honest

by Marcel Wijnands 4. August 2008 17:27

I really love the new Snipping Tool in Vista.

Not just because it is very useful, but it also displays very honest error messages.

Snipping Tool

Tags:

General

My First Blog

by Marcel Wijnands 3. August 2008 17:40

Hello people,

I'm trying to create a blog with VB.NET / C#.NET code snippets, tips and tricks I run across Smile

Have a good read!

Tags:

General

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant