NumericUpDown Control and Selecting Text (Revisited)

Back in Jan ’06 I wrote an post on selecting the text of a NumericUpDown control.  Peter Morris pinged me the other day to let me know that he has updated the code provided and added support for Validation events.  It’s great to see code being used by others and even greater to see when people like Peter share their changes back with the community.  Check out his updates here.

If you have used any code I’ve released via my blog, our Open Source products or any code from OpenNETCF partners either in production or just for a hobby project let me know and I can share your story!

Using Extension Methods in .NET CF 2.0

Extension methods are a great addition to the C# 3.0 Language Specification as it allows you to extend a class by adding new methods and extend the class with features you desire.  For the .NET Compact Framework developer this is available in Visual Studio 2008 and .NET Compact Framework 3.5

At OpenNETCF we have a few developer products but are still using .NETCF 2.0.  Even a lot of our consulting jobs still use .NETCF 2.0 (and sometimes 1.0) but more customers are now wanting CF3.5.

Getting used to extension methods and then not being able to use them in pre .NETCF3.5 projects is not fun.  After doing a little search I found this were Daniel explains using extension methods in .NET 2.0 and how the compiler “figures things out”. Basically the same thing applies for .NETCF 2.0.  You do require Visual Studio 2008 for this to work.

Basically what you have to do is add the following to your project:

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute
    {
    }
}

With that you can share extension methods you have written for .NETCF3.5 projects with .NETCF2.0 projects.

For example, I do a lot of custom drawing for controls on Windows Mobile and draw images with transparency.  Pre extension methods I would use something like this in .NETCF 2.0:

protected override void OnPaint(PaintEventArgs e)
{
    Bitmap image = new Bitmap(m_imagePath);
    ImageAttributes imageAtt = new ImageAttributes();
    Color transparentColor = image.GetPixel(0, 0);
    imageAtt.SetColorKey(transparentColor, transparentColor);
    e.Graphics.DrawImage(image, 
        new Rectangle(0,0,image.Width,image.Height), 
        0, 0, 
        image.Width, image.Height, 
        GraphicsUnit.Pixel, imageAtt);
    image.Dispose();
}

With the Attribute workaround above we create an extension method as follows:

public static void DrawImageTransparent(this Graphics graphics, 
    Bitmap image, 
    Rectangle destinationRect)
{
    ImageAttributes imageAtt = new ImageAttributes();
    Color transparentColor = image.GetPixel(0, 0);
    imageAtt.SetColorKey(transparentColor, transparentColor);
    graphics.DrawImage(image, 
        destinationRect, 0, 0, 
        image.Width, image.Height, 
        GraphicsUnit.Pixel, imageAtt);
}

And we can call it from our OnPaint method as follows:

using (Bitmap image = new Bitmap(m_imagePath))
    e.Graphics.DrawImageTransparent(image, new Rectangle(0, 0, image.Width, image.Height));

In the end we get a lot cleaner code and the extension method is portable to .NETCF3.5 and 2.0.

Outlook Mobile Style Textbox

There was a question on how to create a Textbox similar in style to Outlook Mobile contacts textbox shown below (single line shown for inputs).


Contacts


So to accomplish this I wrote a quick custom control that included a textbox internally and the control was on pixel higher than the textbox and set the Textbox.BorderStyle to None. On the OnPaint method of the custom control I just call Graphics.Clear() to draw a black line since only one pixel height is visible.


Here is the source.  It still requires a lot of work but it’s a good start and it answers the original question.

public class OutlookMobileTextBox : Control
{
private TextBox m_textBox;

public OutlookMobileTextBox()
{
m_textBox = new TextBox();
m_textBox.Location = new System.Drawing.Point(48, 107);
m_textBox.BorderStyle = BorderStyle.None;
m_textBox.Size = new System.Drawing.Size(100, 21);
Controls.Add(m_textBox);
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
m_textBox.Bounds = new Rectangle(0, 0, this.Width, m_textBox.Height);
base.Height = m_textBox.Height + 1;

}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.Black);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}

public override string Text
{
get
{
return this.m_textBox.Text;
}
set
{
this.m_textBox.Text = value;
}
}


And here is what it looks like on a device:


 OutlookMobileTextbox

Extracting EXIF Tags using Compact Framework

We got a request from a customer on how to extract EXIF Tags from an image taken with a camera using .NET Compact Framework and the Smart Device Framework. 


The OpenNETCF.Drawing.Imaging namespace in the Smart Device Framework does provide this data but if you are not familiar with the namespace it may be a little tricky getting the information.


Basically what you have to do is get an IImageDecoder object using the ImagingFactory class.  From there call ImageUtils.GetAllProperties(decoder) which returns an array of ImageProperty[] objects.


Here is the bulk of the code:


StreamOnFile st = new StreamOnFile(openFileDialog1.FileName);
IImageDecoder decoder = null;
ImagingFactory factory = new ImagingFactoryClass();
factory.CreateImageDecoder(st, DecoderInitFlag.DecoderInitFlagNone, out decoder);
ImageProperty[] props = ImageUtils.GetAllProperties(decoder);

foreach (ImageProperty prop in props)
{
   //For Specific tags see ImageTag enum. 
   textBox1.Text = prop.Id.ToString() + “: “ + GetValue(prop) + “\r\n” + textBox1.Text;
}
decoder.TerminateDecoder();


And the output:



Download the sample application here.