Saturday 17 March 2012

OverWrite CSS File Dynamically in codebehind file....

Protected Sub RewriteCSSFile()
        Dim newHeight As String = String.Empty
        Dim newButtonName As String = String.Empty


        Dim PreviousBackgroundPosition As String = String.Empty
        Dim PreviousaHeight As String = String.Empty
        Dim PreviousaWidth As String = String.Empty

        Dim NextBackgroundPosition As String = String.Empty
        Dim NextaHeight As String = String.Empty
        Dim NextaWidth As String = String.Empty

        Dim CloseBackgroundPosition As String = String.Empty
        Dim CloseaHeight As String = String.Empty
        Dim CloseaWidth As String = String.Empty

        Dim aHeight As String = String.Empty
        Dim aWidth As String = String.Empty

        Dim cssFilePath As String = Server.MapPath("~\Other\MagicZoomPlusV4\magiczoomplus.css")

        If rbtn1.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons1.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons1-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons1-small.png"
            End If
        ElseIf rbtn2.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons2.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons2-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons2-small.png"
            End If
        ElseIf rbtn3.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons3.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons3-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons3-small.png"
            End If
        ElseIf rbtn4.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons4.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons4-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons4-small.png"
            End If
        ElseIf rbtn5.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons5.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons5-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons5-small.png"
            End If
        ElseIf rbtn6.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons6.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons6-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons6-small.png"
            End If
        ElseIf rbtn7.Checked Then
            If ddlButtonsSize.SelectedItem.Text = "Normal" Then
                newHeight = "24px"
                newButtonName = "buttons7.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
                newHeight = "32px"
                newButtonName = "buttons7-big.png"
            ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
                newHeight = "18px"
                newButtonName = "buttons7-small.png"
            End If
        End If

        If ddlButtonsSize.SelectedItem.Text = "Normal" Then
            PreviousBackgroundPosition = "0px 0px !important;"
            NextBackgroundPosition = "-24px 0px !important;"
            CloseBackgroundPosition = "-48px 0px !important;"

            PreviousaHeight = "24px !important;"
            PreviousaWidth = "24px !important;"

        ElseIf ddlButtonsSize.SelectedItem.Text = "Large" Then
            PreviousBackgroundPosition = "0px 0px !important;"
            PreviousaHeight = "32px !important;"
            PreviousaWidth = "32px !important;"

            NextBackgroundPosition = "-32px 0px !important;"
            CloseBackgroundPosition = "-64px 0px !important;"

        ElseIf ddlButtonsSize.SelectedItem.Text = "Small" Then
            PreviousBackgroundPosition = "0px 0px !important;"
            NextBackgroundPosition = "-18px 0px !important;"
            CloseBackgroundPosition = "-36px 0px !important;"

            PreviousaHeight = "18px !important;"
            PreviousaWidth = "18px !important;"
        End If

        ResetMagicZoomPlusImage(cssFilePath, newHeight, newButtonName, PreviousBackgroundPosition, PreviousaHeight, PreviousaWidth, NextBackgroundPosition, NextaHeight, NextaWidth, CloseBackgroundPosition, CloseaHeight, CloseaWidth)
    End Sub

