Skip to content

Instantly share code, notes, and snippets.

@RedTahr
Created November 20, 2014 22:24
Show Gist options
  • Save RedTahr/744e723eedd576a78e06 to your computer and use it in GitHub Desktop.
Save RedTahr/744e723eedd576a78e06 to your computer and use it in GitHub Desktop.
WinPhone helper functions in Xamarin
// http://social.msdn.microsoft.com/forums/windowsapps/en-us/b639cd8a-30c2-48cf-99be-559f34cbfa79/convert-string-to-color-in-metro
public static System.Windows.Media.Color GetColorFromHexString(string hexValue) {
hexValue = hexValue.Substring(1, 6); // string will be passed in with a leading #
byte a = 0xff;// Convert.ToByte(hexValue.Substring(0, 2), 16);
var r = Convert.ToByte(hexValue.Substring(0, 2), 16);
var g = Convert.ToByte(hexValue.Substring(2, 2), 16);
var b = Convert.ToByte(hexValue.Substring(4, 2), 16);
return System.Windows.Media.Color.FromArgb(a, r, g, b);
}
// crop image to square based on smallest dimension
public static void CropSquare(ref WriteableBitmap bmp) {
float dimension = Math.Min((float)bmp.PixelWidth, (float)bmp.PixelHeight);
float x = ((float)bmp.PixelWidth / 2) - (dimension / 2);
if (x < 0) { x = 0; }
float y = ((float)bmp.PixelHeight / 2) - (dimension / 2);
if (y < 0) { y = 0; }
bmp = WriteableBitmapExtensions.Crop(bmp, (int)x, (int)y, (int)dimension, (int)dimension);
return;
}
public static string ImageFolder {
get {
string imageFolder = @"\Shared\ShellContent";
using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) {
if (!isoFile.DirectoryExists(imageFolder)) {
isoFile.CreateDirectory(imageFolder);
}
}
return imageFolder;
}
}
// scale image, maintain aspect ratio
public static void Scale(int limitingDimension, ref WriteableBitmap bmp) {
float scaleFactor = Math.Max(limitingDimension / (float)bmp.PixelHeight, limitingDimension / (float)bmp.PixelWidth);
if(scaleFactor > 1) { return; }
Size newDimensions = new Size(bmp.PixelWidth * scaleFactor, bmp.PixelHeight * scaleFactor);
bmp = WriteableBitmapExtensions.Resize(bmp, (int)newDimensions.Width, (int)newDimensions.Height, WriteableBitmapExtensions.Interpolation.Bilinear);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment