Wednesday, December 14, 2011 load an image from embedded resource

Below is a simple method that I couldn't find anywhere else online that loads a .gif image from an embedded resource and returns the image as the web page response

private static void WriteGifImageToResponse(HttpContext context, string resourcePath)
    HttpResponse response = context.Response;
    response.ContentType = "image/gif";
    Assembly asm = Assembly.GetExecutingAssembly();
    using (Stream imageStream = asm.GetManifestResourceStream(resourcePath))
        using (Image theImage = Image.FromStream(imageStream))
            theImage.Save(context.Response.OutputStream, ImageFormat.Gif);

Deploying console apps

You can check out my blog post on deploying console apps on the Dont Panic Labs Blog

Friday, September 16, 2011

Calling MSScript.ocx on a 64 bit Machine

I struggled for a while today with a 3rd party .Net app our company uses to print documents.  We recently moved our production environment from 32 bit Windows Server to 64 bit.  It was seamless for the most part except for the previously mentioned app.

The error message we saw:

Retrieving the COM class factory for component with CLSID {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error: 80040154.

As far as useless error messages go, this is one ranks up pretty high on my list.  However it wasn't totally useless, turns out that CLSID was unique and a quick Google search led me to realize it was associated with the MSScriptControl.  Turns out this app was targeted for "Any CPU" and the script control is a 32 bit resource.  Since we weren't the developers of the code we couldn't just go in and re-build the app, luckily we were able to force the app to run in 32 bit using corflags.

After installing the Windows SDK for .Net you can find corflags.exe in C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin

Open up a command prompt in that directory and you can execute

CorFlags "C:\pathtoyour.exe" /32BIT+

And voila your app will now lunch as a 32 bit app and will load the 32 bit .dlls.

Wednesday, July 13, 2011

Tech: Change Your Phone's Camera Resolution

Chances are that if you have a smartphone, you like to share the pictures you take with it.  Take a moment to take a look at the resolution your camera is set at.  My Droid Incredible goes up to 8 MegaPixel (3264x1952).  Because of the small lens, the pictures are pretty noisy, even in daylight.  So I'd never print anything larger than a 4x6.  Realistically I'd probably print a 3x4.  Most photo printers recommend a resolution of 300 dots per inch.  So a photo printed at 4 inches by 6 inches would need a resolution of 1800 x 1200.  On my phone this resolution is just below my 3 megapixel setting.  So at 3x4 I'd only need to use my 1 megapixel setting.  This also makes emailing and uploading my photos to Facebook MUCH faster!  Furthermore it uses less disk space on my memory card.

Tuesday, July 5, 2011

Code: EF Code First Column Name Mapping

When using Entity Framework Code First there are a couple of ways to map your classes to your database, below is a code snippet of what made the most sense to me.  It maps property names to column names, denotes the table name to use for the class, and specifies the primary key.  There are also a handful of different properties on this class, but because they have the same name as the column in the database they don't need to be explicitly set.

public class LocateDB : DbContext
    public LocateDB()
        : base("LocateDB")
    public DbSet<LocateAccount> LocateAccounts { getset; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        var entity = modelBuilder.Entity<LocateAccount>();
        entity.Property(x => x.PR_Fax).HasColumnName("PR_Fax#");
        entity.Property(x => x.PRATT_Fax).HasColumnName("PRATT_Fax#");
        entity.HasKey(x => x.LocateId);

Wednesday, May 18, 2011

Code: How to draw a Line that follows a path in WPF

I couldn't find a code snippet online that did exactly this, so I thought I'd share.  The following code draws a line on a path that ends up being 3 straight lines:

//line looks somethine like:
//    |
//    \
//     \
//      |
Point p1 = new Point(GetAdjustXPositionForIntrusion(model.LengthInInches, item.LocationInInches), canvasHeight - padding - 2);
Point p2 = new Point(p1.X, p1.Y - intrusionHeight);
Point p3 = new Point(textCanvasLeft + 4, intrusionTextBottom + intrusionHeight);
Point p4 = new Point(p3.X, intrusionTextBottom);
List<PathSegment> segments = new List<PathSegment>();
segments.Add(new LineSegment(p2, true));
segments.Add(new LineSegment(p3, true));
segments.Add(new LineSegment(p4, true));
PathFigure pf = new PathFigure(p1, segments, false);
PathGeometry pg = new PathGeometry();
Path p = new Path();
p.Data = pg;
p.Stroke = GetBrushForIntrusionType(item.IntrusionType);
p.StrokeThickness = 1;

Tuesday, May 17, 2011

Sync Outlook and Google Calendar

Just learned today that google has an app that will quickly and easily sync your outlook and google calendars.  Just a little app to download here:
Once installed just enter your gmail username and password and voila!

Sunday, April 10, 2011

Code: Nebraska Code Camp Windows Phone 7 Presentation

I did a little presentation on developing for Windows Phone 7 at Nebraska Code Camp yesterday.  The presentation was based on an MSDN article I wrote a while back.

Click here for the slides.  And here for the app.  Thanks to those who attended!