------------------------------
 Shared Sub ResetMagicZoomPlusImage(ByVal cssFilePath As String, ByVal newHeight As String, ByVal newButtonName As String, ByVal PreviousBackgroundPosition As String, ByVal PreviousaHeight As String, ByVal PreviousaWidth As String, ByVal NextBackgroundPosition As String, ByVal NextaHeight As String, ByVal NextaWidth As String, ByVal CloseBackgroundPosition As String, ByVal CloseaHeight As String, ByVal CloseaWidth As String)
        Try
            Dim OldcssData As String = String.Empty
            Dim strNewbackground As String = String.Empty
            Dim strNewheight As String = String.Empty
            Dim i As Integer = 0

            If File.Exists(cssFilePath) Then
                Dim oRead As StreamReader
                oRead = IO.File.OpenText(cssFilePath)

                Do While oRead.Peek() <> -1
                    OldcssData += oRead.ReadLine + Environment.NewLine
                Loop
                oRead.Close()

                Dim cssPropertys As String = "background"
                Dim cssText() As String = New String() {"MagicThumb-buttons", "div.MagicThumb-buttons a[title^=""Previous""]", "div.MagicThumb-buttons a[title^=""Next""]", "div.MagicThumb-buttons a[title^=""Close""]", "MagicThumb-buttons a"}

                For Each Str As String In cssText
                    Dim temCssText As String = OldcssData
                    While temCssText.IndexOf(cssText(i)) <> -1
                        temCssText = temCssText.Substring(temCssText.IndexOf(cssText(i)))
                        Dim strFindText As String = temCssText.Substring(0, temCssText.IndexOf("}") + 1)

                        Dim strOldCss = strFindText
                        temCssText = temCssText.Substring(temCssText.IndexOf(strFindText) + strFindText.Length)
                        Dim strNewCss As String = String.Empty

                        Select Case cssText(i)
                            Case "MagicThumb-buttons"
                                If (strFindText.Contains("background")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("background"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("background:"))
                                    strNewCss += "background: transparent url(graphics/" & newButtonName & ")" & "  no-repeat 0 0;" & strFindText.Substring(strFindText.IndexOf(";") + 1)

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)
                                    strOldCss = strNewCss
                                    strFindText = strNewCss
                                End If
                                If (strFindText.Contains("height")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("height"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("height:"))
                                    strNewCss += "height: " & newHeight & strFindText.Substring(strFindText.IndexOf(";"))

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)
                                    Exit While
                                End If
                              

                            Case "div.MagicThumb-buttons a[title^=""Previous""]"
                                If (strFindText.Contains("background-position")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("background-position"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("background-position:"))

                                    'If PreviousaHeight <> "" OrElse PreviousaWidth <> "" Then
                                    strNewCss += "background-position:" & PreviousBackgroundPosition & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    'Else
                                    '    strNewCss += "background-position:" & PreviousBackgroundPosition & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    'End If

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)

                                    strOldCss = strNewCss
                                    strFindText = strNewCss
                                End If

                                If (strFindText.Contains("height")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("height"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("height:"))
                                    If PreviousaHeight <> "" Then
                                        strNewCss += "height:" & PreviousaHeight & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    Else
                                        strNewCss += strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    End If

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)

                                    strOldCss = strNewCss
                                    strFindText = strNewCss
                                End If

                                If (strFindText.Contains("width")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("width"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("width:"))
                                    If PreviousaWidth <> "" Then
                                        strNewCss += "width:" & PreviousaWidth & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    Else
                                        strNewCss += strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    End If
                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)

                                    Exit While

                                End If
                            Case "div.MagicThumb-buttons a[title^=""Next""]"
                                If (strFindText.Contains("background-position")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("background-position"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("background-position:"))

                                    strNewCss += "background-position: " & NextBackgroundPosition & strFindText.Substring(strFindText.IndexOf(";") + 1)

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)
                                    Exit While
                                End If
                            Case "div.MagicThumb-buttons a[title^=""Close""]"
                                If (strFindText.Contains("background-position")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("background-position"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("background-position:"))

                                    strNewCss += "background-position: " & CloseBackgroundPosition & strFindText.Substring(strFindText.IndexOf(";") + 1)

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)
                                    Exit While
                                End If
                            Case "MagicThumb-buttons a"
                                If (strFindText.Contains("height")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("height"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("height:"))
                                    If PreviousaHeight <> "" Then
                                        strNewCss += "height:" & PreviousaHeight & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    Else
                                        strNewCss += strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    End If

                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)

                                    strOldCss = strNewCss
                                    strFindText = strNewCss
                                End If

                                If (strFindText.Contains("width")) Then
                                    strNewCss = strFindText.Substring(0, strFindText.IndexOf("width"))
                                    strFindText = strFindText.Substring(strFindText.LastIndexOf("width:"))
                                    If PreviousaWidth <> "" Then
                                        strNewCss += "width:" & PreviousaWidth & strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    Else
                                        strNewCss += strFindText.Substring(strFindText.IndexOf(";") + 1)
                                    End If
                                    OldcssData = OldcssData.Replace(strOldCss, strNewCss)

                                    Exit While

                                End If
                        End Select
                    End While
                    i += 1
                Next

                Try
                    If File.Exists(cssFilePath) Then
                        File.Delete(cssFilePath)
                    End If

                    Dim oWrite As New StreamWriter(cssFilePath, True)
                    oWrite.WriteLine(OldcssData)
                    oWrite.Close()
                Catch ex As Exception
                    ErrorLog.XMLError("ResetMagicZoomPlusImage Function when overwriting CSS File.", ex)
                End Try
            End If
        Catch ex As Exception
            ErrorLog.XMLError("ResetMagicZoomPlusImage Function", ex)
        End Try
    End Sub

--------------------------------------------------------------------------------
CSS File
---------
/*


   Magic Zoom Plus v4.0.7
   Copyright 2011 Magic Toolbox
   Buy a license: www.magictoolbox.com/magiczoomplus/
   License agreement: http://www.magictoolbox.com/license/


*/


/* Style of the small image link */
.MagicZoomPlus, .MagicZoomPlus:hover {
cursor: url(graphics/zoomin.cur), pointer;
outline: 0 !important;
}

/* Style of the small image */
.MagicZoomPlus img {
border: 0 !important;
padding: 0 !important;
margin: 0 !important;
outline: 0 !important;
}

