//http://aspascension.codeplex.com/
//Microsoft Public License (Ms-PL)
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Data;
namespace ASPAscension.Silverlight.Converters
{
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.GetType() == typeof(String))
{
string val = value as string;
Color c;
if (val.StartsWith("#"))
{
val = val.Replace("#", "");
byte a = System.Convert.ToByte("ff", 16);
byte pos = 0;
if (val.Length == 8)
{
a = System.Convert.ToByte(val.Substring(pos, 2), 16);
pos = 2;
}
byte r = System.Convert.ToByte(val.Substring(pos, 2), 16);
pos += 2;
byte g = System.Convert.ToByte(val.Substring(pos, 2), 16);
pos += 2;
byte b = System.Convert.ToByte(val.Substring(pos, 2), 16);
c = Color.FromArgb(a, r, g, b);
return new SolidColorBrush(c);
}
else
{
try
{
c = GetColorFromString(value as string);
return new SolidColorBrush(c);
}
catch (InvalidCastException ex)
{
return null;
}
}
}
return null;
}
public static Color GetColorFromString(string colorString)
{
Type colorType = (typeof(System.Windows.Media.Colors));
if (colorType.GetProperty(colorString) != null)
{
object color = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
try
{
return (Color)color;
}
catch
{
throw new InvalidCastException("Color not defined");
}
}
return Colors.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
SolidColorBrush val = value as SolidColorBrush;
return val.Color.ToString();
//if (typeof(Colors).GetProperty(val.Color.ToString()) != null)
// return typeof(Colors).GetProperty(val.Color.ToString()).GetValue(val, null);
//else
// return "#" + val.Color.A.ToString() + val.Color.R.ToString() + val.Color.G.ToString() + val.Color.B.ToString();
}
}
}
Month: February 2011
Write Jpeg file from BitmapSource
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
class UseBitmapCodecs
{
static void WriteJpeg(string fileName, int quality, BitmapSource bmp)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
BitmapFrame outputFrame = BitmapFrame.Create(bmp);
encoder.Frames.Add(outputFrame);
encoder.QualityLevel = quality;
using (FileStream file = File.OpenWrite(fileName))
{
encoder.Save(file);
}
}
}
Use JpegBitmapDecoder
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
class UseBitmapCodecs
{
static string GetCamera(string myJpegPath)
{
JpegBitmapDecoder decoder = new JpegBitmapDecoder(new Uri(myJpegPath),BitmapCreateOptions.None, BitmapCacheOption.None);
BitmapMetadata bmpData = (BitmapMetadata) decoder.Frames[0].Metadata;
return bmpData.CameraModel;
}
}
Set opacity for Image
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="300" Width="400">
<StackPanel Orientation="Horizontal">
<Image Margin="-30" Opacity=".7" Source="c:image.gif"
ToolTip="Middle Image" Width="150" />
</StackPanel>
</Window>
Set tooltip for Image
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="300" Width="400">
<StackPanel Orientation="Horizontal">
<Image Margin="-30" Opacity=".7" Source="c:image.gif"
ToolTip="Middle Image" Width="150" />
</StackPanel>
</Window>
Using the Jpeg Image
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Using the Jpeg Image" Height="300" Width="300"
>
<Canvas>
<Image Source="c:image.jpg" />
</Canvas>
</Window>
Image element
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="http://www.yourURL.com/image.jpeg" />
</Page>

