Capturing Photo from a MediaCapture Camera Stream metro app / Windows 8

I was having some difficulty with saving an image from a MediaCapture webcam stream to the local system for a in a metro app.

Below is what I came up with, I hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/// <summary>
/// Gets a picture from a MediaCapture object and saves it to a local file.  Applies the image to a local Image element.
/// </summary>
async private void GetPictureFromPreview()
{
    try
    {
    if (null != _mediaCaptureManager)// The MediaCapture object
    {
        var captureSettings = new ImageEncodingProperties()
        {
        Height = 240,
        Width = 320,
        Subtype = "JPEG"
        };
        // Requires documentsLibrary capability
        var folder = ApplicationData.Current.RoamingFolder;
        var file = await  folder.CreateFileAsync(@"CapturedPhoto.jpg");
        await _mediaCaptureManager.CapturePhotoToStorageFileAsync(captureSettings, file);

        MyImageElement.Source = new BitmapImage(new Uri(file.Path));
    }
    }
    catch (Exception ex)
    {
    Messagebox.Text = ex.Message;
    }
}

Programming Comments (0)

Build, Sign, and Deploy a Windows 8 Metro App with Powershell

Very seldom do I run into a problem and feel like I am the first to encounter it, especially with programming. When developing for Windows 8 I feel that way quite a lot.

