//--------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: Utilities.cs
//
//--------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ParallelMorph
{
/// Helper utilities.
public static class Utilities
{
/// Clones an image into a new Bitmap.
/// The source image.
/// The new Bitmap.
public static Bitmap CreateNewBitmapFrom(Image source)
{
return CreateNewBitmapFrom(source, 1);
}
/// Creates a copy of the source image by scaling it with the specified scale value.
/// The source image.
/// The scaling factor to use when generating the target image.
/// The new Bitmap.
public static Bitmap CreateNewBitmapFrom(Image source, float scalingFactor)
{
return CreateNewBitmapFrom(source, (int)(source.Width*scalingFactor), (int)(source.Height*scalingFactor));
}
/// Creates a copy of the source image using the specified target width and height.
/// The source image.
/// The target width for the generated image.
/// The target height for the generated image.
/// The new Bitmap.
public static Bitmap CreateNewBitmapFrom(Image source, int targetWidth, int targetHeight)
{
var newBmp = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(newBmp)) g.DrawImage(source, 0, 0, newBmp.Width, newBmp.Height);
return newBmp;
}
/// Retrieves an element from a Tuple by item number.
/// Specifies the type of data contained in the tuple.
/// The tuple.
/// The item number.
/// Item1 if itemNumber is 0, otherwise Item2 if itemNumber is 1.
public static T Item(this Tuple tuple, int itemNumber)
{
switch (itemNumber)
{
case 0: return tuple.Item1;
case 1: return tuple.Item2;
default: throw new ArgumentOutOfRangeException("itemNumber");
}
}
}
}