/* Style of the span inside the small image link */
.MagicZoomPlus span {
display: none !important;
}

/* Style of hint box */
.MagicZoomPlusHint {
background: url(graphics/hint.gif) no-repeat 2px 50%;
padding: 2px 2px 2px 20px !important;
min-height: 24px;
margin: 0;
text-decoration: none;
text-align: left;
line-height: 24px;
font-size: 8pt;
font-family: sans-serif;
color: #444;
}

/* Style to hide external title or caption on a page. Only needed for #id method */
.MagicZoomPlus-ExternalText {
display: none;
}


/* Style of the zoomed image */
.MagicZoomBigImageCont {
border: 1px solid #999;
}

/* Style of text on the zoomed image */
.MagicZoomHeader {
font-size: 10pt !important;
line-height: normal !important;
color: #fff;
background: #666;
text-align: center !important;
}

/* Style of square magnify area under the cursor */
.MagicZoomPup {
border: 1px solid #aaa;
background: #fff;
cursor:url(graphics/zoomin.cur),pointer;
}

/* Style of loading message and icon shown during load */
.MagicZoomLoading {
border: 1px solid #ccc;
background: #fff url(graphics/loader.gif) no-repeat 2px 50%;
padding: 4px 4px 4px 24px !important;
margin: 0;
text-decoration: none;
text-align: left;
line-height: 1.5em;
font-size: 8pt;
font-family: sans-serif;
color: #444;
}

/* Style of hotspots */
.MagicHotspots {
display: none;
visibility: hidden;
}
.MagicHotspots a {
border: 1px solid #ccc;
display: block !important;
position: absolute !important;
}
.MagicHotspots a:hover {
border: 1px solid red;
}

/* Style of the small image when the large image is expanded */
.MagicThumb-expanded-thumbnail {
cursor: default;
}

/* Style of the expanded image */
.MagicThumb-expanded {
cursor: url(graphics/zoomout.cur), pointer;
background: #ffffff;
border: 1px solid #ccc;
outline: 0;
padding: 0;
}

/* Style of the caption for the expanded image */
.MagicThumb-caption {
color: #333;
font: normal 10pt Verdana, Helvetica;
background: #ccc;
text-align: left;
padding: 8px;
min-height: 18px !important;
border: 0 !important;
outline: 0 !important;
}

/*Start--This will Modify when Clicking of Save All Button of Admin/AllSettings.aspx Page */
/*Start--- Don't Modify Manually  */
.MagicThumb-buttons {
background: transparent url(graphics/buttons2-big.png)  no-repeat 0 0;
height: 32px;
display: block;
}

/*Start--This is Extra Style Added by Team7e for the close/next/previous buttons */
div.MagicThumb-buttons a[title^="Previous"]
{
     background-position:0px 0px !important;
}
div.MagicThumb-buttons a[title^="Next"]
{
     background-position: -32px 0px !important;
}
 div.MagicThumb-buttons a[title^="Close"]
 {
  background-position: -64px 0px !important;
 }
/*End--This is Extra Style Added by Team7e for the close/next/previous buttons */

.MagicThumb-buttons a {
width:32px !important;
height:32px !important;
margin: 0px 1px !important;
overflow: hidden;
}
/*End--- Don't Modify Manually  */
/*End--This will Modify when Clicking of Save All Button of Admin/AllSettings.aspx Page */


/* Style of the loading message box */
.MagicThumb-loader {
font: normal 12pt sans-serif;
line-height: 1.5em;
border: 1px solid #000;
background: #fff url(graphics/loader.gif) no-repeat 2px 50%;
padding: 2px 2px 2px 22px;
margin: 0;
text-decoration: none;
text-align: left;
}


/* Style of shadow effect behind zoomed image */
.MagicBoxShadow {
-moz-box-shadow: 3px 3px 4px #888888;
-webkit-box-shadow: 3px 3px 4px #888888;
box-shadow: 3px 3px 4px #888888;
border-collapse: separate;
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#888888') !important;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#888888')" !important;
}

/* Style of glow effect behind zoomed image */
.MagicBoxGlow {
-moz-box-shadow: 0px 0px 4px 4px #888888;
-webkit-box-shadow: 0px 0px 4px 4px #888888;
box-shadow: 0px 0px 4px 4px #888888;
border-collapse: separate;
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Glow(Strength=4, Color='#888888') !important;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Glow(Strength=4, Color='#888888')" !important;
}

.MagicZoomPlus {cursor: url(graphics/zoomin.cur), pointer;}
.MagicThumb, .MagicThumb:hover {cursor: url(graphics/zoomin.cur), pointer;}
.MagicThumb-expanded {cursor: url(graphics/zoomout.cur), pointer;}

No comments:

Post a Comment