Compact Framework – Increasing ScrollBar Width

The Datagrid in the Compact Framework has fixed 13 pixel wide/tall scrollbars but no way to resize them. Creating a control that inherits from Datagrid, overriding the size of the scrollbars would be one way to go, but it can be achieved without the need to do this by using reflection.


FieldInfo fieldInfo = dgBinDetail.GetType().GetField("m_sbVert", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);

((VScrollBar)fieldInfo.GetValue(dgBinDetail)).Width = 30; 

Remember to add the System.Reflection namespace to your list of using statements at the top of your class.

Using Reflection to open a form by name

Here is a quick example of how the System.Reflection namespace can be utilised to open form instances in a method that accepts the form name as a string. It also shows how you can pass across variables to forms with overloaded constructors.

It’s particularly useful when working with a main menu structure, such as I have in a current project loaded into a Treeview.


private void LoadNewForm(string formName)
{
    try
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        Type type = assembly.GetType(formName);

        //for this form I have an overloaded constructor, accepting a 'User' object
        ConstructorInfo ci = type.GetConstructor(new Type[1] { typeof(User) }); 
        object[] argVals = new object[] { CurrentUser };  //pass 'CurrentUser' variable to form constructor
        Form frm = (Form)ci.Invoke(argVals);

        frm.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
} 

Graphics resource

Finding free graphics online is not easy, quite rightly artists expect to be paid for the hard work…trouble is I don’t like paying! If I get around to it I’ll knock together a list of the decent free game art sources I’ve found in recent times, but for now here’s just the one…

http://gamemakerfoundry.com/ is a new(ish) site created by Fabio Monzani, but already contains some really quality 2D and 3D game art. I’ve been in contact with Fabio recently and he is planning to release further 3D models in the near future, so this is definitely one to watch folks!

Tiled map editor

I’ve been using this for some while now so thought I’d post the link, so those of you that haven’t already taken advantage of it can do so. It’s a standalone tile map editor that is stupidly easy to use and thanks to Nick Gravelyn’s excellent TiledLib library, stupidly easy to incorporate into your XNA projects.

Whilst it’s nice to develop your own range of development tools, I’m a big believer in making life easy for yourself where you can! Sure I could write my own tile engine in XNA or Winforms, but I couldn’t do it this well and it would take me an age…time that could be better spent on other areas of my projects.

Anyway check out Tiled and Nick’s superb library, they’re both really rather good! 🙂

Selecting a region within an Image

The following code snippet shows you how to draw a selection rectangle over the top of an image and crop it accordingly.

You’ll need to add a Button and a PictureBox to a Form.

Click and drag over the PictureBox to select an area and when you’re ready, click the button and it will perform the crop and save the cropped selection as a new image. This follows on from my last post, have a play around with these methods and explore further!



using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Point startPoint = new Point();  //the point(x,y) we start drawing our rectange
        private Rectangle myRectangle = new Rectangle();  //a rectangle

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Cross;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint.X = e.X;  //start point gets mouse coords
            startPoint.Y = e.Y;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)  //are we holding the left mouse button?
            {
                Point endPoint = new Point();  //the end point (x,y) of our rectangle
                Pen p = new Pen(Brushes.Red);  //a red pen
                Graphics g = pictureBox1.CreateGraphics();

                endPoint.X = e.X;  //end point gets mouse coords
                endPoint.Y = e.Y;

                pictureBox1.Refresh();  //stop flicker and actually display rectangle

                Size myRectSize = new Size(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);  //calculate the size of our rectangle
                myRectangle = new Rectangle(startPoint, myRectSize);  //assign new rectangle object
                g.DrawRectangle(p, myRectangle);  //and draw it!
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Image myImage = CropImage(pictureBox1.Image, myRectangle);  //crop the image
            myImage.Save("C:\\testPic.png", ImageFormat.Png);  //save it
        }

        private static Image CropImage(Image imageToCrop, Rectangle rect)
        {
            Bitmap b = new Bitmap(imageToCrop);  //new bmp
            Image croppedImage = b.Clone(rect, b.PixelFormat);  //copy a section of the original

            return croppedImage;
        }
    }
}

Image resizing and cropping

A couple of methods to demonstrate the resizing and cropping of images.


private static Image ResizeImage(Image imageToResize, Size newSize)
{
    Bitmap b = new Bitmap(newSize.Width, newSize.Height);  //new bitmap
    Graphics g = Graphics.FromImage((Image)b);  //new graphics object 

    g.InterpolationMode = InterpolationMode.HighQualityBicubic;  //change this depending expanding/shrinking etc..
    g.DrawImage(imageToResize, 0, 0, newSize.Width, newSize.Height);  //draw the new image
    g.Dispose();  //done now thanks.

    return (Image)b;
}

You can of course use GetThumbnail in some instances and avoid all of this convolution, however using the interpolation method above will maintain far better image quality.


private static Image CropImage(Image imageToCrop, Rectangle rect)
{
    Bitmap b = new Bitmap(imageToCrop);  //new bmp
    Image croppedImage = b.Clone(rect, b.PixelFormat);  //copy a section of the original

    return croppedImage;
}

Scrolling a picturebox

Can’t be bothered extending the picturebox control to allow scrolling?…me either! Dump a panel on a form and set it’s ‘AutoScroll’ property to true, now place a picturebox on this panel and set it’s SizeMode to ‘AutoSize’. And there you have it, a scrollable picturebox without writing a single line of code!…just the way I like it! 🙂