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.

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;

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!