How to create image dimension macros in Visual Studio?

I hate to do repetitive tasks. When I am writing css very often I need to set background image to some container. But I never know image dimensions. So usually I am going to explorer or photoshop in order to find image dimensions and then manually type css width and height for it. Lucky me Visual Studio has macros. Here is simple guide how to create context menu "Paste Image CSS" command.

  1. Launch Macros IDE
  2. In the Macros IDE, you should see a default Macro project “MyMacros”. Add a new Module to the MyMacros project by Right clicking on the project and then choosing Add | Add Module.
  3. Now copy paste macro code provided below.
  4. Now here is a little trick. From the Visual Studio IDE, Choose Tools | Customize. In the Customize window, check the "Context Menus" from the Toolbars area. If you done it correctly, you should see a new toolbar at top which have most of the Contextual Menus you see while working in the VS IDE. In Project and Solution Context Menu find Item submenu. While Item submenu is open, go to the Commands tab of the Customize Window and click the Macros. Now find and drag your Macro (PasteImageCSS) and drop it over the desired point in the Context Menu.

 

Insert Image CSS Macros Demo

    Sub PasteImageCSS()

        Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)

        Dim solution As UIHierarchy = win.Object

        Dim file As ProjectItem = solution.SelectedItems(0).Object

        Dim editPoint As EditPoint = DTE.ActiveDocument.Selection.ActivePoint.CreateEditPoint

        Try

            Dim objImage As System.Drawing.Image = System.Drawing.Image.FromFile(file.FileNames(0))

            editPoint.Insert(String.Format( _

            "background-image:url(images/{2});{0}" & _

            "{1}background-repeat:no-repeat;{0}" & _

            "{1}width:{3}px;{0}" & _

            "{1}height:{4}px;{0}" & _

            "{1}display:block;" _

            , ControlChars.NewLine, ControlChars.Tab, file.Name, objImage.Width, objImage.Height))

            objImage.Dispose()

        Catch ex As Exception

            MsgBox("Selected file is not an image.")

        End Try

    End Sub


Posted on Thursday, July 30, 2009 by | Add Comment

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5