New IoC Sample : A Basic Wizard

I’ve been asked before about creating a Wizard framework using the IoC Library.  We’ve created several wizards in the past for both the desktop and the compact framework using IoC, but I never formally created a reference project.  Well now that someone asked again over on Stack Overflow, I decided I should actually put one together.

The Wizard reference app follows reasonably good coding practices and uses a Model-View-Presenter/Model-View-Controller pattern (though since it uses data binding you might argue it’s more MVVM).  A Service handles storing all state info and a Presenter handles marshaling state to UI and UI to State data exchanges.

WizardDiagram

Here are some screen shots of the end product:

IoCWizard

To get the source code for this wizard, get the latest change set from the Codeplex project.

New Release: OpenNETCF Virtual Agent

I’ve published a release of the OpenNETCF Virtual Agent.  The Virtual Agent is an .NET implementation of an application using the MTConnect protocol for publishing data.  It uses the MTConnect Managed SDK for the publisher interface and comes with a Machine Simulator showing a simple, yet real-world example of how you might integrate it into your own factory floor.

machinesimulator

OpenNETCF.ORM: Updated Release

I’ve published a new release of the OpenNETCF ORM framework.  This release has some minor bug fixes and adds support for a few new things (TimeSpan data, field defaults, etc).  See the check-in notes for more details.

This release supports SQL Compact 3.5 for both the CF and full (desktop) frameworks and is in heavy use in released commercial applications, so I’m very confident in its stability.  I still don’t have an implementation for Windows Phone yet (I’ve had a few volunteers, but no one has actually ever delivered anything).  If you’d like to help me out, I’d love to see some community involvement here so contact me and let me know.

Disable/Enable Network Connections under Vista

Note: This is content from a blog originally published by Neil Cowburn in June of 2008, recovered with the aid of the wayback machine.


I got an email last week asking about how to disable a particular network connection under Vista. The specific scenario, how to disable an active 3G connection, is not something I’m going to cover, but what I present below could be used as basis for that scenario.

With Vista, Microsoft introduced two new methods to the Win32_NetworkAdapter class under WMI:Enable and Disable. Before can call either of those methods, we need to know how to enumerate the network connections.

The .NET Framework SDK provides a helpful utility called mgmtclassgen.exe, which can be used to create .NET-friendly wrappers of the WMI classes. Open up a Visual Studio command prompt and enter the following:

mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs

This will generate a file called NetworkAdapter.cs which will contain a C# representation of the WMI Win32_NetworkAdapter class. You can add this source code file to your C# project and then access all the properties without too much extra effort.

To filter and disable the specific adapters, you do something like this:

SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach(ManagementObject result in search.Get())
{
    NetworkAdapter adapter = new NetworkAdapter(result);

    // Identify the adapter you wish to disable here. 
    // In particular, check the AdapterType and 
    // Description properties.

    // Here, we're selecting the LAN adapters.
    if (adapter.AdapterType.Equals("Ethernet 802.3")) 
    {
        adapter.Disable();
    }
}

Don’t forget to add a reference to System.Management.dll!

New in OpenNETCF SDF 2.0 – Imaging API Wrapper – part II

NOTE: This entry is originally from Feb 3, 2006 and was recovered from the Internet Wayback Machine on May 27, 2014 due to demand for the content. Some links, etc may no longer be valid.


As promised, here are some details on the OpenNETCF.Drawing.Imaging namespace. I’m going to demonstrate how to accomplish several tasks listed in the previous post as not supported by the CF Bitmap class.

0. Preface. helper classes

In the wrapper we introduce 2 helper classes – StreamOnFile and ImageUtils. The latter is simply a collection of high-level image proverssing methods. The former is an IStream implemented over .NET Stream (including FileStream). The implementation is not complete, but sufficient for the Imaging API methods that expect an IStream parameter.

1. Thumbnails, loading parts of the large image

Loading an image in Imaging API is achieved via calls to decoders – COM objects implementing IImageDecoder interface. The basic imaging interface IImage uses decoders to load image data. Most of the decoders support loading partial image, dicarding the unnecessary data. E.g. if you need to load a 3000×2000 image into a 300×200 PictureBox control, it is obvious that you don’t need all 6MP of data taking a whopping 18 MB of RAM (24bpp). Moreover, most devices will simply throw an OutOfMemoryException fi you try something like this. Decoder can be instructed to load an image of the required size so that it will skip over those pixels that don’t make it (or factor them into interpolation process to scale the image more smoothly). Here is how we achieve it.


        static public IBitmapImage CreateThumbnail(Stream stream, Size size)
{
IBitmapImage imageBitmap;
ImageInfo ii;
IImage image;

ImagingFactory factory = new ImagingFactoryClass();
factory.CreateImageFromStream(new StreamOnFile(stream), out image);
image.GetImageInfo(out ii);
factory.CreateBitmapFromImage(image, (uint)size.Width, (uint)size.Height,
ii.PixelFormat, InterpolationHint.InterpolationHintDefault, out imageBitmap);
return imageBitmap;
}

After we got IBitmapImage object, we can convert it to the .NET Bitmap:

Bitmap bm = ImageUtils.IBitmapImageToBitmap(imageBitmap);

2. Image transformation (flip, rotate, gamma/brightness/contrast controls)

Imaging library offers a limited set of the image operations exposed via interface IBasicBitmapOps. These are also wrapped in the ImageUtils class so that you get the following methods:

public Bitmap RotateFlip(Bitmap bitmap, RotateFlipType type)
public Bitmap Rotate(Bitmap bitmap, float angle)
public Bitmap Flip(Bitmap bitmap, bool flipX, bool flipY)

Of course you are welcome to use the IBasicBitmapOps directly.

3. Image tags

TBD

4. Transparency and alpha blending

If you have a PNG image with alpha channel information and you load it into a Bitmap object, the transparency is immediately lost. Not so, if using IImage class.

ImagingFactory factory = newImagingFactoryClass();
IImage
img;
factory.CreateImageFromFile(
“rgba8.png”, out img);

Bitmap imageBackground = new Bitmap(“MyImage.bmp“);
Graphics
g = Graphics.FromImage(imageBackground);

IntPtr hDC = g.GetHdc();

RECT rc = RECT.FromXYWH(200, 200, width, height);
img.Draw(hDC, rc,
null
);
g.ReleaseHdc(hDC);

The above code will transparently draw rgba8.png over the specified bitmap.