I have been working on a small script to build, sign, and deploy a metro app into the Metro UI environment.  Below is what I have come up with so far. (Powershell 3.0).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function Get-Batchfile ($file) {
    $cmd = "`"$file`" & set"
    cmd /c $cmd | Foreach-Object {
        $p, $v = $_.split('=')
        Set-Item -path env:$p -value $v
    }
}

function Set-VsVars32($version = "11.0")
{
    #$key = "HKLM:SOFTWARE\Microsoft\VisualStudio\" + $version
    #$VsKey = Get-ItemProperty -Path $VsKey

    #$VsInstallPath = [System.IO.Path]::GetDirectoryName($VsKey.InstallDir)
    #$VsToolsDir = [System.IO.Path]::GetDirectoryName($VsInstallPath)
    #$VsToolsDir = [System.IO.Path]::Combine($VsToolsDir, "Tools")
    #$BatchFile = [System.IO.Path]::Combine($VsToolsDir, "vsvars32.bat")
    # I didn't take the time to get the above to work in Windows 8/PS3.  Shortcut below.
    $BatchFile = 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat'
    Get-Batchfile $BatchFile
    [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
}


function Build-Sln($Path, $Config="Release")
{
    $configParam = "/p:Configuration=" +$Config
    msbuild.exe $path $configParam
}

function Get-AppxPackageByName($Name)
{
    $package = Get-AppxPackage | Where {$_.Name -eq $Name}
    if($package.Name -ne $Name) { Write-Error "Unable to find package: $Name" }
    return $package
}

# Include the Visual Studio exes in our path
Set-VsVars32

# Change location to our working project dir
Set-Location 'C:\Users\username\Documents\Visual Studio 11\Projects\discovery\Base\Base\'

# Call Visual Studio to build your solution
Build-Sln -Path '..\Base.sln' -Config "Release"

# Remove our Appx file if it already exists
if(Test-Path .\bin\Base.appx) { Remove-Item .\bin\Base.appx }

# Pack the output into Base.Appx
MakeAppx.exe pack /d .\bin\Release\ /p .\bin\Base.appx

# Sign  the Appx so that it can be deployed.  Uses a PFX key (that you already have)
signtool.exe sign /fd sha256 /f .\Base_TemporaryKey.pfx .\Bin\Base.appx

# Insert the certificate into the root of your cert store. This allows the Appx to be executed.
certutil.exe -addstore root .\Base_Certificate.cer

#To add an Appx package
Add-AppxPackage -Path .\bin\Base.appx -Verbose
# To remove an Appx package
#Remove-AppxPackage(Get-AppxPackageByName("9f229500-7554-28302834-a28b-abb26743654a")) -Verbose

Computers Comments (0)

Visual Studio – Test List Not Updating

I have been frustrated at the fact that I must rebuild my sollution every time I want a new test method to appear in my test list. Refreshing did not work.

I discovered that there is an option which disables the discovery of new test methods, and that in Visual Studio 2010 it is enabled by default. To disable it:

  1. Tools
  2. Options
  3. Test Tools
  4. Test Project
  5. Disable background discovery of test methods
Disable Discovery Of Test Methods

Visual Studio: Disable Discovery Of Test Methods

Programming Comments (0)

Visual Studio 2010 – Cannot Start Test Project

I created a test project in Visual Studio with some unit tests in it, but was unable to run any of the tests. The test list was empty, and when I attempted to run it I got the following error:
Cannot start test project so-and-so because the project does not contains any tests.
Uggh I was so frustrated! It took me about an hour to figure out my silly mistake. I have a custom build configuration for the solution, and it was not set to build my test project. After enabling the build, everything worked (except for my test cases, which all failed (Red, Green, Refactor!).

Programming Comments (0)

The Lenovo Mini Cooper / “The Do Car”

Starting in September, one Lenovo randomly selected one person to drive a Lenovo Mini Cooper (The Do Car)for a week. Driving this car was amazingly fun. So much so that I drove over one thousand miles in less than a week! I also got to hand out Lenovo t-shirts and other nifty doo-dads. The interior is a tad bit goofy, especially with the speedometer being placed in the middle of the dash. Prior to driving the Mini I had no desire for a car other than my Honda Accord. Now I feel the urge for a smaller, lighter car that accelerates quickly and handles well. One of the best things about the Mini is that it easily got over 40 miles per gallon.

If you would be interested in winning a computer, check out the The Lenovo Do Car website!

I’d like to share some photos that I took while I was driving around. Taking pictures of the car was not a requirement of Lenovo’s, I did it on my own free will as a fun, mini project.

If you have seen the Do Car, I’d be interested in hearing where you saw it!

Parking deck – Downtown Raleigh, NC

Parking deck – Downtown Raleigh, NC. With some faux speed added.

Parking deck – Downtown Raleigh, NC

Fayetteville Street – Downtown Raleigh, NC

North Carolina Museum of Art


Carter–Finley Stadium


Carter–Finley Stadium


Carter–Finley Stadium

RBC Center

Boylan Bridge, Downtown Raleigh, North Carolina

Personal Comments (2)

Load All Comments on Reddit

I recently wanted to perform a search through all the comments for a Reddit story.
Rather than clicking on all the “Load more comments” links, I figured I would let JavaScript do it for me.

If you try to click them all at once, Reddit tends to freeze. This method allows a one second pause between each additional load.

Paste the following in your address bar while on the page, and watch them load:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
javascript:
function loadMore() {
    var id =  ($(".button"))[0].id;
    if($('#'+ id).html() == 'loading...') {
        $('#'+ id).removeClass('button');
    }else{
        $('#'+ id).click();
    }
    if(id) {
    next();
    }
}
function next(){
    window.setTimeout(loadMore, 1000);
}
loadMore();

And, if it is of any use, here is the compressed version:

1
javascript:function b(){window.setTimeout(a,1e3)}function a(){var a=$(".button")[0].id;if($("#"+a).html()=="loading..."){$("#"+a).removeClass("button")}else{$("#"+a).click()}if(a){b()}}a()

Computers, Programming Comments (1)

Reading Google Chrome bookmarks with C# & .net

Google Chome stores bookmarks for a given user in JSON format. With Windows, this file (Called Bookmarks) can typically be found located inside C:\Users\[user]\AppData\Local\Google\Chrome\User Data\Default .

The bookmarks system contains two roots, the ‘Bookmarks bar’ and ‘Other bookmarks’. Within these roots there can exist a (seemingly) infiniant amount of bookmarks and folders containing bookmarks.
The easiest way to extract the bookmarks is to use a JSON library, but I wanted to avoid adding an extra assembly to my project. The next option is to develop a series of classes which will be used to deserialize the JSON using the .net JavaScriptSerializer class.

A simple bookmark JSON file looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
   "checksum": "4136adb9cd06656cfcac7ed910d6c5d9",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "12951434934671257",
            "id": "3",
            "name": "Woot One Day, One Deal",
            "type": "url",
            "url": "http://www.woot.com/"
         } ],
         "date_added": "0",
         "date_modified": "12951434946783257",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [ {
            "date_added": "12951434946783257",
            "id": "4",
            "name": "Welcome to Facebook - Log In, Sign Up or Learn More",
            "type": "url",
            "url": "http://www.facebook.com/"
         } ],
         "date_added": "0",
         "date_modified": "12951434950972257",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

A more complex bookmark structure looks more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
   "checksum": "550dd4d89e2cd11eaf2b87d2aa05eaf6",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "12948923695706831",
            "id": "3",
            "name": "Welcome to Facebook - Log In, Sign Up or Learn More",
            "type": "url",
            "url": "http://www.facebook.com/"
         }, {
            "date_added": "12948923744214264",
            "id": "7",
            "name": "Jason Grimme - Web Developer in Raleigh, NC",
            "type": "url",
            "url": "http://www.studioshorts.com/"
         }, {
            "date_added": "12948923703380831",
            "id": "4",
            "name": "Google",
            "type": "url",
            "url": "http://www.google.com/"
         }, {
            "children": [ {
               "date_added": "12948923708491831",
               "id": "5",
               "name": "Woot\u00AE : One Day, One Deal\u2122",
               "type": "url",
               "url": "http://www.woot.com/"
            } ],
            "date_added": "12950310189344299",
            "date_modified": "12951434337054697",
            "id": "10",
            "name": "Sub Folder",
            "type": "folder"
         } ],
         "date_added": "0",
         "date_modified": "12951434418373129",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [ {
            "date_added": "12950114893839849",
            "id": "8",
            "name": "Yahoo!",
            "type": "url",
            "url": "http://www.yahoo.com/"
         }, {
            "children": [ {
               "children": [ {
                  "children": [ {
                     "children": [ {
                        "date_added": "12951434418373129",
                        "id": "18",
                        "name": "xkcd",
                        "type": "url",
                        "url": "http://xkcd.com/"
                     } ],
                     "date_added": "12950313354025472",
                     "date_modified": "12951434422846129",
                     "id": "15",
                     "name": "Sub-Sub-Sub Folder",
                     "type": "folder"
                  }, {
                     "date_added": "12950314249325837",
                     "id": "17",
                     "name": "Microsoft Corporations",
                     "type": "url",
                     "url": "http://www.microsoft.com/en-us/default.aspx"
                  } ],
                  "date_added": "12950313354025472",
                  "date_modified": "12950314249325837",
                  "id": "14",
                  "name": "Sub-Sub folder",
                  "type": "folder"
               } ],
               "date_added": "12950310189345299",
               "date_modified": "12950310189345299",
               "id": "12",
               "name": "TestC",
               "type": "folder"
            } ],
            "date_added": "12950310189345299",
            "date_modified": "12950313339333472",
            "id": "11",
            "name": "Sub Folder",
            "type": "folder"
         } ],
         "date_added": "0",
         "date_modified": "12950310144811299",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

Deserializing Chrome Bookmarks

I developed a few classes to deserialize the data into, and this is what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    /// <summary>
    /// Contains all information about bookmarks in Chrome
    /// </summary>
    public class Bookmarks
    {
        public string Checksum { get; set; }
        public Root Roots { get; set; }
        public int Version { get; set; }
    }

    /// <summary>
    /// Contains 'folders' such as 'Bookmarks bar' and 'Other bookmarks'
    /// </summary>
    public class Root
    {
        public RootItem Bookmark_bar { get; set; }
        public RootItem Other { get; set; }
    }

    /// <summary>
    /// A folder of bookmarks
    /// </summary>
    public class RootItem
    {
        public List<Child> Children { get; set; }
        public string Date_added { get; set; }
        public string Date_modified { get; set; }
        public int Id { get; set; }
        public String Name { get; set; }
        public string Type { get; set; }
    }

    /// <summary>
    /// Contains information about a specific bookmark
    /// </summary>
    public class Child
    {
        public List<Child> Children { get; set; }
        public string date_added { get; set; }
        public int ID { get; set; }
        public string Name { get; set; }
        public String Type { get; set; }
        public Uri Url { get; set; }
    }


   
     static void Main(string[] args)
    {
        string pathToDB = @"C:\Users\username\AppData\Local\Google\Chrome\User Data\Default\Bookmarks";
        string pathToCopiedDB = System.IO.Path.GetTempFileName();
        System.IO.File.Copy(pathToDB, pathToCopiedDB, true);

        string jsonData = string.Empty;
        using (StreamReader streamReader = new StreamReader(pathToCopiedDB))
        {
            jsonData = streamReader.ReadToEnd();
            streamReader.Close();
        }
       
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Bookmarks bookmarks = serializer.Deserialize<Bookmarks>(jsonData);
    }

You will now have a replica of your bookmarks in your project! Have fun.

Programming Comments (1)

Find more articles in the Archives...

-->